gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:19:07 25 4
gpt4 key购买 nike

错误 [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;
}

最佳答案

mysql 驱动程序以其他方式工作,它不支持命名参数,仅支持 orderd。您需要将查询中的@IMG 更改为?。担心您查询应该是这样的:

INSERT INTO item_inventory (Images) VALUES (?)

关于c# - 将图像插入数据库时​​出错 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48137283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com