从C#应用程序中插入和检索Access 2007 DB中的图像

问题描述:

我需要在访问数据库中插入一个图像类型为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,但也提出了相同的异常。

在此先感谢

我觉得Image是一个保留字,所以也许它只是你应该使用[Image],或者更好,重命名列。 (顺便说一下,你确定要将图像存储在数据库中吗?)

+0

感谢它现在的作品 – George