i want opposite of this
Hi here image is getting converted into binary and getting inserted into sqltable .
Now I want the opposite of it i mean i want to retrieve the binary
data and display it as image.
protected void btnupload_Click(object sender, EventArgs e)
{
// set temporary variable for database connection
SqlConnection SqlCon = new SqlConnection("Data Source=localhost;Initial Catalog=ArjunPlastic;Uid=sa;Pwd=vagi0903");
SqlCon.Open();
string SQL = "INSERT INTO pictures (Filename,Picture) VALUES ('flower',@image)";
SqlCommand SqlComm = new SqlCommand(SQL,SqlCon);
SqlParameter parimage= new SqlParameter("@image", SqlDbType.Image);
// convert image file to byte array and pass to sql parameter value
parimage.Value = FileToByteArray("C:\\Documents and Settings\\Administrator\\Desktop\\4014_thumb.jpg");
SqlComm.Parameters.Add(parimage);
SqlComm.ExecuteNonQuery();
}
public byte[] FileToByteArray(string FileName)
{
byte[] Buffer = null;
// Open file for reading
FileStream FileStream = new FileStream(FileName,FileMode.Open,FileAccess.Read);
// attach filestream to binary reader
BinaryReader BinaryReader = new BinaryReader(FileStream);
// get total byte length of the file
long TotalBytes = new FileInfo(FileName).Length;
// read entire file into buffer
Buffer = BinaryReader.ReadBytes((Int32)TotalBytes);
// close file reader
FileStream.Close();
FileStream.Dispose();
BinaryReader.Close();
return Buffer;
}
coollife
Junior Poster in Training
85 posts since Aug 2009
Reputation Points: 6
Solved Threads: 0
Please use code tags when posting code on daniweb:
[code=c#]
...code here...
[/code]
Next -- Please stop opening duplicate thread. I have already given you the answer to that in http://www.daniweb.com/forums/thread209172.html
For some reason you have in your head that I am not storing the image as binary on the SQL server which is not the case. Please try the code first.
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
Thread closed, double posting is not welcome.
Stick to original post.
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902