2011-06-26 383 views
2

我正在嘗試使用Microsoft Access數據庫的附件數據類型。 但我不知道如何使用它。如何使用Microsoft Access數據庫的附件數據類型?

我想使用.Net Windows窗體插入圖像到訪問數據庫。

在SQL Server 2008中,圖像數據類型和字節是兼容性的。 但我不知道如何將圖像插入到訪問數據庫。

是否需要更改像SQL Server的字節或可以直接插入到訪問數據庫。

回答

2
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); 
+0

我認爲圖像數據類型是從SQLServer數據類型 我已經知道,我想知道在Microsoft Access中的附件數據類型。 – Titan

+0

是的。我已經在MS訪問中創建了數據庫。 我也想使用OpenFileDialog,並使用該OpenFileDialog插入圖像到訪問數據庫..我可以這樣做... 在訪問數據庫中,我創建了一個數據類型附件列.. – Titan

+0

@Bunny - 對不起,我剛剛意識到你是什​​麼意思。我不認爲你可以通過OleDb提供程序使用附件數據類型。你需要進入'Microsoft.Office.Interop.Access.Dao',這並不好玩。任何你不能使用'OLE Object'類型的原因? –

1

下面是我用來從.net代碼中的OleDB連接將文件附件添加到帶有附件字段類型的Microsoft Access數據庫:

這個方法從我的表中附件字段名稱「Pic」中獲取你想要的Ordinal Position中的文件..你可以在附件字段中存儲很多文件,所以你必須指定你想要的文件......希望這有助於(我用這個作爲網頁的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; 

    } 
相關問題