0
我試圖將圖像插入到SQL Server數據庫中,插入varbinary(MAX)
類型的列中;但無論我做什麼,我得到這個錯誤:將圖像作爲varbinary插入到SQL Server數據庫時出現Excel VBA錯誤(MAX)
System Error &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
你能用你在SSMS上使用的確切SQL語句來編輯你的文章嗎?也許有一個語法問題。 vba會告訴你哪一行是錯誤的嗎? – ShanayL
不是SQL服務器的人,但運行數據庫的帳戶是否有權訪問存儲jpg的文件夾? –
我認爲@TimWilliams是正確的。從0x8開始的錯誤代碼是訪問權限錯誤。通常這些是由於打開或訪問文件的權限不足造成的,但這可能是由內存訪問問題引起的(在這種情況下,我認爲不太可能)。 –