Saturday, March 10, 2012

save / retrieve image in SQL SERVER database (C#)


I think storing images in Database can save a developer lots of time and can ease his life while dealing with visuals, either creating a local application or a web application. Think of storing your images of a certain web application in a database instead in a directory exits somewhere in a cloud which one-a bit headache and second-completely transparent with respects to its location. So, here we are, I am going to create here a local application through which we can insert and retrieve an image using c# from a sql server 2008 database.

 First of all you need to create database to store and retrieve images in the form of bytes as follows....

 imageid, imagename, image are the columns for the table images

here we need 3 buttons, 1 file dialog control one image control to display image and store image in DB.

Declare 4 global variables as stated...

        string imageid;
        string imagename;
        SqlDataAdapter da;
        DataSet ds;

In btnSelect_Click to select Image

            try
            {
                FileDialog fldlg = new OpenFileDialog();

                fldlg.InitialDirectory = @":D\"; 

                fldlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";   // To select OnlyImages            
                if (fldlg.ShowDialog() == DialogResult.OK)
                {
                    imagename = fldlg.FileName;
                    Bitmap newimg = new Bitmap(imagename);
                    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                    pictureBox1.Image = (Image)newimg;
                }
                fldlg = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

In brnsave _Click event


 try
            {              
                if (imagename != "" && imageid != "")
                {
                    FileStream fs;
                    fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);

                    byte[] picbyte = new byte[fs.Length];
                    fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
                    fs.Close();
                   
                    //open the database using odp.net and insert the data
                    string connstr = "Data Source=(local)\\MYDATABASE;Initial Catalog=images;Persist Security Info=True;User ID=sa;Password=*****";                   
                    SqlConnection conn = new SqlConnection(connstr);
                    conn.Open();
                    string query;
                    query = "insert into test_image(id_image,pic) values(" + textBox1.Text + "," + " @pic)";
                    SqlParameter picparameter = new SqlParameter();
                    picparameter.SqlDbType = SqlDbType.Image;
                    picparameter.ParameterName = "pic";
                    picparameter.Value = picbyte;
                    SqlCommand cmd = new SqlCommand(query, conn);
                    cmd.Parameters.Add(picparameter);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Image Added");
                    cmd.Dispose();
                    conn.Close();                   
                    Connection();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

No comments:

Post a Comment