string sql = "Insert into tbl_borrowed (FirstName,LastName,BookName,Category,DateBorrowed,Time,DateToBeReturned) values (@fname,@lname,@bname,@category,@dborrow,@time,@dreturn)";
string sql2= "Insert into tbl_return (FirstName,LastName,BookName,Category,DateBorrowed,Time) values (@fname,@lname,@bname,@category,@dborrow,@time";
MySqlCommand sda = new MySqlCommand("", conn);
sda.CommandType = CommandType.Text;
sda.CommandText = sql;
sda.Parameters.AddWithValue("@fname", txtfname.Text);
sda.Parameters.AddWithValue("@lname", txtlname.Text);
sda.Parameters.AddWithValue("@bname", txtbook.Text);
sda.Parameters.AddWithValue("@category", cmbcategory.Text);
sda.Parameters.AddWithValue("@dborrow", dateTimePicker1.Value.Date);
sda.Parameters.AddWithValue("@time", this.time.Text);
sda.Parameters.AddWithValue("@dreturn", dateTimePicker2.Value.Date);
MessageBox.Show("Item has been added!");
showlv("Select * from tbl_borrowed", lvborrowed);
showlv2("Select * from tbl_return", rb.lvreturn);
txtfname.Clear();
txtlname.Clear();
txtbook.Clear();
cmbcategory.Clear();
dateTimePicker1.ResetText();
dateTimePicker2.ResetText();
try
{
conn.Open();
sda.CommandText = sql2;
sda.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("ASDF" + ex);
}
finally
{
conn.Close();
}
我想在2个表中插入相同的值。我只是一个初学者,请帮助我。忍受我......
错误:
答案 0 :(得分:1)
在INSERT
查询中Time
是一个保留字,需要使用如下所示的backtique进行转义。顺便说一下,你的INSERT
声明都有同样的错误。
Insert into tbl_borrowed (FirstName,LastName,BookName,Category,DateBorrowed,`Time`,DateToBeReturned)
同样,不是以这种方式执行多个INSERT
,而是将这些查询包装在存储过程中并从代码中调用该过程要好得多。如果你需要的话,你可以通过将INSERT
块中的两个包装在同一个事务块中来实际运行begin trans
。