简体   繁体   English

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

[英]Error Inserting Image into Database c#

ERROR [HY000] [MySQL][ODBC5.3(a) Driver][mysqlId-5.7.11] Column 'Images' cannot be null. 错误[HY000] [MySQL] [ODBC5.3(a)驱动程序] [mysqlId-5.7.11]列“ Images”不能为空。 (This is my Problem) (这是我的问题)

This is my inserting button code 这是我的插入按钮代码

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();

This one is my loading button code 这是我的加载按钮代码

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

The mysql driver works otherwise, it doesn't support named parameters, only orderd. mysql驱动程序可以用其他方式工作,它不支持命名参数,只能排序。 You need to change your @IMG in query to ?. 您需要将查询中的@IMG更改为?。 Afret that you query should be looks like: 您查询的清单看起来应该像这样:

INSERT INTO item_inventory (Images) VALUES (?)

这是答案字符串查询=“ INSERT INTO item_inventory(Images)VALUES(?)”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM