Tuesday, July 7, 2009

Retrieving Image from SQL Server

hi,

Please check out this code for retrieving the image from the sql server, first get the image field data in a data reader and from there we can convert the data into a BYTE array, using that array the file can be created..

have a look

byte[] file = SqlConvert.ToByteArray(dr["FileImage"]);

string fileName = SetupFileName(SqlConvert.ToString(dr["FileName"]));

string filePath = System.Environment.CurrentDirectory + “\\myFiles\\” + fileName;

FileStream fs
= new FileStream(filePath, FileMode.Create, FileAccess.Write);

BinaryWriter bw
= new BinaryWriter(fs);

bw.
Write(file);

bw.
Flush();

bw.
Close();

fs.
Close();


Happy Coding!!!! :)

Thanks
Anil Kumar Pandey
System Architect
Green Point Technology (India) Ltd.
Mumbai, Maharshtra
INDIA

Kontera