我正在为T-SQL创建一个简单的桌面示例,但由于某种原因,它引发了一个异常。int j = cmd.ExecuteNonQuery();
出现异常
DateTime d = DateTime.Now;
String Id = textBox4.Text;
String date = d.ToString();
String quantity = textBox2.Text;
String connectionstring = @"Data Source = (LocalDB)MSSQLLocalDB; AttachDbFilename = C:UsersJunaid ComputerssourcereposWindowsFormsApp3Softech.mdf;Integrated Security = True";
SqlConnection con = new SqlConnection(connectionstring);
String query = "INSERT INTO order (CustomerId,OrderDate,OrderQuantity) VALUES('" + Id + "','" + date + "','" + quantity + "')";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
int j = cmd.ExecuteNonQuery();
if (j > 0)
{
MessageBox.Show("Data Inserted:");
}
else if (j == 0)
{
MessageBox.Show("SOrry! No Insertion:");
}
试试INSERT INTO "order".
单词"订单;是一个保留关键字。这对我有效。