문제

This function works fine with MDB Access databases

Function InsertRecord(dbConnectionString, tableName, primaryKeyName, fieldNames, fieldValues)
    Dim cn,rs,ID
    Set cn = Server.CreateObject("ADODB.Connection")
    Set rs = Server.CreateObject("ADODB.Recordset") 
    cn.Open dbConnectionString
    cn.BeginTrans
    rs.Open tableName, cn, 1, 3, 2 'adOpenKeyset, adLockOptimistic, adCmdTable  
    rs.AddNew fieldNames, fieldValues
    rs.Update
    rs.MoveLast
    ID = rs.Fields(primaryKeyName).Value 'in MySQL it's not returning the last AUTOINCREMENT PrimaryKey ID
    rs.Close
    Set rs = Nothing
    cn.CommitTrans
    cn.Close
    Set cn = Nothing
    If Err.Number = 0 Then
        InsertRecord = ID
    Else
        InsertRecord = Nothing
    End If
End Function

When I execute the following code I can get the last inserted Primary Key (Auto Increment) ID in a table of the *.MDB database

Dim ThisRecordID, arr1, arr2
arr1 = Array("mynumber", "mytext")
arr2 = Array("123456789", "max")            
ThisRecordID = InsertRecord("{connection string}", "myTable", "ID", arr1, arr2) 
Response.Write("Last record ID: " & ThisRecordID & "<br>")

assuming the table is named "myTable" and with the following fields "ID" (INT autoincrement), "mynumber" (text), "mytext" (text).

If I run the code for a table with the same fields in a MySQL database, "ThisRecordID" value is not equal to the ID value stored in the last record.

도움이 되었습니까?

해결책

I rewrote the function like this:

Function InsertRecord(dbConnectionString, tableName, primaryKeyName, fieldNames, fieldValues)
    Dim cn,rs,ID
    Set cn = Server.CreateObject("ADODB.Connection")
    Set rs = Server.CreateObject("ADODB.Recordset") 
    cn.Open dbConnectionString
    cn.BeginTrans
    rs.Open tableName, cn, 1, 3, 2 'adOpenKeyset, adLockOptimistic, adCmdTable  
    rs.AddNew fieldNames, fieldValues
    rs.Update
'   Fixed Bug:
'   reading from a MySQL database, the following code is not returning the last AUTOINCREMENT PrimaryKey ID
'       rs.MoveLast
'       ID = rs.Fields(primaryKeyName).Value
    rs.Close
    Dim sql : sql = "SELECT * FROM " & tableName & " ORDER BY " & primaryKeyName & " DESC"
    Set rs = cn.Execute(sql)
    ID = rs.Fields(primaryKeyName).Value
    rs.Close
    Set rs = Nothing
    cn.CommitTrans
    cn.Close
    Set cn = Nothing
    If Err.Number = 0 Then
        InsertRecord = ID
    Else
        InsertRecord = Nothing
    End If
End Function

다른 팁

From your original post, all you need to do is set the recordset cursor location.

Function InsertRecord(dbConnectionString, tableName, primaryKeyName, fieldNames, fieldValues)
   Dim cn,rs,ID
   Set cn = Server.CreateObject("ADODB.Connection")
   Set rs = Server.CreateObject("ADODB.Recordset") 
   rs.cursorLocation = 3
   cn.Open dbConnectionString
   cn.BeginTrans
   rs.Open tableName, cn, 1, 3, 2 'adOpenKeyset, adLockOptimistic, adCmdTable  
   rs.AddNew fieldNames, fieldValues
   rs.Update
   rs.MoveLast
   ID = rs.Fields(primaryKeyName).Value 
   rs.Close
   Set rs = Nothing
   cn.CommitTrans
   cn.Close
   Set cn = Nothing
   If Err.Number = 0 Then
       InsertRecord = ID
   Else
       set InsertRecord = Nothing
   End If
End Function
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top