2014-02-13 74 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并改变你的方法。