Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to Insert images in sql Database?
Posted
Comments
StackQ 13-Dec-12 7:15am    
1st u have to declare type "image" of that column where u want to store them.Then u have to convert ur image file to byte format,store it to byte array,and now insert/update to ur database.
StackQ 13-Dec-12 7:15am    
below i write the code to convert ur image to byte array format.

This code will convert your image to proper format by which you can save your image to the database.
C#
private void btnBrowse_Click(object sender, EventArgs e)
        {   
            
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
 
                openFileDialog1.InitialDirectory = @"C:\";
                openFileDialog1.Title = "Select Ur Image File";
                
                openFileDialog1.Filter = "All Files (*.*)|*.*";
                openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;
 
                openFileDialog1.ReadOnlyChecked = true;
                openFileDialog1.ShowReadOnly = true;
             
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    tbUpload.Text = openFileDialog1.SafeFileName;
string FilePath;
                    FilePath = openFileDialog1.FileName;
                    if (openFileDialog1.FileName != null)    
                    {
                        
System.IO.FileStream fs = new System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
                        System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fs);
                        long byteLength = new System.IO.FileInfo(FilePath).Length;
byte[] Attachmnt;
                        Attachmnt = binaryReader.ReadBytes((Int32)byteLength);
                        fs.Close();
                        fs.Dispose();
                        binaryReader.Close();
                    }                    
                }
               
             }

Now you can store the value of Attachmnt through stored procedure or query into the database, but the column where you want to put your image Attachmnt should be type "image"

Here I explained it through openfiledialog by Browse button, where you will select your image.
 
Share this answer
 
v2
Hi,

Its advised to avoid storing the images in the database.
Please verify the following link you will get your answer too.
Images in the database

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900