如何获取刚刚插入的新记录的自动生成ID?(使用ASP classic和MSSQL 2005)
我正在使用JDBC连接到Java DB数据库,并希望检索插入的最后一条记录的id(自动增量).
我看到这是一个常见问题,但我看到使用例如MS SQL Server的解决方案,Java DB的等价物是什么?
我正在处理用户从gridview中选择的项目,并执行电子邮件和数据库插入操作.
当用户选择按钮时,下面的代码从gridview获取信息,在Order表中创建新订单,并在Transactions表中创建新条目.
如果我使用这种插入记录的方法,如何获取最后插入的ID?你会推荐一种解决这个简单问题的方法吗?
protected void btnOrder_Click(object sender, EventArgs e)
{
double Total = 0;
string MailFrom = "webadmin@domain.com";
string MailTo = "purchasing@domain.com";
string MailSubject = "Online Order";
string MailCC = "";
string MailBCC = "";
string MailReplyTo = "";
string MailBody = "";
TextBox ItmCostCode = (TextBox)form1.FindControl("txtCostCode");
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox cb = (CheckBox)gvr.FindControl("ItemSelect");
Label ItmTotal = (Label)gvr.FindControl("ItmTotal");
Label ItmPrice = (Label)gvr.FindControl("ItmPrice");
Label ItmName = (Label)gvr.FindControl("lblName");
TextBox ItmQty = (TextBox)gvr.FindControl("ItmQty");
TextBox ItmID = (TextBox)gvr.FindControl("lblItemID");
//Add entry to …Run Code Online (Sandbox Code Playgroud) 我目前正在使用下面的方法来获取最后插入的行的ID.
Database.ExecuteNonQuery(query, parameters);
//if another client/connection inserts a record at this time,
//could the line below return the incorrect row ID?
int fileid = Convert.ToInt32(Database.Scalar("SELECT last_insert_rowid()"));
return fileid;
Run Code Online (Sandbox Code Playgroud)
到目前为止,这种方法工作正常,但我不确定它是否完全可靠.
假设有两个客户端应用程序,每个应用程序都有自己独立的数据库连接,它们在同一时间调用上面的服务器端方法.请记住,客户端线程并行运行,并且SQLite一次只能运行一个操作(或者我听说过),一个客户端实例是否可以返回由插入的记录的行ID另一个例子?
最后,是否有更好的方法来获取最后插入的行ID?
sql ×3
c# ×2
asp-classic ×1
asp.net ×1
gridview ×1
javadb ×1
jdbc ×1
oracle ×1
sql-server ×1
sqlite ×1