4

I am trying to read binary from database and write as a file in local disk using c#.

using the below code... But there is problem in this line : byte[] fileAsByte = byte.Parse(row["Blob"]);

public static void ReadBlob()
{ 
    int icount = 0;
    string FileName;
    SqlConnection Mycon = new SqlConnection(Con);
    Mycon.Open();
    string queryString = "select * from " + TblName;
    SqlDataAdapter adapter = new SqlDataAdapter(queryString, Mycon);

    DataTable dtBlob = new DataTable();
    adapter.Fill(dtBlob);


    foreach (DataRow row in dtBlob.Rows)
    {
        byte[] fileAsByte = byte.Parse(row["Blob"]);
        FileName = FilePath + TblName + row["BlobId"].ToString() + FileType;

        WriteBlob(fileAsByte, FileName);
    }

    Mycon.Close();
}

public static void WriteBlob(byte[] buff, string fileName)
{
    try
    {
        FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
        BinaryWriter bw = new BinaryWriter(fs);
        bw.Write(buff);
        bw.Close(); 
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    } 
}
Oleks
  • 31,955
  • 11
  • 77
  • 132
Anuya
  • 8,082
  • 49
  • 137
  • 222
  • What is happening? At first glance, it looks that the only problem is the file name, that will have the row values: " row["BlobId"].ToString() ". – Pih Apr 29 '11 at 08:35
  • What is "the problem" exactly? I can probably, guess... but are you getting an Exception? If so, please post the WHOLE exception message. The more info you give us, the more actual help you're likely to get. – corlettk Apr 29 '11 at 08:35

2 Answers2

5

byte.Parse will try to parse a single byte. Have you tried just casting?

byte[] fileAsByte = (byte[]) row["Blob"];

If that fails, it should at least show you what type is actually in the DataRow. Hopefully it's some type which is reasonably easily convertible to byte[].

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I'd do a "type safe cast" instead: `byte[] fileAsByte = (row["Blob"]) as byte;` (In .NET 3.5, VS2008 at least) – corlettk Apr 29 '11 at 08:44
  • @corlettk: I wouldn't. If the conversion fails, I'd *want* it to throw an exception, assuming this is required data which should always be there. Only use `as` when it's *valid* for the conversion to fail. – Jon Skeet Apr 29 '11 at 08:46
  • @corlettk: If you think `(byte[])data` is not safe but `data as byte[]` is, you are mistaken. .NET is type safe no matter what syntax you use. The only difference is that first one will throw an exception if `data` is not of type `byte[]` but second will return a `null` reference. – Hemant Apr 29 '11 at 08:47
  • /Hemant: Thank you both for clearing that up... Humble pie... Rueful grin. – corlettk Apr 29 '11 at 09:22
0

If your column is of type varbinary(max), you can use GetSqlBytes or GetSqlBinary on the SqlDataReader. If your column is of type varchar(max) or nvarchar(max), use GetSqlChars on SqlDataReader. You can as well use, GetBytes {this takes a size of the array buffer} or GetSqlBytes.

Also, as suggested above, for varbinary(MAX) the following line as well should work

byte[] binaryData = (byte[])row["Blob"];

Hope this helps.

Sanket Naik
  • 215
  • 1
  • 4
  • 11