【问题标题】:Dapper return specfic field from queryDapper 从查询中返回特定字段
【发布时间】:2018-08-20 12:22:54
【问题描述】:

我正在连接到 SQL 数据库并在我的 C# 应用程序中设置变量以匹配来自 Dapper 查询的返回值。

我能够返回正确的行信息(我使用 datagridview 来显示我得到了正确的行,并且当我调试时我看到了正确的数据)但是如何将程序变量设置为仅其中一列?这是一些显示我的过程的代码

连接并运行存储过程:

            using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MfgDataCollector"].ToString()))
        {
            DynamicParameters param = new DynamicParameters();
            param.Add("@UserID", txt_userid.Text.Trim());

            List<User> userinfo = conn.Query<User>("GetUserInfo", param, commandType: CommandType.StoredProcedure).ToList<User>();

            Variables.UserID = txt_userid.Text.Trim();

            datagridview1.DataSource = userinfo; //this displays the right information, however I want to store the information as a public variable(like above)
            Variables.Userfull = userinfo; //when debugging this shows I have the right information but its all columns of user

用户类别:

class User
{
    public string UserID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int AccessLevel { get; set; }
    public int FirstUse { get; set; }
    public string Password { get; set; }
    public int LoginCounter { get; set; }
    public string LastLogin { get; set; }
    public string FirstLogin { get; set; }
    public string UserFullName { get; set; }
}

编辑

根据答案,我尝试使用以下代码切换命令以执行标量:

var userinfo = conn.ExecuteScalar<User>("GetUserInfo", param, commandType: CommandType.StoredProcedure);

这让我可以设置

Variables.UserID = userinfo.UserID;

没有红色波浪线但是在运行时我在“System.InvalidCastException: 'Invalid cast from 'System.String' to 'Name_Space.User'”的执行标量上得到一个错误。

我已经检查了用户类以确保数据类型与数据库匹配,并且我发现那里没有问题,我不确定我做错了什么?

【问题讨论】:

    标签: dapper


    【解决方案1】:

    如果您只需要一行中的一列的值,请使用类似于 ADO.NET ExecuteScalarExecuteScalar。它将返回object,您必须进一步处理。

    string sql = "SELECT COL1 FROM Table1 WHERE ID = 1";
    //OR
    //string sql = "SELECT TOP 1 COL1 FROM Table1";
    //OR similar
    object colValue = conn.ExecuteScalar(sql, ....);
    

    如果没有找到匹配记录,返回值为null

    查看 Dapper 文档以了解其他通用方法变体:

    public static T ExecuteScalar<T>(this IDbConnection cnn, CommandDefinition command);
    

    如果您想要多行中的单个列,那么您现在执行此操作的方式是正确的。只需修改您的 SQL 查询/存储过程以返回相同的内容。 Dapper 只会映射返回的列。 User 中的所有其他属性将保持未分配状态。


    如果您只是想将值分配给 Variables.UserID 而不是使用 DataSource 绑定这些值,那么只需这样做:

    Variables.UserID = userinfo.UserID;
    Variables.Userfull = userinfo.UserFullName;
    //and so on....
    

    所以,完整的代码如下:

    using(SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MfgDataCollector"].ToString()))
    {
        DynamicParameters param = new DynamicParameters();
        param.Add("@UserID", txt_userid.Text.Trim());
    
        User userinfo = conn.Query<User>("GetUserInfo", param, commandType: CommandType.StoredProcedure).ToList<User>().First();
    
        Variables.UserID = userinfo.UserID;
        Variables.Userfull = userinfo.UserFullName;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      相关资源
      最近更新 更多