如何使用c#windows应用程序将图像保存到sql数据库中 [英] how to save image into sql Database using c# windows application

查看:96
本文介绍了如何使用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

推荐答案

检查此链接,确实有效。



< a href =http://www.dbtutorials.com/retrieve/saving-retrieving-image-cs/> http://www.dbtutorials.com/retrieve/saving-retrieving-image-cs/ [ ^ ]
Check this link, It works.

http://www.dbtutorials.com/retrieve/saving-retrieving-image-cs/[^]


正如其他人所说,您可以将图像数据存储在BLOB类型字段中。不过,这并不明智。首先,因为那些通常是大数据块会大大增加数据库大小(如果你有SQL Express,这是一个关键因素)并且可能导致性能问题,特别是如果你有一个低端服务器和高负载。 />
SQL Server有一个特殊功能,称为FILESTREAM。主要概念是实际的文件数据直接存储在服务器文件系统而不是数据库文件中。

这是一篇你可能认为有用的文章:我如何:使用SQL文件流 [ ^ ],这是另一个: HTTP:/ /lennilobel.wordpress.com/2011/01/23/sql-server-2008-filestream-part-3-of-3-using-the-opensqlfilestream-api-2/ [ ^ ]
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/[^]


//保存图像

//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



//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;
        }


这篇关于如何使用c#windows应用程序将图像保存到sql数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆