As Ak

As Ak

  • NA
  • 65
  • 7.6k

Fetch a binary data from database

Oct 9 2020 2:15 AM
I've a list of hexadecimal value with a data type varbinary in the database. But when I run a query, nothing shown as a result.
 
The example of the hexadecimal value in the database:
 
0x10160000, 0x1016FFFF
0x10170000, 0x1017FFFF
 
This is my current code
  1. //this string SNE will get the data from the text file string  
  2. SNE = data["End_Rg"];  
  3. query = "SELECT Start_Range, End_Range, Status FROM Range WHERE End_Range = '" + SNE + "' ";  
  4. sqlcmd = new SqlCommand(query, cnn);  
  5. readData = sqlcmd.ExecuteReader();  
  6. readData.Read();  
  7. Console.WriteLine("Start Range: {0}", readData["Start_Range"]);  
  8. Console.WriteLine("End Range: {0}", readData["End_Range"]);  
  9. Console.WriteLine("Status: {0}", readData["Status"]);  
Currently, I'm getting an error:
 
> System.InvalidOperationException: 'Invalid attempt to read when no data is present.'
 
Can I know what is the best way to fetch the binary data from the database?

Answers (7)