Getting primary key after an insert in asp.net (visual basic)

前端 未结 5 841
野的像风
野的像风 2021-01-15 00:39

I\'m adding a record like this:

    Dim pathString As String = HttpContext.Current.Request.MapPath(\"Banking.mdb\")
    Dim odbconBanking As New OleDbConnect         


        
5条回答
  •  感动是毒
    2021-01-15 01:32

    I believe a parameterized query would look something like this:

    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()
    

    My syntax might be a bit off as I am more accustomed to using the Sql Server library and not the Odbc library, but that should get you started.

提交回复
热议问题