An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Incorrect syntax near '4'.
Unclosed quotation mark after the character string ''.
我的代碼:
public partial class editemp : Window
{
SqlConnection con = new SqlConnection(@"Data Source=Haier;Initial Catalog=HRMS;Integrated Security=True");
SqlCommand cmd;
SqlDataReader dr;
int empid;
public editemp()
{
InitializeComponent();
}
private void editemp_fun(object sender, RoutedEventArgs e)
{
con.Open();
searchid(textBox.Text);
dr.Close();
con.Close();
con.Open();
cmd = new SqlCommand("update Employee set EmpID='"+empid+ "',E_Name='" + textBox.Text + "',E_Contact='" + textBox_Copy.Text + "',Designaiton='" + textBox_Copy1.Text + "',Password='" + passwordBox.Password.ToString() + "Where EmpID='"+empid.ToString() +"'", con);
cmd.ExecuteNonQuery();
con.Close();
new employees().Show();
Close();
}
public void searchid(string name)
{
//con.Open();
cmd = new SqlCommand("Select * from Employee where E_Name='"+textBox.Text+"'",con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
empid = Convert.ToInt32(dr[0].ToString());
}
}
private void cancel(object sender, RoutedEventArgs e)
{
new employees().Show();
Close();
}
}
那必須是焦點主要查詢
cmd = new SqlCommand("update Employee set EmpID='"+empid+ "',E_Name='" + textBox.Text + "',E_Contact='" + textBox_Copy.Text + "',Designaiton='" + textBox_Copy1.Text + "',Password='" + passwordBox.Password.ToString() + "Where EmpID='"+empid.ToString() +"'", con);
在where子句中它給上面提到的錯誤。
非常感謝你
第一件事:停止bu像這樣的SQL。改用參數化的SQL。這可能是所有你需要做的事情,但它絕對應該是你做的第一件事。請參閱http://bobby-tables.com –
在關鍵字之前給出「引用」和「空間」。 「'其中EmpID ='」+ empid.ToString() –
接下來,瞭解.NET的命名約定,並遵循它們。 –