【发布时间】: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