2011-08-26 80 views
0

我需要在訪問數據庫中插入一個圖像類型爲OLE的對象 我試圖使用下面的代碼,但它在插入問題時給了我一個語法錯誤但我確信表名和列名以及imageContent值不爲空從C#應用程序中插入和檢索Access 2007 DB中的圖像

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,但也提出了相同的異常。

在此先感謝

回答

2

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

+0

感謝它現在的作品 – George

相關問題