Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting to Byte Array after reading a BLOB from SQL in C#

Tags:

c#

sqlite

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

like image 893
Soham Avatar asked Apr 17 '10 10:04

Soham


People also ask

How do you create a BLOB byte array?

At first, I have created an object of Scanner class and read my file using FileReader() method. Then created a BLOB object initialized with null. Next, I have Converted the file into a Byte array ( byte[] ), stored it into my_byte_array. Finally converted the byte array into BLOB using SerialBlob() method.

Is BLOB same as byte array?

Blob simply means (Binary Large Object) and its the way database stores byte array.

Can you store a byte array in SQL?

In Sql Server, use image data type or varbinary data type, to store byte array data.

What is Bytearray C#?

In C#.Net, we can create an unsigned byte array by using byte, byte is used to store only positive values between the range of 0 to 255 (Unsigned 8 bits integer). It occupies 1-byte memory for each element, if array size is 10, it will take 10 bytes memory.


2 Answers

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.

like image 120
Guffa Avatar answered Nov 14 '22 20:11

Guffa


byte[] binDate = (byte[])row["Date"];
like image 43
Sandeep Sachan Avatar answered Nov 14 '22 20:11

Sandeep Sachan