我试图将图像插入到SQL Server数据库中,插入到varbinary(MAX)
类型的列中;但无论我做什么,我都会收到这个错误:
系统错误& H80040E14(-2147217900)
我做错了什么?请帮帮我:-( ...
当我在SQL Server Management Studio中运行它时,SQL语句可以正常工作。
这是我的代码:
Public Sub ExecuteSQL()
Dim Conn As ADODB.Connection
Dim SQL As String
Dim Connected As Boolean
DBServer = Sheets("Config").Cells(12, 2).Value
DBName = Sheets("Config").Cells(13, 2).Value
DBUser = Sheets("Config").Cells(14, 2).Value
DBPass = Sheets("Config").Cells(15, 2).Value
Connected = ConnectToDB(Conn, CStr(DBServer), CStr(DBName), CStr(DBUser), CStr(DBPass))
SQL = "INSERT INTO Items (ItemName, Description, Image) "
SQL = SQL & "VALUES ('Item1', 'This is a test', (SELECT BulkColumn FROM OPENROWSET('C:\Temp\Test.JPG', SINGLE_BLOB) as Rec))"
RecCount = Query(Conn, SQL, Sheets("SQLResults").Cells(1, 1))
Conn.Close
Set Conn = Nothing
End Sub
Function ConnectToDB(Conn As ADODB.Connection, Server As String, Database As String, UserName As String, Password As String) As Boolean
Set Conn = New ADODB.Connection
On Error Resume Next
Conn.ConnectionString = "Provider=SQLOLEDB; Server=" & Server & "; Database=" & Database & ";" & "Uid=" & UserName & ";" & "Pwd=" & Password & ";"
Conn.Open
If Conn.State = 0 Then
ConnectToDB = False
Else
ConnectToDB = True
End If
End Function
Function Query(Conn As ADODB.Connection, SQL As String, RangeToPaste As Range) As Integer
Dim recordSet As ADODB.recordSet
Dim Field As ADODB.Field
Dim Col As Long
Query = 0
Set recordSet = New ADODB.recordSet
recordSet.Open SQL, Conn, adOpenStatic, adLockReadOnly, adCmdText ' <--- Error Here'
If recordSet.State Then
Query = recordSet.RecordCount
RangeToPaste.CopyFromRecordset recordSet
Set recordSet = Nothing
End If
End Function
答案 0 :(得分:0)
嗯,最终是权限问题(感谢蒂姆)我发现为了让SQL用户能够运行图像查询,例如插入和编辑,用户应该具有服务器角色:bulkadmin。