如何使用 Microsoft Access 数据库的附件数据类型?

Posted

技术标签:

【中文标题】如何使用 Microsoft Access 数据库的附件数据类型?【英文标题】:How to use Microsoft Access Database's Attachment Data Type? 【发布时间】:2011-06-26 04:48:54 【问题描述】:

我正在尝试使用 Microsoft access 数据库的附件数据类型。 但我不知道怎么用。

我想使用 .Net Windows Form 将图像插入到 access 数据库中。

在 SQL Server 2008 中,图像数据类型和字节与其兼容。 但我不知道如何将图像插入到访问数据库中。

是否需要像SQL Server那样改变字节或者可以直接插入到access数据库中。

【问题讨论】:

【参考方案1】:
using (var connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\BlankDatabase.mdb"))

    connection.Open();

    // Create table
    using (var command = connection.CreateCommand())
    
        command.CommandText = @"
            CREATE TABLE FileTable (
                FileName VARCHAR(255),
                File IMAGE)
            ";
        command.ExecuteNonQuery();
    

    var imageContent = File.ReadAllBytes(@"C:\logo.png");

    // upload image to the table
    using (var command = connection.CreateCommand())
    
        command.CommandText = @"
            INSERT INTO FileTable (FileName, File)
            VALUES (@FileName, @File)
            ";
        command.Parameters.AddWithValue("@FileName", "Logo");
        command.Parameters.AddWithValue("@File", imageContent);
        command.ExecuteNonQuery();
    

    // retreive image from the table
    using (var command = connection.CreateCommand())
    
        command.CommandText = @"
            SELECT File
            FROM FileTable
            WHERE FileName = 'Logo'
            ";
        var readImageContent = (byte[])command.ExecuteScalar();
        File.WriteAllBytes(@"C:\logo1.png", readImageContent);
    

    // alter image from the table
    using (var command = connection.CreateCommand())
    
        command.CommandText = @"
            UPDATE FileTable
            SET File = @File
            WHERE FileName = 'Logo'
            ";
        command.Parameters.AddWithValue("@File", imageContent);
        command.ExecuteNonQuery();
    

    // delete image from the table
    using (var command = connection.CreateCommand())
    
        command.CommandText = @"
            DELETE FROM FileTable
            WHERE FileName = 'Logo'
            ";
        command.ExecuteNonQuery();
    

在此代码中,BlankDatabase.mdb 是一个空的 MS Access 数据库文件。

[编辑]

当您将图像保存到数据库时,如上所示,您可以检索图像字节,如上所示:

您可以像这样从图像字节构造Image

var imageConverter = new ImageConverter();
pictureBox1.Image = (Image)imageConverter.ConvertFrom(fileContent);

【讨论】:

我认为 Image 数据类型是 From SQLServer DataType 我已经知道了。我想知道 Microsoft Access 中的 Attachment DataType。 是的。我已经在 MS Access 中创建了数据库。我还想使用 OpenFileDialog 并使用该 OpenFileDialog 将图像插入到 access 数据库中。我该怎么做...在 access 数据库中,我创建了一个数据类型为 Attachment 的列。 @Bunny - 抱歉,我才明白你的意思。我认为您不能通过 OleDb 提供程序使用附件数据类型。您需要与Microsoft.Office.Interop.Access.Dao 互操作,这并不好玩。有什么理由不能使用OLE Object 类型? 我还想展示我在 C#.net windows 窗体中插入到 PictureBox 中的图像。我该怎么做? @Titan - 如果我的回答没有帮助,请告诉我。【参考方案2】:

这是我用来将文件附件从 .net 代码中的 OleDB 连接获取到带有附件字段类型的 microsoft access 数据库的方法:

此方法从我的表格中的附件字段名称“Pic”中获取您想要的Ordinal Position文件..您可以在附件字段中存储许多文件,因此您必须指定您想要的文件..希望这有帮助(我使用它作为 web url 从访问数据库中的附件字段中获取图像,但 COM 调用在你的 winform 应用程序中将是相同的)..祝你好运

        try
            
                  //You get your file in a byteArray fileType is just the ordinal file position in the fileattachment field..ex. 1, 2, 3 (shown in the access listbox)
                Response.BinaryWrite(GetPicField(productID, fileType));
                Response.ContentType = "image/bmp";
            

            catch 
            
                //need to get missing product photo image here as well N/A
                Response.BinaryWrite(GetNA_Image());
                Response.ContentType = "image/bmp";
            

    //getting from Database
    private byte[] GetPicField(string productID,int fileToShow)
    
        DBEngine dbe = new DBEngine();
        Database db;
        Recordset rs;

        byte[] byteArray = null;

        dbe = new DBEngine();
        db = dbe.OpenDatabase(Application["DB_FileName"].ToString());
        rs = db.OpenRecordset("SELECT PIC FROM PRODUCT WHERE PRODUCTID = " + productID, RecordsetTypeEnum.dbOpenForwardOnly, 0, LockTypeEnum.dbPessimistic);

        if (rs.RecordCount > 0)
        

            Recordset rs2 = (Recordset2)rs.Fields["Pic"].Value;
            int i = 1;

            while (i < fileToShow)
            
                rs2.MoveNext();
                i++;
            

          //get the thubmnail
           Field2 f2 = (Field2)rs2.Fields["FileData"]; //0 is first pic

            byteArray = f2.GetChunk(20, f2.FieldSize - 20);

            System.Runtime.InteropServices.Marshal.ReleaseComObject(f2);
            rs2.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs2);
            f2 = null;
            rs2 = null;

        

        rs.Close();
        db.Close();

        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(dbe);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(db);

        rs = null;
        db = null;
        dbe = null;

        return byteArray;

    

【讨论】:

以上是关于如何使用 Microsoft Access 数据库的附件数据类型?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用空字符串更新 Microsoft Access 数据库?

如何在 java 中访问 microsoft access 数据库 (1997)

如何在 C# 中创建 Microsoft Access 数据库? [复制]

如何将 sql server 链接到 Microsoft Access?

如何使用 JDBC 和 Microsoft Access 获取自动递增的密钥?

如何通过 JDBC 将值插入 Microsoft Access 数据库?