2011-06-26 19 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

(私はアクセスのデータベースに添付ファイルフィールドから画像を取るためにウェブのURLとしてこれを使用しますが、COMの呼び出しは、あなたのWinフォームアプリケーションで同じになります)ことができます私はそのイメージ・データ型を考えますSQLServerデータ型からです 私はすでにthat.iがMicrosoft AccessのAttachment DataTypeを知りたいと知っています。 – Titan

+0

はい。私は既にMSアクセスでデータベースを作成しています。 また、OpenFileDialogを使用して、そのOpenFileDialogを使用してアクセスデータベースに画像を挿入したいと考えていますか... アクセスデータベースで、データ型添付付きの列を作成しました.. – Titan

+0

@Bunny - 申し訳ありません。どういう意味ですか。 OleDbプロバイダ経由でAttachment Data Typeを使用できるとは思いません。あなたは 'Microsoft.Office.Interop.Access.Dao'と相互作用する必要があります。これは楽しいものではありません。 'OLE Object'型を使用できない理由は何ですか? –

1

添付ファイルの種類があるMicrosoft Accessデータベースに.netコードのOleDB接続から添付ファイルを取得するために使用します。

このメソッドは、テーブル内の添付ファイル名 "Pic"から序数の位置に必要なファイルを取得します。添付ファイルのフィールドに多くのファイルを格納できるため、必要なファイルを指定する必要があります。希望これは...幸運

 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; 

    } 
関連する問題