此函数在SQL数据库中插入一行,并需要返回创建的标识号:
Function WriteDatabase(backupTypeID, numImages, folderSize, success, errorMessage, strLogFileName)
On Error Resume Next
err.clear
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
objConnection.Open "Provider=SQLOLEDB;Data Source=x.x.x.x;Initial Catalog=DB;User ID=sa;Password=xxxxxx"
sqlquery = "INSERT INTO tblImageCopies (BackupCopyDate, BackupCopyTypeID, NumImages, ImagesFolderSize, Success, ErrorMessage) VALUES (GETDATE(), " & backupTypeID & ", " & numImages & ", " & folderSize & ", " & success & ", " & errorMessage & "); SELECT scope_identity() AS ImageCopyID;"
objRecordSet.Open sqlquery,objConnection
objRecordSet.MoveFirst
WriteDatabase = objRecordSet("ImageCopyID")
objRecordSet.Close
objConnection.Close
If err.number <> 0 Then
WriteLog "Error writing to the EHN database - " & err.number & " " & err.description, strLogFileName
End If
End Function
它成功地插入了该行,但出现错误消息“3704,当对象关闭时,不允许进行操作。”当它尝试返回记录集中的标识号时。当我直接在服务器上执行此sql查询时,它可以工作。有人能帮忙吗?
最佳答案
我做同样的事情(无论如何都很相似)。我相信会有两套结果返回,一套用于INSERT,另一套用于SELECT。尝试调用objRecordSet.NextRecordset()。