2015-12-03 51 views
-1

我正試圖在C#中的SqlCommand中執行存儲過程。存儲過程或函數期望沒有提供的參數

這是在C#代碼:

string s = ConfigurationManager.ConnectionStrings["connection"].ToString(); 
SqlConnection conn = new SqlConnection(s); 

conn.Open(); 

SqlCommand cmd = new SqlCommand("Signup1", conn); 
cmd.CommandType = CommandType.StoredProcedure; 

string password = TextBox2.Text; 
cmd.Parameters.Add(new SqlParameter("@email", email)); 

SqlParameter pass = cmd.Parameters.Add("@password", SqlDbType.VarChar, 50); 
pass.Value = password; 

SqlParameter usertype = cmd.Parameters.Add("@usrtype", SqlDbType.VarChar, 50); 
usertype.Value =usertype.Value; 

cmd.ExecuteNonQuery(); 

conn.Close();  

這是存儲過程:

Create Proc Signup1 
    @email varchar(20), 
    @password Varchar(24), 
    @usrtype Varchar(30) 
as 
    Insert into Members(email, password) 
    Values(@email, @password) 

    if @usrtype = 'Normal User' 
    begin 
     Insert into Normal_Users(email) 
     Values(@email) 
    end 
    else if @usrtype = 'Development Team' 
    begin 
     Insert into Development_Eeams(email) 
     Values(@email) 
    end 
    else if @usrtype = 'Verified Reviewer' 
    begin 
     Insert into Verified_reviewers(email) 
     Values(@email) 
    end 
    else 
     raiserror('Invalid type',16,1) 

當我執行該命令出現此錯誤

過程或函數' Signup1'需要參數'@password',它沒有提供。

雖然我給過程的參數值是多少,解決方法是什麼?由於

+1

的數據類型,你爲什麼將它們定義爲不同尺寸時加他們在C#比他們在存儲過程?而且,由於其他人會這樣說,您正在向存儲純文本密碼的問題敞開大門。 –

+0

'cmd.Parameters.Add(new SqlParameter(「@ email」,email));'也許這就是問題..你在哪裏定義電子郵件..? – MethodMan

+0

您需要真正調試您的代碼以及瞭解如何定義您在代碼中聲明的內容大於存儲過程所期望的數據大小。 – MethodMan

回答

0
string email = //where are you getting the email address 
string password = TextBox2.Text; 
string s = ConfigurationManager.ConnectionStrings["connection"].ToString(); 
using (SqlConnection connStr new SqlConnection(s); 
using (SqlCommand cmd = new SqlCommand("Signup1", connStr)) 
{ 
    c.Open(); 
    command.Parameters.Add(new SqlParameter("@email", SqlDbType.VarChar) { Value = email }); 
    command.Parameters.Add(new SqlParameter("@password", SqlDbType.VarChar) { Value = password }); 
    command.Parameters.Add(new SqlParameter("@usrtype", SqlDbType.VarChar) { Value = userType }); //Where are you assigning userType 
    cmd.ExecuteNonQuery(); 
} 

如果上面的例子是複雜的,那麼你可以使用Parameters.AddWithValue功能,讓數據庫服務器處理解決例如

string email = //where are you getting the email address 
string password = TextBox2.Text; 
string s = ConfigurationManager.ConnectionStrings["connection"].ToString(); 
using (SqlConnection connStr new SqlConnection(s); 
using (SqlCommand cmd = new SqlCommand("Signup1", connStr)) 
{ 
    c.Open(); 
    command.Parameters.AddWithValue("@email", email); 
    command.Parameters.AddWithValue"@password", password); 
    command.Parameters.AddWithValue("@usrtype", userType); //Where are you assigning userType 
    cmd.ExecuteNonQuery(); 
} 
+0

我試過這段代碼,但它給了我同樣的錯誤?! – user3423255

相關問題