gpt4 book ai didi

sql-server - 将 Picturebox 中的图像与 SQL Image 数据类型进行比较

转载 作者:行者123 更新时间:2023-12-03 11:25:51 27 4
gpt4 key购买 nike

我有一个代码可以将图片框(vb6 中的图像)中的图像保存到 SQL 中,数据类型为 Image,这里是它的输出。

Column Name = Picture

enter image description here

我的问题是如何比较这里的图像

enter image description here

进入我的 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而不是 =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
  • 关于sql-server - 将 Picturebox 中的图像与 SQL Image 数据类型进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40714239/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com