从C#App在Access 2007 DB中插入并检索图像

问题描述 投票:0回答:2

我需要在具有OLE Object类型的列的访问数据库中插入图像我尝试使用以下代码,但在插入插入时给了我一个语法错误,但我确定表名和列名以及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,但也会引发相同的异常。

提前感谢

我需要在具有OLE对象类型列的访问数据库中插入图像,我尝试使用以下代码,但是在插入内容中给了我语法错误,但是我确定表名和...

c# visual-studio-2010 ms-access-2007
2个回答
2
投票

我认为Image是保留字,所以也许只是您应该使用[Image],或者更好的是,重命名该列。 (顺便说一句,您确定要在数据库中存储图像吗?)


1
投票

重命名列您应该使用[Image]

© www.soinside.com 2019 - 2024. All rights reserved.