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

懵懂的女人 提交于 2019-12-11 15:47:29

问题


I need to insert an image in access database that has a column of type OLE Object I tried to use the following code but it gives me a Syntax Error in insert staetment but I am sure of table name and column name and the imageContent value is not null

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

Also I tried to use ? instead of @photo but also the same exception raised.

Thanks in advance


回答1:


I think Image is a reserved word, so maybe it's just that you should use [Image] or, better, rename the column. (BTW, are you sure you want to store images in your database?)



来源:https://stackoverflow.com/questions/7211367/insert-and-retrieve-image-in-access-2007-db-from-c-sharp-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!