使用 Dapper select 来自 MS SQL 的数据

Using Dapper to select data from MS SQL

我用Dapper select data form Mssql,结果显示"null"

(使用存储过程、List获取数据,"myDictionary[0].Account"和myDictionary[0].Password获取详细信息),

但是 MS SQL 数据库有数据。

如何修复代码?谢谢。

public void Do_Click(object sender, EventArgs e)
    {
        string strAccount = Request.Form["userAccount"];
        string strPassword = Request.Form["userPwd"];

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

                List<LoginModel> myDictionary = 
                    conn.Query<LoginModel>(@"uspSelectLoginChk",
                                            new { LoginAcc = strAccount, LoginPsd = strPassword }, commandType: CommandType.StoredProcedure).ToList();
               string strAccountChk =  myDictionary[0].Account;
               string strPASChk =  myDictionary[0].Password;

                conn.Close();


                if (strAccountChk != null && strAccountChk != null)
                {

                    Response.Redirect("test.aspx");
                }
                else
                {

                }


            }
            catch (Exception ex)
            {
               response.write( ex.ToString());
            }
        }

    }

您需要检查: 在 sql 中使用可以在调试代码中查看的参数执行 sp。

   example:
   exec uspSelectLoginChk 'LoginAccValue', 'LoginPsdValue'

如果sql中的任何数据执行,你的代码是错误的,如果没有数据,你可以在数据调试结果之前插入数据。 谢谢

试试这个,

// Modified your code
public void Do_Click(object sender, EventArgs e)
{
    string strAccount = Request.Form["userAccount"];
    string strPassword = Request.Form["userPwd"];
    var _para = new DynamicParameters();
    _para.Add("@LoginAcc", strAccount);
    _para.Add("@LoginPsd", strPassword);
    var _list = _con.Query<LoginModel>("uspSelectLoginChk", _para, commandType: CommandType.StoredProcedure); // _con is SqlConnection _con = new SqlConnection("your connection string")
    if(_list != null) {
        Response.Redirect("test.aspx");
    }

}