我想从用户向 DataTable 插入数据。我得到这个异常:“异常:事务附近的语法不正确”。我试图找到语法错误,但无法解决。
public void Add_to_Transaction(SqlConnection conn , int serial_number , DataTable dt)
{
try
{
//DataTable dt = new DataTable();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = conn;
cmd1.CommandText = "SELECT * FROM Transaction";
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd1;
adapter.Fill(dt);
SqlParameter serial_number1 = new SqlParameter("product_id",serial_number);
SqlParameter quantity = new SqlParameter("quantity", 0);
SqlParameter date = new SqlParameter("date", DateTime.Today);
cmd1.Parameters.Add(serial_number1);
cmd1.Parameters.Add(quantity);
cmd1.Parameters.Add(date);
Console.WriteLine("111111111111111111");
cmd1.CommandText = "INSERT INTO Transaction (quantity,date,product_id) VALUES (@quantity,@date,@product_id)";
cmd1.ExecuteNonQuery();
Console.WriteLine("222222222222222222");
cmd1.Parameters.Clear();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}