此函数将一行插入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查询时,它可以工作。有人能帮忙吗?
答案 0 :(得分:6)
我做同样的事情(无论如何都非常相似)。我相信有两组结果会回来,一组用于INSERT,另一组用于SELECT。尝试调用objRecordSet.NextRecordset()。
答案 1 :(得分:6)
在查询开头使用“SET NOCOUNT ON;”
答案 2 :(得分:1)
我收到了确切的错误,并设法将其跟踪到我在SQL Server存储过程中留下的PRINT语句。一旦我注释了PRINT语句,Recordset就会毫无错误地返回结果。