我編寫此代碼以從文本框中提取數據到數據庫,但是當它轉到cmd.ExecuteNonQuery();
代碼時,它顯示Incorrect syntax near '-'.
錯誤。在c#中的語法錯誤.net
這是代碼我寫:
private void pictureBox1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
string str = "server=localhost;Database=ord-95;Integrated Security=True";
con.ConnectionString = str;
//SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
//DataSet ds = new DataSet();
cmd.CommandText = " insert into build-seller" +
"(name-build, name-Seller, family-Seller)" +
"values(@name-build, @name-Seller, @family-Seller)";
cmd.Parameters.AddWithValue("@name-build",txtbuild.Text);
cmd.Parameters.AddWithValue("@name-Seller",txtname.Text);
cmd.Parameters.AddWithValue("@family-Seller",txtfamily.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
txtamount.Text = "";
txtbuild.Text = "";
txtfamily.Text = "";
txtname.Text = "";
txtobject.Text = "";
txtshop.Text = "";
MessageBox.Show("success !", "msg",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
加上'[]'圍繞每個字支架與'-',然後再試一次。例如:'[build-seller]','[name-build]'等等。 –
你不能看SQL並解開神祕的錯誤信息嗎? – Plutonix
不要以這種方式連接字符串! [字符串不可變](https://msdn.microsoft.com/en-us/library/362314fe.aspx)! –