當我編譯程序時,我收到沒有錯誤,但只要我點擊保存按鈕 我收到'InvalidOperationException was unhandled'。 cmd.ExecuteNonQuery();然後突出顯示,我花了一段時間在這個,沒有運氣。希望你能幫助。InvalidOperationException被未處理
private void save_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection("host=" ";user="";password=""; database="" ;");
con.Open();
MySqlCommand cmd = new MySqlCommand("INSERT INTO Staff (username, password, FirstName, SecondName, Phone, Email, Role, Phone 2, Fax)" + "values" + "("+username+","+password+","+Fname+","+Lname+","+Phone+","+email+","+role+","+Phone2+","+Fax+")");
cmd.BeginExecuteNonQuery();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data Inserted");
con.Close();
}
你的'MySqlCommand'對象如何知道'MySqlConnection';)? – Kevin 2011-03-10 11:23:48
在查詢中使用參數,它將爲您節省sql注入。 – 2011-03-10 11:30:08