How to store images into the databse
OpenFileDialog op = new OpenFileDialog (); op.ShowDialog ( this ); string imageName = op.FileName; this.pictureBox1.Image = Image.FromFile ( imageName ); long imageLength = op.FileName.Length; FileInfo fi = new FileInfo ( imageName ); byte[] byteArray; imageLength = fi.Length; FileStream fs = new FileStream ( imageName, FileMode.Open, FileAccess.Read, FileShare.Read ); byteArray = new byte[ Convert.ToInt32 ( imageLength ) ]; int byteRead = fs.Read ( byteArray, 0, Convert.ToInt32 ( imageLength ) ); fs.Flush (); fs.Close (); SqlConnection connection = new SqlConnection("connectionString"); string commandText = "Insert into Image values ( @image )"; SqlCommand cmd = new SqlCommand ( commandText, connection ); cmd.Parameters.Add ( "@image", SqlDbType.Image ).Value = byteArray; connection.Open (); cmd.ExecuteNonQuery (); connection.Close ();
|
| Author: Nagamohan kumar P 24 May 2008 | Member Level: Gold Points : 2 |
this is cool ,but could you please say how can we retrieve the image from the database ?
|
| Author: Siva Prasad 26 May 2008 | Member Level: Gold Points : 2 |
// Code for Retrieving Images from Database
SqlConnection connection = new SqlConnection ("connectionString" ); string commandText = "Select [image] from Image Where id=" + textBox1.Text; connection.Open (); SqlCommand cmd = new SqlCommand ( commandText, connection ); byte[] image = null; image= ( byte[] ) cmd.ExecuteScalar (); connection.Close (); string strfn=Convert.ToString(DateTime.Now.ToFileTime()); FileStream fs = new FileStream ( strfn, FileMode.CreateNew, FileAccess.Write ); fs.Write ( image, 0, image.Length ); fs.Flush (); fs.Close (); pictureBox1.Image = Image.FromFile ( strfn );
Siva Prasad
|
| Author: komaladevi 26 May 2008 | Member Level: Gold Points : 2 |
i have given the code to retreive the image from database
|
| Author: syed shakeer hussain 01 Dec 2008 | Member Level: Gold Points : 0 |
hi
|