6

I need to read a BLOB and store it in a byte[], before going forward with Deserializing;

Consider:

 //Reading the Database with DataAdapterInstance.Fill(DataSet);
     DataTable dt = DataSet.Tables[0];
    foreach (DataRow row in dt.Rows)
    {
    byte[] BinDate = Byte.Parse(row["Date"].ToString()); // convert successfully to byte[]

    }

I need help in this C# statement, as I am not able to convert an object type into a byte[]. Note, "Date" field in the table is a blob and not of type Date;

Help appreciated; Soham

Soham
  • 863
  • 2
  • 19
  • 35
  • You may take a look at the answer I gave to a similar question: http://stackoverflow.com/questions/625029/how-do-i-store-and-retrieve-a-blob-from-sqlite/625485#625485 – Darin Dimitrov Apr 17 '10 at 10:40

3 Answers3

18

Just cast the value to a byte array:

byte[] binDate = (byte[])row["Date"];

A blob in the database maps to a byte array in .NET, so the database driver have already done that conversion for you.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2
byte[] binDate = (byte[])row["Date"];
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
0

If "Date" is a blob, it should already come out as a byte[] - not sure why you are calling ToString(), but Byte.Parse will only parse a single byte.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900