【问题标题】:Error Inserting Image into Database c#将图像插入数据库c#时出错
【发布时间】:2018-01-07 12:53:29
【问题描述】:

错误 [HY000] [MySQL][ODBC5.3(a) 驱动程序][mysqlId-5.7.11] 列“图像” 不能为空。(这是我的问题)

这是我的插入按钮代码

OdbcConnection connection = new OdbcConnection("dsn=dsn_pos");
        MessageBox.Show(this.imagePath.Text);
        FileStream filestreme = new FileStream(imagePath.Text,System.IO.FileMode.Open,System.IO.FileAccess.Read);
        byte[] image = new byte[filestreme.Length];
        filestreme.Read(image, 0, Convert.ToInt32(filestreme.Length));
        filestreme.Close();




        connection.Open();
        string query = "INSERT INTO item_inventory (Images) VALUES (?)";
        OdbcCommand cmd = new OdbcCommand(query, connection);
        OdbcParameter prm = new OdbcParameter("@IMG",OdbcType.Binary,image.Length,ParameterDirection.Input,false,0,0,null,DataRowVersion.Current,image);
        cmd.Parameters.Add(prm);
        cmd.ExecuteNonQuery();
        connection.Close();

这是我的加载按钮代码

 OpenFileDialog dialog = new OpenFileDialog();
        dialog.Filter = "png files(*.png)|*.png|jpg files(*.jpg)|*.jpg|All files(*.*)|*.*";
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            string picPath = dialog.FileName.ToString();
            imagePath.Text = picPath;
            pictureBox2.ImageLocation = picPath;
        }

【问题讨论】:

  • Images 列的类型是什么?
  • 图片类型是 blob。
  • 为什么不使用 MySql 驱动程序? dev.mysql.com/doc/connector-net/en
  • 我们的项目使用ODBC MYsql
  • 谢谢@shawkyz1 我已经解决了..

标签: c# mysql


【解决方案1】:

mysql 驱动不支持命名参数,只支持有序。您需要将查询中的@IMG 更改为?。 担心你查询的应该是这样的:

INSERT INTO item_inventory (Images) VALUES (?)

【讨论】:

  • 是的 mysql 驱动程序正在工作.. 让我试试你的建议。
  • 谢谢。 ?标志是问题〜_〜为什么我需要使用?我能解释一下吗
  • 是的 SQL @ 和 odbc ?
  • 其实我不是很擅长术语,但是在实践中我知道这个问题。因此,很抱歉我无法给您一个解释的答案。
【解决方案2】:

这是答案字符串 query = "INSERT INTO item_inventory (Images) VALUES (?)";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-10
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多