我有一个代码将图像从图片框(vb6中的图像)保存到SQL,其数据类型是Image,这里是输出。
Column Name = Picture
我的问题是如何比较这里的图像
进入我的SQL数据库?我的目标是检查image3
中的图像是否存在于我的数据库中。
这是我的代码,但它不起作用。
Dim arrImageByte() As Byte
Dim strPhotoPath As String
strPhotoPath = Image3.Picture & ".jpg"
Set rs = New ADODB.Recordset
Open strPhotoPath For Binary As #1
ReDim arrImageByte(FileLen(strPhotoPath))
fNum = FreeFile()
Open strPhotoPath For Binary As #fNum
Get #fNum, , arrImageByte
Close fNum
Text1.Text = FreeFile
rs.Open "select * from tbl_image with (nolock) where CONVERT(varbinary,[picture]) = '" & Text1.Text & "'", sql, 1, 1, 1
If rs.RecordCount = 0 Then
MsgBox "Image exist"
Else
MsgBox "Image does not exist."
End If
我认为最好的方法是将image3
转换为二进制(Picture Column
)并执行select命令。
请希望有人帮助我
TYSM
答案 0 :(得分:1)
这应该有效,我做了一些改变,希望这会有所帮助。
WITH(NOLOCK)
这对索引不利,可能会降低性能。将您的if语句更改为>0
而不是=0
,因为如果您获得了结果,则会显示图片不存在。
Dim arrImageByte() As Byte
Dim strPhotoPath As String
strPhotoPath = Image3.Picture & ".jpg"
Set rs = New ADODB.Recordset
Open strPhotoPath For Binary As #1
ReDim arrImageByte(FileLen(strPhotoPath))
fNum = FreeFile()
Open strPhotoPath For Binary As #fNum
Get #fNum, , arrImageByte
Close fNum
Text1.Text = FreeFile
Set cmd = New ADODB.Command
cmd.ActiveConnection = sql
cmd.CommandText ="SELECT * FROM tbl_image where " & _
"CONVERT(varbinary,[picture]) = CONVERT(varbinary,?)"
cmd.Parameters(1)=Text1.Text
rs = cmd.Execute()
'Change this
If rs.RecordCount > 0 Then 'instead of =
MsgBox "Image exist"
Else
MsgBox "Image does not exist."
End If