本文介绍了如何使用c#windows应用程序将图像保存到sql数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 亲爱的朋友们, 我已经开发了员工详细信息并上传了图片。如何将图像保存到sql数据库中发送你的示例代码.. 有了问候, Vivek.R 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推荐答案 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 //RetriveSqlConnection 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(); //方法 //Methodspublic 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; } 这篇关于如何使用c#windows应用程序将图像保存到sql数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 14:57