Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
3.38/5 (4 votes)
See more:
Dear Frnds,

I have Developed employee details and upload image. how to save image into sql database send ur sample codes..



With Regards,
Vivek.R
Posted

As others stated, you may store image data in BLOB type fields. Still, it is not wise. First of all, because those are usually large blocks of data increasing considerably the database size (which is a key factor if you have SQL Express) and could lead to performance problems especially if you have a lower end server and a high load.
SQL Server has a special feature fur such tasks, called FILESTREAM. The main concept is that the actual file data is stored as discrete file directly in the server file system instead of the database file.
This is one article you might see useful: How Do I: Use SQL File Stream[^], and here is another one: http://lennilobel.wordpress.com/2011/01/23/sql-server-2008-filestream-part-3-of-3-using-the-opensqlfilestream-api-2/[^]
 
Share this answer
 
//Save Image
  SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=notitest;user id=sa;password=cos123");
  cn.Open();
  SqlCommand cmd = new SqlCommand("insert imgtest values(" + textBox1.Text + ",'" + ImageToBase64(pictureBox1.Image,
System.Drawing.Imaging.ImageFormat.Png) + "')", cn);
  SqlDataReader dr = cmd.ExecuteReader();
  cn.Close();


//Retrive
SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=notitest;user id=sa;password=cos123");
cn.Open();
SqlCommand cmd = new SqlCommand("select * from imgtest", cn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
    textBox2.Text = dr[0].ToString();
    pictureBox2.Image = Base64ToImage(dr[1].ToString());
}
cn.Close();



//Methods
public string ImageToBase64(Image image,
          System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // Convert Image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                // Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }

        public Image Base64ToImage(string base64String)
        {
            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0,
              imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);
            return image;
        }
 
Share this answer
 
Comments
Member 11335460 7-Jan-15 7:19am    
when retrieving the data i got error as "Invalid character in a Base-64 string."
Please reply..
Sid_Joshi 8-Jan-15 0:57am    
What Data Type U used in sql server database?
and at which line shows error you described?
Member 11335460 8-Jan-15 4:16am    
Thank you for responding.
I used image datatype & i got error in following line,
byte[] imageBytes = Convert.FromBase64String(base64String);
Invalid character in a Base-64 string.
Sid_Joshi 8-Jan-15 4:21am    
Use varchar(max) data type.
If you want to use image data type, the code will be different.
replay me if u want image data type
Member 12209354 17-Dec-15 6:45am    
hi. i have used your code in my project.
I'm currently working on a library management system and i am trying to store my bar code into the database, which is on bitmap. I'm not getting any error in saving the values, but when I open my database, the field for the bar code image is blank. I just wanted to know if you can give me some idea on why this is happening. Looking forward for your response. Thank you so much in advance and also for sharing your code. have a good day!
 
Share this answer
 
Hello ,Have you tried google?There are many links available.
Refer :how to save image into sql Database using c# windows application
 
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