2010-08-26 63 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