Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Dapper调用T-SQL存储过程时,是否需要使用'@';?_C#_Tsql_Stored Procedures_Dapper - Fatal编程技术网

C# 使用Dapper调用T-SQL存储过程时,是否需要使用'@';?

C# 使用Dapper调用T-SQL存储过程时,是否需要使用'@';?,c#,tsql,stored-procedures,dapper,C#,Tsql,Stored Procedures,Dapper,我想用Dapper调用一个存储过程,到目前为止我看到的所有代码都是通过用@字符指定存储过程参数来实现的。这意味着我不能简单地定义一个模型类并通过重新转换模型实例将其传递到Dapper的查询或执行方法中,这似乎是在浪费时间和内存。这是我真正需要做的吗 例如,下面的代码接受一个模型实例。该模型具有sp_GetUser表的所有属性。我希望Dapper在调用存储过程时将这些模型属性传递到存储过程的参数中。是这样还是我真的需要定义在“param”参数中传递的对象 public IList<U

我想用Dapper调用一个存储过程,到目前为止我看到的所有代码都是通过用@字符指定存储过程参数来实现的。这意味着我不能简单地定义一个模型类并通过重新转换模型实例将其传递到Dapper的查询或执行方法中,这似乎是在浪费时间和内存。这是我真正需要做的吗

例如,下面的代码接受一个模型实例。该模型具有sp_GetUser表的所有属性。我希望Dapper在调用存储过程时将这些模型属性传递到存储过程的参数中。是这样还是我真的需要定义在“param”参数中传递的对象

    public IList<User> GetUsers(UserSP user)
    {
        using (var cn = new SqlConnection(ConnectionString))
        {
            var users = cn.Query<User>("sp_GetUsers",
                    param: new
                    {
                        @Id = user.Id,
                        @NAme = user.Name,
                        @Age = user.Age
                    },
                    commandType: CommandType.StoredProcedure).ToList();
            return users;
        }
    }
public IList GetUsers(UserSP user)
{
使用(var cn=new-SqlConnection(ConnectionString))
{
var users=cn.Query(“sp_GetUsers”,
param:新的
{
@Id=user.Id,
@NAme=user.NAme,
@年龄=用户年龄
},
commandType:commandType.StoredProcess.ToList();
返回用户;
}
}

试试这样的方法

班级

 public class ReportIndex
    {
        public int SlideNumber { get; set; }
        public string ChartName { get; set; }
        public string SheetName { get; set; }
    }
让全班同学衣冠楚楚

public List<ReportIndex> GetReportIndex(int reportId)
    {
        List<ReportIndex> reportIndex = null;

        using (var conn = new SqlConnection(connString))
        {
            conn.Open();

            var p = new DynamicParameters();
            p.Add("@ReportId", reportId);

            reportIndex = conn.Query<ReportIndex>("sp_ReportGetReportIndex",
                                        p,
                                        commandType: CommandType.StoredProcedure).ToList();
        }

        return reportIndex;
    }
public List GetReportIndex(int reportId)
{
List reportIndex=null;
使用(var conn=new SqlConnection(connString))
{
conn.Open();
var p=新的动态参数();
p、 添加(“@ReportId”,ReportId);
reportIndex=conn.Query(“sp_ReportGetReportIndex”,
P
commandType:commandType.StoredProcess.ToList();
}
回报指数;
}

不需要,您不需要使用@-字符()。我还需要只传入那些可以映射到过程参数的属性吗?你能试试吗?param:userRight,很遗憾不是。是的,只允许那些可以映射到过程参数的属性。否则它将引发异常。为什么要使用“@”?为什么要使用“new DynamicParameters()”?这是我使用dapper向SP发送参数的方式。之所以这样问,是因为本文中的问题专门针对dapper调用中的“@”字符。对我问题的第一个评论回答了我的问题。