开发者

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

开发者 https://www.devze.com 2023-03-30 22:08 出处:网络
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

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


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?)


rename the column you should use [Image]

0

精彩评论

暂无评论...
验证码 换一张
取 消