在 asp.net 中插入后获取主键(visual basic)
Posted
技术标签:
【中文标题】在 asp.net 中插入后获取主键(visual basic)【英文标题】:Getting primary key after an insert in asp.net (visual basic) 【发布时间】:2008-12-06 03:52:44 【问题描述】:我正在添加这样的记录:
Dim pathString As String = HttpContext.Current.Request.MapPath("Banking.mdb")
Dim odbconBanking As New OleDbConnection _
("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" + pathString)
Dim sql As String
sql = "INSERT INTO tblUsers ( FirstName, LastName, Address, City, Province, Zip, Phone, UserName, [Password])" & _
" VALUES ('" & firstName & "', '" & lastName & "', '" & address & _
"', '" & city & "', '" & province & "', '" & zip & "', '" & phone & "', '" & username & "', '" & password & "');"
odbconBanking.Open()
Dim cmd As New OleDbCommand(sql, odbconBanking)
cmd.ExecuteNonQuery()
odbconBanking.Close()
主键是一个名为 UserID 的自动编号字段。那么,如何获取我刚刚插入的记录的主键呢?
谢谢。
【问题讨论】:
【参考方案1】:我相信参数化查询看起来像这样:
Dim pathString As String = HttpContext.Current.Request.MapPath("Banking.mdb")
Dim odbconBanking As New OleDbConnection _
("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" + pathString)
Dim sql As String
sql = "INSERT INTO tblUsers ( FirstName, LastName, Address, City, Province, Zip, Phone, UserName, [Password])" & _
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
odbconBanking.Open()
Dim cmd As New OleDbCommand(sql, odbconBanking)
//Add Params here
cmd.Parameters.Add(new OdbcParameter("@FirstName", firstName))
cmd.Parameters.Add(new OdbcParameter("@LastName", lastName))
//..etc
//End add Params here
cmd.ExecuteNonQuery()
Dim newcmd As New OleDbCommand("SELECT @@IDENTITY", odbconBanking)
uid = newcmd.ExecuteScalar
odbconBanking.Close()
我的语法可能有点偏离,因为我更习惯于使用 Sql Server 库而不是 Odbc 库,但这应该可以帮助您入门。
【讨论】:
【参考方案2】:"SELECT @@IDENTITY"
在插入之后应该可以与 Microsoft.Jet Provider 一起使用。
【讨论】:
【参考方案3】:你会想要添加
;select scope_identity();
查询后,运行 executeScalar 以取回值。
另外,在一个完全不相关的注释中,您确实应该更改您的代码以使用参数化查询,这样您就不会陷入 SQL 注入攻击,或者由于连接到您的 sql 语句中的不正确转义字符串而导致崩溃。
【讨论】:
如何进行参数化查询?【参考方案4】:知道了。上面的代码现在是这样的:
Dim pathString As String = HttpContext.Current.Request.MapPath("Banking.mdb")
Dim odbconBanking As New OleDbConnection _
("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" + pathString)
Dim sql As String
sql = "INSERT INTO tblUsers ( FirstName, LastName, Address, City, Province, Zip, Phone, UserName, [Password])" & _
" VALUES ('" & firstName & "', '" & lastName & "', '" & address & _
"', '" & city & "', '" & province & "', '" & zip & "', '" & phone & "', '" & username & "', '" & password & "');"
odbconBanking.Open()
Dim cmd As New OleDbCommand(sql, odbconBanking)
cmd.ExecuteNonQuery()
Dim newcmd As New OleDbCommand("SELECT @@IDENTITY", odbconBanking)
uid = newcmd.ExecuteScalar
odbconBanking.Close()
【讨论】:
【参考方案5】:最简单的方法是在代码级别生成您的 uniqueIdentifier 值,将其添加到您的字符串中并将其发送到服务器。
Dim sql As String, _
myNewUserId as variant
myNewUserId = stGuidGen 'this function will generate a new GUID value)'
sql = "INSERT INTO tblUsers ( userId, LastName)" & _
" VALUES ('" & myNewUserId & "','" & LastName);"
您可以查看stGuidGen guid 生成代码here 的提案。
这种客户端技术清晰、干净且有用
【讨论】:
以上是关于在 asp.net 中插入后获取主键(visual basic)的主要内容,如果未能解决你的问题,请参考以下文章
sQL数据库表的主键列设为标识,增量为1,下次插入数据时能不插入主键列吗
我的 ASP.NET / Visual Basic 代码有啥问题?