我有一个代码将图像从图片框(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
最佳答案 这应该工作,我做了一些改变,希望这有帮助.
>我正在使用命令,因为我从不相信数据是安全的,即使我只会使用该程序
>我删除了WITH(NOLOCK)这对索引不好,可能会降低性能.
>将您的if语句更改为>而不是= 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