C# Access中OLE對象的操作


有時候需要大數據的存取時,如圖片,需要用到ole對象的操作。

首先,在默認文件中,添加兩個名空間

using System.Data.OleDb;
using System.IO;

一個用於數據庫操作,一個用於二進制文件操作

 

在Access中新建數據庫Database1.mdb, 完整文件路徑D:\Documents\Database1.mdb

 

寫入OLE對象數據

     打開文件,選擇一個大的圖像文件,然后存入數據庫。

代碼如下:

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                Byte[] buff = new Byte[fs.Length];
                BinaryReader rd = new BinaryReader(fs);
                rd.Read(buff, 0, Convert.ToInt32(fs.Length));

                OleDbCommand command = new OleDbCommand("INSERT INTO 表1 ([object], path) VALUES(@object, @path)", conn);
                command.Parameters.Add("@object", OleDbType.Binary, buff.Length).Value = buff;
                command.Parameters.Add("@path", OleDbType.Char, 255).Value = openFileDialog1.FileName;
                command.ExecuteNonQuery();
                rd.Close();
                fs.Close();

            }

        }

 

讀出OLE對象數據按鈕

      將數據庫里存進的圖片文件一個個讀出來,另存為臨時文件testfile*.png(測試用的文件都是png圖片)

代碼如下:

        private void button2_Click(object sender, EventArgs e)
        {
            OleDbCommand command = new OleDbCommand("SELECT [object], path FROM 表1", conn);
            OleDbDataReader dr = command.ExecuteReader();
            FileStream fs;
            BinaryWriter writer;
            int bufferSize = 100;
            byte[] outByte = new byte[bufferSize];
            int i=0;
            while (dr.Read())
            {
                string filename = "D:\\documents\\testfile"+ i.ToString() + ".png";
                fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
                writer = new BinaryWriter(fs);
                int startIndex = 0;
                long retval = dr.GetBytes(0, startIndex, outByte, 0, bufferSize);
                while (retval == bufferSize)
                {
                    writer.Write(outByte);
                    writer.Flush();

                    startIndex += bufferSize;
                    retval = dr.GetBytes(0, startIndex, outByte, 0, bufferSize);
                }

                writer.Write(outByte, 0, (int)retval - 1);
                writer.Flush();

                writer.Close();
                fs.Close();

                i++;

            }
            dr.Close();
        }

 

此外,注意使用數據庫前,先要打開數據庫,用完關閉。

相關代碼片段1:

        OleDbConnection conn;
        public Form1()
        {
            InitializeComponent();
            conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=d:\Documents\Database1.mdb";
            conn.Open();
        }

 

片段2:

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            conn.Close();
        }

 

至此,OLE對象存取學習實踐完畢,代碼也已完整。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM