How to put images in SQL database?
I want to know how to store images in sql database using c# and also how to display the through c# stored in word file and also in a folder
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mukesh KumarRank #119 · 18.2K reputationPosted Nov 26, 2015, 8:26 PM EST
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; }Krishna Rajput SinghRank #605 · 4K reputationPosted Nov 26, 2015, 8:39 PM EST
Ravi PatelRank #439 · 5.7K reputationPosted Nov 26, 2015, 9:01 PM EST