将图像插入数据库c时出错#

laik7k3q  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(326)

错误[hy000][mysql][odbc5.3(a)driver][mysqlid-5.7.11]列“images”不能为空(这是我的问题)
这是我插入按钮的代码

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

sh7euo9m1#

这是回答字符串query=“insert into item_inventory(images)values(?)”;

rvpgvaaj

rvpgvaaj2#

mysql驱动程序在其他情况下工作,它不支持命名参数,只支持orderd。您需要将查询中的@img更改为?。您查询的afret如下所示:

INSERT INTO item_inventory (Images) VALUES (?)

相关问题