Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Save Image in Sql Server Using C#


In this blog I will tell you that how to save image in Sql Server using c#. Here I will give you necessary steps to store image in sql server.

1)      Before we are starting to make application in C# firstly we need to create a table in sql server where we store out image. Here I will create a table named ImageTable in which I will store image. ImageTable contains two column one is imageID which will store id of image in image table. And other column which have name imageData is stored image in format of binary.

Here I will provide a sample sql code snippet to create ImageTable

create table ImageTable

(

     imageID int,

     imageData image

)

2)      Now import System.Data.SqlClient namespace in your project. This namespace provide you all necessary classes and interfaces which is necessary to establish connection to SqlServer.

3)      Create following methods and call it according to your requiremnt.

/// 

        /// On click event of browse button write down following code. This

        /// Will display a open file dialog box which will ask to choose you

        /// image and will Store path of image in textbox.

        /// 

        /// 

        /// 

        private void button1_Click(object sender, EventArgs e)

        {

            OpenFileDialog ofd = new OpenFileDialog();

            if (DialogResult.OK == ofd.ShowDialog())

            {

                textBox1.Text = ofd.FileName;   //storing path of image in textbox.

            }

        }

        /// 

        /// On the click of SaveButton call saveImageInDataBase(int imgId) method

        /// which will save image in database.

        /// 

        /// 

        /// 

        private void button2_Click(object sender, EventArgs e)

        {

            saveImageInDataBase(1);

        }

        /// 

        /// This is a required method which will save image in database.

        /// 

        /// 

        public void saveImageInDataBase(int imageID)

        {

            byte[] imageData = ReadImageFile(textBox1.Text);    //This nethod returns image in Byte Array format.

            SqlConnection con = new SqlConnection();

            con.ConnectionString = "Data Source=aaaa;User ID=sa;Password=abcd;Initial Catalog=Workbook";  //provide connection string of your server.

            con.Open();   //open connection to server.

            string query = "insert into ImageTable values(@imageId,@imageData)";     //create a query variable.

            SqlCommand cmd = new SqlCommand(query, con);          //create a sqlcommand object.

            cmd.Parameters.Add(new SqlParameter("@imageId", imageID));    //add first parameters.

            cmd.Parameters.Add(new SqlParameter("@imageData", imageData));    //add second parameters.

            int rows = cmd.ExecuteNonQuery();         //execute query.

            if (rows > 0)

                MessageBox.Show("Image saved.");

            else

                MessageBox.Show("Unable to save image.");

            con.Close();

        }

        /// 

        /// This method will converts image in byte array format and returns to its caller.

        /// use System.IO namespace regarding streaming concept.

        /// 

        /// 

        /// 

        public byte[] ReadImageFile(string imageLocation)

        {

            byte[] imageData = null;

            FileInfo fileInfo = new FileInfo(imageLocation);

            long imageFileLength = fileInfo.Length;

            FileStream fs = new FileStream(imageLocation, FileMode.Open, FileAccess.Read);

            BinaryReader br = new BinaryReader(fs);

            imageData = br.ReadBytes((int)imageFileLength);

            return imageData;

        }

    }



This post first appeared on Microsoft SharePoint, please read the originial post: here

Share the post

Save Image in Sql Server Using C#

×

Subscribe to Microsoft Sharepoint

Get updates delivered right to your inbox!

Thank you for your subscription

×