[英]Retreive last record inserted
I want to retrieve the last record of data inserted. 我想检索插入数据的最后一条记录。 I researched and learn't I must use SELECT LAST_INSERT_ID().
我研究了一下,知道不是必须使用SELECT LAST_INSERT_ID()。 I have tried and not working.
我已经尝试了,但是没有工作。 So I have provided my code below
所以我在下面提供了我的代码
//Inserting into Student Table
string query = "INSERT INTO student(name) Values('" + txtboxname.Text + "' )";
// query= "SELECT LAST_INSERT_ID()";
// query += " SELECT SCOPE_IDENTITY()";
//Inserting into course table
string query1 = "INSERT INTO course(title) Values('" + txttitle.Text + "')";
// query1 = " SELECT LAST_INSERT_ID()";
//Execute Insert Statement queries
MySqlCommand command = new MySqlCommand(query, con);
command.ExecuteNonQuery();
command = new MySqlCommand(query1, con);
command.ExecuteNonQuery();
//Close and dispose off connections
command.Dispose();
con.Close();
//Preview page //预览页面
string query1 = "SELECT (name) FROM student WHERE ID = id";
string query2 = "SELECT (title) FROM course WHERE ID = id";
MySqlCommand command = new MySqlCommand(query1, con);
lblname.Text = command.ExecuteScalar().ToString();
command = new MySqlCommand(query2, con);
lbltitle.Text = command.ExecuteScalar().ToString();
con.Close();
如果您已将表ID设置为自动递增,则根据我的知识,如果可以,则从表中查找将是您最后插入的最大ID。
In your Code change as follows, 在您的代码更改如下,
line no: 3 query="set @std_id=last_insert_id();"
行号:3
query="set @std_id=last_insert_id();"
line no: 7 query1="set @title_id=last_insert_id();"
行号:7
query1="set @title_id=last_insert_id();"
Preview Page: 预览页面:
string query1 = "SELECT (name) FROM student WHERE ID = query";
string query2 = "SELECT (title) FROM course WHERE ID = query1";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.