Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Searching Sql Table for Image using byte array parameter

Searching Sql Table for Image using byte array parameter

Scheduled Pinned Locked Moved Visual Basic
helpdatabasealgorithmsdata-structuresquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thedom2
    wrote on last edited by
    #1

    Hi All Im trying desparately to search a sql table image field passing in a parameter which is a byte array. I am having no luck whatsoever. I get this error when i run my code below :The data types image and image are incompatible in the equal to operator. Public Sub SearchByteArray(ByVal ByteArr() As Byte) Dim strSQL As String Dim oConn As SqlConnection Dim oCmd As SqlCommand Dim oBLOBParam As SqlParameter Dim str As String = "" ' Try ' Create and open connection object oConn = New SqlConnection("data source=XXX;initial catalog=CustomApps;user id=sa;pwd=xxx;") oConn.Open() ' Insert statement ' Notice that @BLOBValue is a placeholder for the actual data strSQL = "SELECT BlobField from MyImageTable where BlobField = (@BLOBValue)" ' Create a command object oCmd = oConn.CreateCommand() ' Set SQL statement oCmd.CommandText = strSQL ' Create a command parameter oBLOBParam = New SqlParameter("@BLOBValue", SqlDbType.Image, ByteArr.Length, ParameterDirection.Input) ' Finally, set the actual data oBLOBParam.Value = ByteArr ' Add this parameter to the command oCmd.Parameters.Add(oBLOBParam) ' Execute SQL statement MessageBox.Show(oCmd.ExecuteNonQuery()) ' Close the connection oConn.Close() End Sub Can anyone help or assist please ?

    L 1 Reply Last reply
    0
    • T thedom2

      Hi All Im trying desparately to search a sql table image field passing in a parameter which is a byte array. I am having no luck whatsoever. I get this error when i run my code below :The data types image and image are incompatible in the equal to operator. Public Sub SearchByteArray(ByVal ByteArr() As Byte) Dim strSQL As String Dim oConn As SqlConnection Dim oCmd As SqlCommand Dim oBLOBParam As SqlParameter Dim str As String = "" ' Try ' Create and open connection object oConn = New SqlConnection("data source=XXX;initial catalog=CustomApps;user id=sa;pwd=xxx;") oConn.Open() ' Insert statement ' Notice that @BLOBValue is a placeholder for the actual data strSQL = "SELECT BlobField from MyImageTable where BlobField = (@BLOBValue)" ' Create a command object oCmd = oConn.CreateCommand() ' Set SQL statement oCmd.CommandText = strSQL ' Create a command parameter oBLOBParam = New SqlParameter("@BLOBValue", SqlDbType.Image, ByteArr.Length, ParameterDirection.Input) ' Finally, set the actual data oBLOBParam.Value = ByteArr ' Add this parameter to the command oCmd.Parameters.Add(oBLOBParam) ' Execute SQL statement MessageBox.Show(oCmd.ExecuteNonQuery()) ' Close the connection oConn.Close() End Sub Can anyone help or assist please ?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I am not an SQL or database expert but I would be very surprised if SQL could do record selection based on BLOB content. I do have one suggestion: you could add an 'imageHash' field to your table, then with an app, calculate and store the image hash value one. Any algorithm would be fine. Later on, when you want to search for an image, calculate its hash value, then search for the hash value. NB: there is a slim chance a hash search may result in more than one match, so you would still need code to compare the bytes if you want to be absolutely sure you got the right image. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups