Compare Picturebox Image to SQL Image Data Type

I have code that saves an image from an image window (image in vb6) in SQL, a data type that is an image, and here is the result of that.

Column Name = Picture

enter image description here

My question is: how can I compare the image here?

enter image description here

in my SQL database? My goal here is to check if an image exists image3in my database.

Here is my code and it doesn't work.

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

I think the best way to do this is to convert image3to binary ( Picture Column) and execute the select command.

Please, I hope someone helps me with this.

Tysm

+4
source share
1 answer

, , .

  • , , , -
  • 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
    
+1

Source: https://habr.com/ru/post/1661408/


All Articles