簡體   English   中英

從 C# 應用程序在 Access 2007 DB 中插入和檢索圖像

[英]Insert and retrieve Image in Access 2007 DB from C# App

我需要在具有 OLE 對象類型列的訪問數據庫中插入一個圖像我嘗試使用以下代碼,但它在插入語句中給了我一個語法錯誤,但我確定表名和列名,而 imageContent 值不是無效的

    System.Data.OleDb.OleDbConnection MyConnection;
    private void Insert_Customer_Load(object sender, EventArgs e)
    {
        string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Customer.accdb;Persist Security Info=True;Jet OLEDB:Database Password=123";
        MyConnection = new System.Data.OleDb.OleDbConnection(strConn);
    }

    private void InsertBtn_Click(object sender, EventArgs e)
    {
        System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
        string sql = null;

        MyConnection.Open();
        myCommand.Connection = MyConnection;
        byte[] imageContent = File.ReadAllBytes(imagePathTxt.Text);
        sql = "INSERT INTO [Customer_Data] (Image) VALUES (@photo)";
        myCommand.CommandText = sql;
        OleDbParameter ph = new OleDbParameter("@photo", OleDbType.VarBinary);
        ph.Value = imageContent;
        ph.Size = imageContent.Length;
        myCommand.Parameters.Add(ph);
        myCommand.ExecuteNonQuery();
        MyConnection.Close();
    }

我也嘗試使用 ? 而不是@photo,但也引發了相同的異常。

提前致謝

我認為Image是一個保留字,所以也許您應該使用[Image]或者更好地重命名該列。 (順便說一句,您確定要將圖像存儲在數據庫中嗎?)

重命名您應該使用的列 [Image]

暫無
暫無

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

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