这有什么不对?在我的代码中(将图像插入数据库 - C#)

[英]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. I am using the following code:

我试图从C#winform在我的访问数据库中插入一个图像。我使用以下代码:

    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. What is wrong here in my code? I can successfully insert text to the fields of database by using the same query.

当我运行代码时,它向我显示一个错误:INSERT INTO语句中的语法错误。我的代码中有什么问题?我可以使用相同的查询成功地将文本插入到数据库的字段中。

6 个解决方案

#1


6  

Image is a reserved word, so I assume you should put this word in quotes or square brackets in the SQL query.

图像是保留字,因此我假设您应该在SQL查询中将此单词放在引号或方括号中。

#2


1  

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

最好的祝福

#3


0  

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

#4


0  

You try to set the value for parameter "@img", but your query only mentions "img", so instead you need to set the query text thus:

您尝试设置参数“@img”的值,但您的查询仅提到“img”,因此您需要设置查询文本:

cmd.CommandText = "INSERT INTO Table1 (Product, Manufacturer, Description, Price, Image) VALUES ('Column1', 'Column2', 'Column3', 'Column4', @img )";

#5


0  

may be some input data is not in good form, leading to Sql injection . 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.

可能是某些输入数据不是很好,导致Sql注入。所以我建议你这样试试。使用参数化查询。还尝试为列提供一些好的描述性名称。形象将是暧昧的。因为它将被视为关键字。

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

#6


0  

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 ';'. I added it in the query above.

编辑:也用';'结束你的SQL查询。我在上面的查询中添加了它。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2012/01/30/56588634da873c161f4075ee5822783c.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号