2014-02-13 76 views
0

我想使用sql命令插入一些數據到數據庫表中。我不斷收到此錯誤:錯誤在sql命令c#

Must declare the scalar variable "@MC" 

代碼:

void CreateModule() { 
    Query = "INSERT INTO TblModules (Module_Code, Module_Name, Date,Start_Time, Duration) values (@MC, @MN, @DATE, @ST, @DURATION)"; 

    theReader = dc.ExecuteStatement(Query); 
    command.Parameters.AddWithValue("@MC", ModuleCodeTxt.Text); 
    command.Parameters.AddWithValue("@MN", ModulenameTxt.Text); 
    command.Parameters.AddWithValue("@DATE", MyDateTimePicker.Value); 
    command.Parameters.AddWithValue("@ST", comboBoxTime.SelectedItem); 
    command.Parameters.AddWithValue("@DURATION", comboBoxDuration.SelectedItem); 

    MessageBox.Show(" Module Created"); 
    conn.CloseConnection(); 
} 
+1

'dc'是什麼類型? –

+0

對不起,這意味着博康...如在 – user3187867

+1

你能告訴我們ExecuteStatement方法嗎? –

回答

7

您應該爲您的參數給值執行查詢

command.CommandText = Query; // <-- don't forget to set command text 
command.Parameters.AddWithValue("@MC", ModuleCodeTxt.Text); 
command.Parameters.AddWithValue("@MN", ModulenameTxt.Text); 
command.Parameters.AddWithValue("@DATE", MyDateTimePicker.Value); 
command.Parameters.AddWithValue("@ST", comboBoxTime.SelectedItem); 
command.Parameters.AddWithValue("@DURATION", comboBoxDuration.SelectedItem); 

//execute the query here, maybe you want this? 
theReader = command.ExecuteReader(); 

之前順便說在哪裏您的command?你的Query變量只是一個字符串字面值,它包含命令文本。我不知道你的方法在做什麼,但我認爲你應該將command傳遞給你的ExecuteStatement並改變你的方法。