我正在编写一个需要在数据库中存储图像的 VB 应用程序。用户在他们的计算机上选择图像,这将路径作为字符串提供给我。这是我的尝试,但是我收到错误“INSERT INTO 查询不能包含多值字段”。
这是我的代码:
Dim buff As Byte() = Nothing
Public Function ReadByteArrayFromFile(ByVal fileName As String) As Byte()
Dim fs As New FileStream(fileName, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
Dim numBytes As Long = New FileInfo(fileName).Length
buff = br.ReadBytes(CInt(numBytes))
Return buff
End Function
Sub ....
Dim connImg As New OleDbConnection
Dim sConnString As String
Dim cmdImg As New OleDbCommand
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Settings.DB & ";Persist Security Info=False;"
connImg = New OleDbConnection(sConnString)
connImg.Open()
cmdImg.Connection = connImg
cmdImg.CommandType = CommandType.Text
If d.slogo <> "" Then
cmdImg.CommandText = "INSERT INTO Logo ( refId, [type], [img] ) VALUES(@refId, @type, @imgBinary)"
cmdImg.Parameters.Add("@refId", OleDbType.Double).Value = refId
cmdImg.Parameters.Add("@type", OleDbType.Double).Value = 0
cmdImg.Parameters.Add("@imgBinary", OleDbType.VarBinary).Value = ReadByteArrayFromFile(PathToImage)
cmdImg.ExecuteNonQuery()
End If
....
End Sub
我曾尝试在线搜索其他解决方案,但似乎我找到的所有内容都是 VB6 或 VBA 代码。我知道人们会争辩说图像不应该存储在数据库中,但在这种情况下,这是我唯一的选择。
感谢您的任何帮助!