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);
}
}