Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Code Snippets » ADO.NET »
Saving Image in Sql Server
|
//Create a windows application //place one textbox and two buttons named store and load //place a combobox and a button called retrieve //create a database Image demo //create table named testimage with //id_image nvarchar(50),pic image
//code for loading the selected image in the picture box
private void btnLoad_Click(object sender, EventArgs e) { try { FileDialog fdlg = new OpenFileDialog(); //filter used to open the particular type of files fdlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";
if (fdlg.ShowDialog() == DialogResult.OK) { imagename = fdlg.FileName; Bitmap newimg = new Bitmap(imagename); pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox1.Image = (Image)newimg; } fdlg = null; } catch (Exception ex) { imagename = ""; MessageBox.Show(ex.Message.ToString()); } }
//code for saving the image in the database //call this function in the update button click
private void UpdateData() { try { if (imagename != "") { FileStream fs; fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read); //bytearray to read the image byte[] imgarr = new byte[fs.Length]; fs.Read(imgarr, 0, System.Convert.ToInt32(fs.Length)); fs.Close();
string connstr = @"Data Source=.;Initial Catalog=MyDB; uid=sa;pwd=welcome";
SqlConnection conn = new SqlConnection(connstr);
conn.Open(); string query;
query = "insert into testimage (id_image,pic) values(" + textBox1.Text + "," + " @pic)"; SqlParameter picparameter = new SqlParameter(); picparameter.SqlDbType = SqlDbType.Image; picparameter.ParameterName = "pic"; picparameter.Value = imgarr; SqlCommand cmd = new SqlCommand(query, conn); cmd.Parameters.Add(picparameter); cmd.ExecuteNonQuery(); MessageBox.Show("Image Added"); cmd.Dispose(); conn.Close(); conn.Dispose(); Connection(); } } catch (Exception ee) { MessageBox.Show(ee.Message.ToString()); } }
//code for reloading the image from database in picturebox
private void button3_Click(object sender, EventArgs e) { DataTable dataTable = dset.Tables[0]; //if there is an already an image in picturebox, then delete it if (pictureBox2.Image != null) {
pictureBox2.Image.Dispose();
}
//using filestream object write the column as bytes and store it as an image
FileStream FS1 = new FileStream(AppDomain.CurrentDomain.BaseDirectory+@"\"+@"image.jpg", FileMode.Create);
foreach (DataRow dataRow in dataTable.Rows)
{
if (dataRow[0].ToString() == comboBox1.SelectedItem.ToString())
{
byte[] blob = (byte[])dataRow[1];
FS1.Write(blob, 0, blob.Length);
FS1.Close();
FS1 = null; //file will get saved in the application directory pictureBox2.Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"\" + @"image.jpg");
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox2.Refresh();
}
} }
//code for connection and loading //inserted image id values in the combobox
private void Connection() { //connect to the database and table //selecting all the columns //adding the name column alone to the combobox try { string connstr = @"Data Source=.;Initial Catalog=MyDB; uid=sa;pwd=welcome"; SqlConnection conn = new SqlConnection(connstr); conn.Open(); empadap1 = new SqlDataAdapter(); empadap1.SelectCommand = new SqlCommand("SELECT * FROM testimage" , conn); dset = new DataSet("dset"); empadap1.Fill(dset); DataTable dtable; dtable = dset.Tables[0]; comboBox1.Items.Clear(); foreach (DataRow drow in dtable.Rows) { comboBox1.Items.Add(drow[0].ToString()); comboBox1.SelectedIndex = 0; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|