簡體   English   中英

這有什么不對? 在我的代碼中(將圖像插入數據庫 - C#)

[英]What is wrong here? In my code (Inserting Image into Database - C#)

我試圖從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();
    }

當我運行代碼時,它向我顯示一個錯誤: Syntax error in INSERT INTO statement. 我的代碼中有什么問題? 我可以使用相同的查詢成功地將文本插入到數據庫的字段中。

Image是保留字,因此我假設您應該在SQL查詢中將此單詞放在引號或方括號中。

試試這個並將參數添加到其他列:

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

最好的祝福

嘗試在sql查詢中輸入img as @img:

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

編輯:也用' ;結束你的SQL查詢 ”。 我在上面的查詢中添加了它。

我相信你忘記在你的參數之前添加一個@ -sign:

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

應該

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

可能是某些輸入數據不是很好,導致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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM