【问题标题】:How to save image in database using C# [closed]如何使用 C# 将图像保存在数据库中 [关闭]
【发布时间】:2011-04-02 16:20:53
【问题描述】:

我想用 C# 将用户图像保存到数据库中。我该怎么做?

【问题讨论】:

标签: c# sql asp.net-mvc ado.net


【解决方案1】:

试试这个方法。当您要存储图像的字段为byte 类型时,它应该可以工作。 首先它为图像创建byte[]。然后它使用binary 类型的IDataParameter 将其保存到数据库中。

using System.Drawing;
using System.Drawing.Imaging;
using System.Data;

    public static void PerisitImage(string path, IDbConnection connection)
    {
        using (var command = connection.CreateCommand ())
        {
            Image img = Image.FromFile (path);
            MemoryStream tmpStream = new MemoryStream();
            img.Save (tmpStream, ImageFormat.Png); // change to other format
            tmpStream.Seek (0, SeekOrigin.Begin);
            byte[] imgBytes = new byte[MAX_IMG_SIZE];
            tmpStream.Read (imgBytes, 0, MAX_IMG_SIZE);

            command.CommandText = "INSERT INTO images(payload) VALUES (:payload)";
            IDataParameter par = command.CreateParameter();
            par.ParameterName = "payload";
            par.DbType = DbType.Binary;
            par.Value = imgBytes;
            command.Parameters.Add(par);
            command.ExecuteNonQuery ();
        }
    }

【讨论】:

  • 对于那些稍后阅读本文的人,DbType.Binary 处理 varbinary(MAX),即使帮助工具提示说它限制为 8000 字节。
【解决方案2】:

您需要在 C# 中将图像转换为 byte[],然后您的数据库列将是 varbinary(MAX)

之后,就像保存任何其他数据类型一样。

【讨论】:

    【解决方案3】:

    这是在asp.net中使用FileUpload控件的方法:

    byte[] buffer = new byte[fu.FileContent.Length];
    Stream s = fu.FileContent;
    s.Read(buffer, 0, buffer.Length);
    //Then save 'buffer' to the varbinary column in your db where you want to store the image.
    

    【讨论】:

      【解决方案4】:

      您需要将图像序列化为可以存储在 SQL BLOB 列中的二进制格式。假设您使用的是 SQL Server,这里有一篇关于这个主题的好文章:

      http://www.eggheadcafe.com/articles/20020929.asp

      【讨论】:

      【解决方案5】:

      我个人的偏好是不将图像保存到数据库中。将图像保存在文件系统中的某个位置,并在数据库中保存参考。

      【讨论】:

      • 仅在少数情况下有用,因为您必须使相关文件系统对所有 .NET 代码开放。也是不一致的。只要您永远不需要扩展到网络农场,在网络应用程序中就足够有用了,此时共享文件可能会很麻烦。
      • 您可以使用 SQL msdn.microsoft.com/en-us/library/cc949109.aspx - 从 2.1 开始也可在 nHIbernate 中使用 zvolkov.com/blog/post/2009/07/20/… 在数据库中序列化不是一个好的设计策略 - 必须在文件夹上设置权限并不是一个很好的理由做得不好:)
      • @cvista 我经常将图像保存到数据库。将 memcached 实例放入其中,它会变得更具吸引力。
      • 在任何情况下都有用,尤其是农场。您创建一个服务来保存图像并将引用存储在 SQL Server 中。影像服务会自动填充场。您可以获得自动横向扩展和冗余。
      【解决方案6】:

      您可以将图像的路径保存在数据库中或将图像本身保存为 BLOB(二进制 - 字节数组)。这取决于您的情况,如果您的应用程序是 Web 应用程序,则保存图像的路径要好得多。但是如果您有一个基于客户端的应用程序连接到一个集中式数据库,那么您必须将其保存为二进制文件。

      【讨论】:

      • 这必须是评论。用户要求别的东西。如果您对该方法有批评意见,请发表评论。
      【解决方案7】:

      由于您使用的是 SQL,因此建议您不要使用 adhoc(“在字符串中编写语句”),特别是考虑到您正在加载图像。

      ADO.NET 可以为您完成映射、转义等所有繁重的工作。

      要么创建存储过程,要么使用 SqlParameter 进行绑定。

      正如其他海报所说,使用 VARBINARY(MAX) 作为您的存储类型 - IMAGE 正在被弃用。

      【讨论】:

        【解决方案8】:

        我认为这里已经回答了这个有效的问题。我也试过了。我的问题只是使用图片编辑(来自 DevExpress)。这就是我解决它的方法:

        • 将 PictureEdit 的“PictureStoreMode”属性更改为 ByteArray: 它当前设置为“默认”
        • 将控件的编辑值转换为再见: byte[] newImg = (byte[])pictureEdit1.EditValue;
        • 保存图片: this.tbSystemTableAdapter.qry_updateIMGtest(newImg);

        再次感谢您。查格伯特

        【讨论】:

        • 最初的问题是如何使用 C# 将图像保存到数据库中,您提出的解决方案涉及从 DevExpress 添加第三方产品,而不是使用请求和可用的 C# 实现解决方案。跨度>
        【解决方案9】:
        //Arrange the Picture Of Path.***
        
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
        
                            string[] PicPathArray;
                            string ArrangePathOfPic;
        
                            PicPathArray = openFileDialog1.FileName.Split('\\');
        
                            ArrangePathOfPic = PicPathArray[0] + "\\\\" + PicPathArray[1];
                            for (int a = 2; a < PicPathArray.Length; a++)
                            {
                                ArrangePathOfPic = ArrangePathOfPic + "\\\\" + PicPathArray[a];
                            }
                       }
        
        // Save the path Of Pic in database
        
                            SqlConnection con = new SqlConnection("Data Source=baqar-pc\\baqar;Initial Catalog=Prac;Integrated Security=True");
                            con.Open();
        
                            SqlCommand cmd = new SqlCommand("insert into PictureTable (Pic_Path) values (@Pic_Path)", con);
                            cmd.Parameters.Add("@Pic_Path", SqlDbType.VarChar).Value = ArrangePathOfPic;
        
                            cmd.ExecuteNonQuery();
        
        ***// Get the Picture Path in Database.***
        
        SqlConnection con = new SqlConnection("Data Source=baqar-pc\\baqar;Initial Catalog=Prac;Integrated Security=True");
                        con.Open();
        
                        SqlCommand cmd = new SqlCommand("Select * from Pic_Path where ID = @ID", con);
        
                        SqlDataAdapter adp = new SqlDataAdapter();
                        cmd.Parameters.Add("@ID",SqlDbType.VarChar).Value = "1";
                        adp.SelectCommand = cmd;
        
                        DataTable DT = new DataTable();
        
                        adp.Fill(DT);
        
                        DataRow DR = DT.Rows[0];
                        pictureBox1.Image = Image.FromFile(DR["Pic_Path"].ToString());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-26
          • 2015-06-27
          • 2015-08-13
          • 1970-01-01
          • 2015-01-08
          • 2013-02-09
          • 1970-01-01
          相关资源
          最近更新 更多