[英]SQL INSERT an ID of freshly inserted row
im writing a library database program. 我正在编写一个图书馆数据库程序。 It can insert books, but I have a problem in making a reference between book and a person which rents it.
它可以插入书籍,但是我很难在书籍和租书者之间进行参考。 I can't get a last inserted id from a rents table to put it to the compilation table to assign book to a person who rents it.
我无法从租金表中获得最后插入的ID,无法将其放入编译表中,无法将书分配给出租人。 I've tried SCOPE_IDENTITY() but it doesn't works for me.
我已经尝试过SCOPE_IDENTITY(),但是它对我不起作用。 Here's the code:
这是代码:
private void addRentButton_Click(object sender, EventArgs e) {
elibrary f1 = new elibrary();
string query = "INSERT INTO rents VALUES (@renterName, @rentStartDate, @rentEndDate)";
using(f1.Connection = new SqlConnection(f1.connectionString))
using(SqlCommand command = new SqlCommand(query, f1.Connection)) {
f1.Connection.Open();
command.Parameters.AddWithValue("@renterName", rentNameBox.Text);
command.Parameters.AddWithValue("@rentStartDate", DateTime.Now);
command.Parameters.AddWithValue("@rentEndDate", rentEndDatePicker.Value);
command.ExecuteScalar();
}
rentEndDatePicker.Value = DateTime.Now;
string Compilationquery =" INSERT INTO compilation VALUES (@bookId, SELECT SCOPE_IDENTITY())";
using(f1.Connection = new SqlConnection(f1.connectionString))
using(SqlCommand command = new SqlCommand(Compilationquery, f1.Connection)) {
f1.Connection.Open();
command.Parameters.AddWithValue("@bookId", f1.listBook.SelectedValue);
command.ExecuteScalar();
Actually, you are not retrieving the last inserted ID value from the first query, since the SCOPE_IDENTITY()
is wrongly placed and you are not assigning the ExecuteScalar()
return value anywhere: 实际上,您没有从第一个查询中检索最后插入的ID值,因为
SCOPE_IDENTITY()
放置错误,并且您没有在任何地方分配ExecuteScalar()
返回值:
String query = "INSERT INTO rents VALUES (@renterName, @rentStartDate, @rentEndDate); SELECT CONVERT(INT, SCOPE_IDENTITY())"; // "SELECT CAST(SCOPE_IDENTITY() AS INT)" can also be an option
Int32 lastId = 0;
using (f1.Connection = new SqlConnection(f1.connectionString))
using (SqlCommand command = new SqlCommand(query, f1.Connection))
{
f1.Connection.Open();
command.Parameters.AddWithValue("@renterName", rentNameBox.Text);
command.Parameters.AddWithValue("@rentStartDate", DateTime.Now);
command.Parameters.AddWithValue("@rentEndDate", rentEndDatePicker.Value);
lastId = (Int32)command.ExecuteScalar();
}
Once this is done, you can proceed with the second query as follows: 完成此操作后,您可以继续进行第二个查询,如下所示:
String compilationQuery = "INSERT INTO compilation VALUES (@bookId, @rentId)";
using (f1.Connection = new SqlConnection(f1.connectionString))
using (SqlCommand command = new SqlCommand(compilationQuery, f1.Connection))
{
f1.Connection.Open();
command.Parameters.AddWithValue("@bookId", f1.listBook.SelectedValue);
command.Parameters.AddWithValue("@rentId", lastId);
// ...
You have disposed the command so SCOPE_IDENTITY() is gone. 您已经处置了该命令,所以SCOPE_IDENTITY()不存在了。 There is no reason to dispose of the commmand twice.
没有理由将命令处理两次。
using(SqlCommand command = new SqlCommand(query, f1.Connection))
{
f1.Connection.Open();
command.Parameters.AddWithValue("@renterName", rentNameBox.Text);
command.Parameters.AddWithValue("@rentStartDate", DateTime.Now);
command.Parameters.AddWithValue("@rentEndDate", rentEndDatePicker.Value);
command.ExecuteScalar();
int id = (Int32)command.ExecuteScalar();
command.Parameters.Clear();
Compilationquery = "INSERT INTO compilation VALUES (@bookId, @id)";
command.CommandText = Compilationquery;
command.Parameters.AddWithValue("@bookId", f1.listBook.SelectedValue);
command.Parameters.AddWithValue("@id", id);
command.ExecuteScalar();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.