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


Comments

Author: Dotnet Developer-201507 Jul 2009 Member Level: Gold   Points : 1

hi

anil

chenage this line vb.net here

Dim file() As Byte = SqlConvert.ToByteArray(dr("FileImage"))
Dim fileName As String = SetupFileName(SqlConvert.ToString(dr("FileName")))



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: