Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.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# 调用存储过程的方法中的返回值_C#_Enterprise Library - Fatal编程技术网

C# 调用存储过程的方法中的返回值

C# 调用存储过程的方法中的返回值,c#,enterprise-library,C#,Enterprise Library,我正在使用Microsoft.Practices.EnterpriseLibrary访问C代码中的SQL Server数据库。我有一个方法调用存储过程并向存储过程发送参数,该存储过程返回一行用户名和密码 我不确定如何在此方法中返回值 这是我的代码: public Model.Login GetUsernameandPasswordByPartnerID(int partnerId) { Model.Login login; string myConnection

我正在使用Microsoft.Practices.EnterpriseLibrary访问C代码中的SQL Server数据库。我有一个方法调用存储过程并向存储过程发送参数,该存储过程返回一行用户名和密码

我不确定如何在此方法中返回值

这是我的代码:

public Model.Login GetUsernameandPasswordByPartnerID(int partnerId)
{
        Model.Login login;

        string myConnection = System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ToString();
        SqlDatabase db = new SqlDatabase(myConnection);

        using (DbCommand command = db.GetStoredProcCommand("AS_AuthenticateByPartner"))
        {
            db.AddInParameter(command, "partnerID", DbType.String, partnerId);

            try
            {
                login = db.ExecuteScalar(command); //error on this line

                // rtn = Message("Success", "Acquisition", "GetLogin", "Service", db.ExecuteScalar(command).ToString());
            }
            catch (Exception ex)
            {
            }

            db = null;
            return login;
        }
    }
还有我的Model.login

public class Login
{
    public string username;
    public string password;
}
我不确定该如何使用这一行:

 login=  db.ExecuteScalar(command);
现在我遇到了一个错误,因为您无法显式地将对象转换为Model.login。

ExecuteScalar方法通常用于从数据库返回单个值

试试这个。我假设存储过程最多返回一条带有匹配用户名和密码的记录

using (var command = db.GetStoredProcCommand("AS_AuthenticateByPartner"))
{
    db.CommandType = CommandType.StoredProcedure;

    db.AddInParameter(command, "partnerID", DbType.String, partnerId);

    // make sure myConnection is open before querying the database

    var reader = db.ExecuteReader();

    if (!reader.HasRows)
        return null;  // no records found... return null? display error?

    reader.Read();

    return new Login {username = reader.GetString(0), password = reader.GetString(1)};
}
此代码指定命令文本为存储过程。作为查询的结果,它使用ExecuteReader从数据库中获取可能的多个记录


另外,FWIW,不要使用空的catch块。默默地吃着永远都不好。。。有些事情可能出了问题,您永远也不会知道。

此错误是因为以下语句返回了object is

login=  db.ExecuteScalar(command);
你必须这样做

login=  (Model.Login)db.ExecuteScalar(command);
但这并不能解决您的问题,因为ExecuteScalar不返回.net类型的登录。只有从SQLServer返回int、string等时,它才有用。为了对此有所帮助,我需要知道存储过程返回的是什么

如果我假设你返回的是这样的东西

 select username,password from tblUser
然后不能使用ExecuteScalar,因为它只用于返回单个值

你必须这样做

 Model.Login model = null;
 var result  =  db.ExecuteReader();
 if (!reader.HasRows)
    model = null;
 else 
  {
        reader.Read();
        model = new Model.Login(){ UserName = reader.GetString(0) , Password=  reader.GetString(1) };
  } 

如果您想使用企业库检索数据作为对象,那么您应该考虑使用数据访问器。有关说明,请参阅,它适用于版本5,但同样适用于版本6。