【问题标题】:C# insert image to database with classC# 使用类将图像插入数据库
【发布时间】:2020-06-15 15:19:41
【问题描述】:

我想用类文件将我的图像保存到数据库中,但是当我尝试上传时,cmd.ExecuteNonQuery(); 出现错误。我对图像列使用 bin 数据类型。 这是我的课程代码

    public int addclubs(string clubname, string president, string presidentID, string vicepresident, string vicepresidentID, string secretary, string secretaryID,
                          string clubdesc, DateTime established, Image images)
    {
        int status = 0;

        string insertSQL = "INSERT INTO Clubs(club_name,president,presidentID,vice_president,vice_presidentID,secretary,secretaryID,club_desc,established,image)" +
            "Values(@club,@prs,@prsID,@vc,@vcID,@sec,@secID,@clubdesc,@esb,@img)";
        Connect();
        SqlCommand cmd = new SqlCommand(insertSQL, conn);
        cmd.Parameters.AddWithValue("@club", clubname);
        cmd.Parameters.AddWithValue("@prs", president);
        cmd.Parameters.AddWithValue("@prsID", presidentID);
        cmd.Parameters.AddWithValue("@vc", vicepresident);
        cmd.Parameters.AddWithValue("@vcID", vicepresidentID);
        cmd.Parameters.AddWithValue("@sec", secretary);
        cmd.Parameters.AddWithValue("@secID", secretaryID);
        cmd.Parameters.AddWithValue("@clubdesc", clubdesc);
        cmd.Parameters.AddWithValue("@esb", established);
        cmd.Parameters.AddWithValue("@img", images);
        cmd.ExecuteNonQuery();

        return status;
    } 

这里是private void btnRegRegister_Click(object sender, EventArgs e)

                byte[] imagebt = null;
            FileStream fst = new FileStream(picUploadReg.ImageLocation, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fst);
            imagebt = br.ReadBytes((int)fst.Length);
            DateTime established = DateTime.Parse(dtpClubReg.Value.ToString());
            ctrls.addclubs(txtNameClubReg.Text, txtPresidentReg.Text, txtPresidentIDReg.Text, txtViceReg.Text, txtViceIDReg.Text, txtSecReg.Text, txtSecIDReg.Text, txtClubDescReg.Text, established,picUploadReg.Image);

【问题讨论】:

  • 你得到什么错误?
  • Image 是一个用于操作图像的类,而不是图像的数据。对于数据库,图像只是一个 blob - 一堆字节。在 C# 中,您需要使用这些字节传递 byte[]Stream
  • @itsme86 此异常最初是在此调用堆栈中引发的:[外部代码] IOOP_Assignment.MyClass.controllers.addclubs(string, string, string, string, string, string, string, string, System. Controllers.cs 中的 DateTime, System.Drawing.Image) IOOP_Assignment.Register.btnRegRegister_Click(object, System.EventArgs) Register.cs [外部代码] Program.cs 中的 IOOP_Assignment.Program.Main()
  • @YogiSeptiargy 但有什么例外?!
  • 我怀疑您将 2000-2005 年使用的过时 image 类型名称误认为 .NET Image 类型?他们根本没有关系。 SQL Server 有一个 varbinary(max) 类型,15 多年前称为 image。事实上that name is marked for deletion in the future。不要使用这些名称,并更改当前字段以使用正确的名称。无论如何,这只是一个存储字节的 BLOB。

标签: c# sql database forms winforms


【解决方案1】:

我建议不要将图像本身保存在您的表格中。您可能希望在应用程序内的文件夹中保存或创建图像的副本,并引用表中的图像。根据我的经验,这更容易,更有效。 当我在手机上时,我很乐意在回家后向您提供代码。

【讨论】:

    猜你喜欢
    • 2017-05-09
    • 2013-05-10
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    相关资源
    最近更新 更多