2014-02-22 70 views
0

此功能正常工作與MDB Access數據庫ADODB.Recordset沒有得到最後插入的ID在一個MySQL數據庫

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 

當我執行下面的代碼,我可以得到最後插入的主鍵(自動遞增)在的* .MDB數據庫

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

假設表的表ID被命名爲 「myTable的」,並與以下字段 「ID」(INT自動增量), 「mynumber的」(文本), 「mytext的」(文本)。

如果我運行MySQL數據庫中具有相同字段的表的代碼,「ThisRecordID」值不等於存儲在最後一條記錄中的ID值。

+0

錯誤:'InsertRecord = Nothing'需要'Set'。 –

回答

0

我改寫了這樣的功能:

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 
+0

BUG:'InsertRecord = Nothing'需要'Set'。 –

0

你不能使用

Dim sql 
If dbConnectionString = " [ your JET connection string ]" then 
sql = "select top 1 ID from MyTable order by ID desc" 
elseif dbConnectionString = " [ your MySQL connection string ]" then 
sql = "select ID from MyTable order by ID desc limit 0,1" 
end if 
rs.open sql 
response.write rs("ID") 
rs.close 
+0

該函數需要使用MDB和MySQL數據庫,LIMIT語句只能在MySQL中使用 – Max

+0

您可以使用條件語句,我編輯了我的答案 – John

0

從你原來的職位,所有你需要做的是設置記錄光標位置。

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 
+0

錯誤:'InsertRecord = Nothing'需要'Set'。 –

+0

同意。我只是在複製他,並添加我的改變。 –

相關問題