[英]What is wrong here? In my code (Inserting Image into Database - C#)
I am trying to insert an image in my access database from C# winform. 我试图从C#winform在我的访问数据库中插入一个图像。 I am using the following code:
我使用以下代码:
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb");
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "INSERT INTO Table1 (Product, Manufacturer, Description, Price, Image) VALUES ('Column1', 'Column2', 'Column3', 'Column4', @img)";
byte[] yourPhoto = imageToByteArray(pictureBox1.Image);
cmd.Parameters.AddWithValue("@img", yourPhoto);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public byte[] imageToByteArray(System.Drawing.Image iImage)
{
MemoryStream mMemoryStream = new MemoryStream();
iImage.Save(mMemoryStream, System.Drawing.Imaging.ImageFormat.Png);
return mMemoryStream.ToArray();
}
When I run the code it show me an error: Syntax error in INSERT INTO statement.
当我运行代码时,它向我显示一个错误:
Syntax error in INSERT INTO statement.
What is wrong here in my code? 我的代码中有什么问题? I can successfully insert text to the fields of database by using the same query.
我可以使用相同的查询成功地将文本插入到数据库的字段中。
Image
是保留字,因此我假设您应该在SQL查询中将此单词放在引号或方括号中。
Try this and add parameters to your other column: 试试这个并将参数添加到其他列:
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb");
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "INSERT INTO Table1 (Product, Manufacturer, Description, Price,[Image]) VALUES (@Product,@Manufacturer,@Description,@Price,@Image)";
byte[] yourPhoto = imageToByteArray(pictureBox1.Image);
cmd.Parameters.AddWithValue("@Product", "yourProductValue");
cmd.Parameters.AddWithValue("@Manufacturer","yourManufacturerValue");
cmd.Parameters.AddWithValue("@Description", "yourDescriptionValue");
cmd.Parameters.AddWithValue("@Price","yourPriceValue");
cmd.Parameters.AddWithValue("@Image", yourPhoto);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public byte[] imageToByteArray(System.Drawing.Image iImage)
{
MemoryStream mMemoryStream = new MemoryStream();
iImage.Save(mMemoryStream, System.Drawing.Imaging.ImageFormat.Png);
return mMemoryStream.ToArray();
}
Best Regards 最好的祝福
Try entering img as @img in your sql query: 尝试在sql查询中输入img as @img:
cmd.CommandText = "INSERT INTO Table1 (Product, Manufacturer, Description, Price, Image) VALUES ('Column1', 'Column2', 'Column3', 'Column4', @img);";
EDIT: also end your sql query with a ' ; 编辑:也用' ;结束你的SQL查询 '.
”。 I added it in the query above.
我在上面的查询中添加了它。
I believe you forget to add an @-sign before your parameter: 我相信你忘记在你的参数之前添加一个@ -sign:
cmd.CommandText = "INSERT INTO Table1 (Product, Manufacturer, Description, Price, Image) VALUES ('Column1', 'Column2', 'Column3', 'Column4', img)";
should be 应该
cmd.CommandText = "INSERT INTO Table1 (Product, Manufacturer, Description, Price, Image) VALUES ('Column1', 'Column2', 'Column3', 'Column4', @img)";
您尝试设置参数“@img”的值,但您的查询仅提到“img”,因此您需要设置查询文本:
cmd.CommandText = "INSERT INTO Table1 (Product, Manufacturer, Description, Price, Image) VALUES ('Column1', 'Column2', 'Column3', 'Column4', @img )";
may be some input data is not in good form, leading to Sql injection . 可能是某些输入数据不是很好,导致Sql注入。 so i suggest you to try like this .
所以我建议你这样试试。 with parametrized query.
使用参数化查询。 also try to give some good descriptive name to your columns.
还尝试为您的列提供一些好的描述性名称。 Image will be ambigious.
形象将是暧昧的。 as it will be treated as keyword.
因为它将被视为关键字。
cmd.CommandText = "INSERT INTO Table1 (Product, Manufacturer, Description, Price, [Image]) VALUES (@column1, @column2, @column3, @column4, @img )";
cmd.Parameters.AddWithValue("@column1", yourval);
cmd.Parameters.AddWithValue("@column2", yourval);
cmd.Parameters.AddWithValue("@column3", yourval);
cmd.Parameters.AddWithValue("@column4", yourval);
cmd.Parameters.AddWithValue("@img", yourPhoto);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.