2010-08-26 64 views
0

我想获取存储过程的输出到一个函数,但与我的代码我总是得到一个错误,告诉输出参数没有指定。你能帮忙吗?错误获取存储过程的输出

的代码是在这里.....

public int UserAuthentication(String username, String password) 
{ 
    SqlConnection conn = new SqlConnection("Data Source=...\\..; Initial Catalog=CUSTOMER360;User ID=sa;password=******"); 
    SqlCommand command = new SqlCommand("sp_Campaign_UserAuthentication", conn); 
    command.CommandType = CommandType.StoredProcedure; 
    command.Parameters.Add(new SqlParameter("@login_id", SqlDbType.VarChar, 50, "loginid")); 
    command.Parameters.Add(new SqlParameter("@password", 
     SqlDbType.VarChar,50, 
     "password")); 
    SqlParameter ret = command.Parameters.Add(" @result", SqlDbType.Int); 
    ret.Direction = ParameterDirection.ReturnValue; 
    command.Parameters["@login_id"].Value = username; 
    command.Parameters["@password"].Value = password; 
    conn.Open(); 
    command.ExecuteNonQuery(); 
    conn.Close(); 
    return (int)ret.Value; 
} 
+1

我假设你发布的sa用户密码是一个虚拟字符串? :-) – 2010-08-26 09:48:44

回答

0

我想返回值参数必须在(编辑)命令参数列表中的第一个。

此外,请在关闭连接之前尝试查询返回参数的值。

确保您的存储过程调用

RETURN (@SomeReturnValue) 

最后,尝试在“@result”参数名除去最主要的空间,以防万一。

+0

@result是返回值,它在参数列表中。而且我也试图在关闭连接之前得到返回值,但结果是一样的......... – Alivia 2010-08-26 10:01:25

+0

您的代码有该参数作为第三个参数添加到命令的参数列表中。将其作为第一个移动。 – 2010-08-26 10:15:14

+0

你能否也发布存储过程的头文件?只需要proc名称和参数列表就可以了。我想知道你是否混合了Output和ReturnValue参数类型。 – 2010-08-26 10:20:35