我對C#相當新,我試圖設置調用存儲過程在我的數據庫,它採用一個參數。C#存儲過程或函數期望不提供參數
我得到的錯誤「過程或函數‘SP_getName’需要參數‘@用戶名’,但未提供。」當我與參數提供它
我的存儲過程工作正常,我通過SQL運行管理工作室。
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[SP_getName]
@username = 'bob101'
SELECT 'Return Value' = @return_value
GO
但是,當我嘗試調用它的錯誤是我如何傳入參數,但我不能發現問題是什麼。
//create a sql command object to hold the results of the query
SqlCommand cmd = new SqlCommand();
//and a reader to process the results
SqlDataReader reader;
//Instantiate return string
string returnValue = null;
//execute the stored procedure to return the results
cmd.CommandText = "SP_getName";
//set up the parameters for the stored procedure
cmd.Parameters.Add("@username", SqlDbType.NVarChar).Value = "bob101";
cmd.CommandType = CommandType.Text;
cmd.Connection = this.Connection;
// then call the reader to process the results
reader = cmd.ExecuteReader();
任何幫助發現我的錯誤將不勝感激!
我也試着看這兩個職位,但我沒有任何運氣:
Stored procedure or function expects parameter which is not supplied
Procedure or function expects parameter, which was not supplied
謝謝!
順便說一句,您不應該使用SP_作爲您的過程的前綴。這是保留給系統特效。如果MS在某些時候釋放具有相同名稱的proc,則不會再運行。老實說,你根本不應該使用前綴,他們沒有增加任何澄清。 – 2014-10-31 14:29:43