简体   繁体   中英

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

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

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.

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

如果“日期”是一个blob,它应该已经出来作为一个byte [] -不知道为什么您呼叫ToString()Byte.Parse只会解析一个字节。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM