我寫的一個類,注意二進位檔案操作的部分不通用,需要根據具體情況去改
using System;using System.Data;using System.Configuration;using System.Data.OleDb;using System.Collections;using System.Windows.Forms;namespace AccessOperator{ public class AccessHelper { #region 屬性 private OleDbConnection conn; // private OleDbCommand cmd; #endregion #region 開啟Access /// <summary> /// 開啟串連 /// </summary> /// <param name="AccessPath">完整路徑,//</param> /// <param name="UserName">無使用者名稱,則參數為""</param> /// <param name="UserPWD">無密碼,則參數為""</param> public AccessHelper(string AccessPath, string UserName, string UserPWD) { try { //組成資料庫連接字串 string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + AccessPath + ";"; if (UserName != string.Empty && UserPWD != string.Empty) { ConnStr += "User Id=" + UserName + "; Password=" + UserPWD + ";"; } conn = new OleDbConnection(ConnStr); conn.Open(); if (conn.State != ConnectionState.Open) { MessageBox.Show("資料庫連接異常"); } } catch (Exception ex) { MessageBox.Show("資料庫連接異常,請確保Access檔案存在,使用者名稱、密碼正確"); } } #endregion /// <summary> /// 返回受影響的行數 /// </summary> /// <param name="cmdText">SQL 陳述式</param> /// <returns>返回受影響的函數</returns> public int ExecuteNonQuery(string cmdText) { OleDbCommand cmd = new OleDbCommand(); PrepareCommand(cmd, conn, cmdText); try { int count = cmd.ExecuteNonQuery(); return count; } catch { conn.Close(); throw; } } // 返回一個DataReader對象 public OleDbDataReader ExecuteReader(string cmdText) { //建立一個SqlCommand對象 OleDbCommand cmd = new OleDbCommand(); PrepareCommand(cmd, conn, cmdText); try { OleDbDataReader reader = cmd.ExecuteReader(); return reader; } catch { conn.Close(); throw; } } // 返回資料集 public DataSet ExecuteDataSet(string cmdText) { //建立一個SqlCommand對象,並對其進行初始化 OleDbCommand cmd = new OleDbCommand(); PrepareCommand(cmd, conn, cmdText); OleDbDataAdapter da = new OleDbDataAdapter(cmd); DataSet ds = new DataSet(); try { //填充ds da.Fill(ds); return ds; } catch { conn.Close(); throw; } } //返回結果集的第一列 public object ExecuteScalar(string cmdText) { OleDbCommand cmd = new OleDbCommand(); PrepareCommand(cmd, conn, cmdText); object val = cmd.ExecuteScalar(); return val; } // 執行SQL前的準備 private void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, string cmdText) { //判斷串連的狀態。如果是關閉狀態,則開啟 if (conn.State != ConnectionState.Open) conn.Open(); //cmd屬性賦值 cmd.Connection = conn; cmd.CommandText = cmdText; cmd.CommandType = CommandType.Text; //添加cmd需要的預存程序參數 } #region - 二進位檔案用,不操作檔案系統時可以刪除 - //二進位檔案的儲存 //表的結構:三列,Title、Description、nr,類型分別為, 文本 文本 Ole對象 public void SaveBinaryData(string strFileName, string strHD, string strZH, string strPic) { System.IO.FileStream fs = new System.IO.FileStream(strFileName, System.IO.FileMode.Open); System.IO.BinaryReader br = new System.IO.BinaryReader(fs); byte[] buffer = br.ReadBytes((int)fs.Length); OleDbCommand command = new OleDbCommand("INSERT INTO 圖形繪製知識庫(巷道類型,支護類型,圖形類型,繪圖知識)" + "VALUES (@HDStyle,@ZHMode,@PicType,@nr)", conn); command.Parameters.AddWithValue("@HDStyle", strHD); command.Parameters.AddWithValue("@ZHMode", strZH); command.Parameters.AddWithValue("@PicType", strPic); command.Parameters.AddWithValue("@nr", buffer); command.ExecuteNonQuery(); br.Close(); fs.Close(); MessageBox.Show("儲存完畢。"); OleDbCommand cmd = new OleDbCommand(); } /// <summary> /// 匯出位元據 /// </summary> /// <param name="strFileName">檔案名稱,表中第一個欄位</param> /// <param name="strSavePath">檔案儲存路徑,全路徑</param> public void ExportBinaryData(string strSavePath, string strHD, string strZH, string strPic) { //構建資料庫連接,SQL語句,建立參數 OleDbCommand command = new OleDbCommand("select top 1 * from 圖形繪製知識庫 where 巷道類型 = \"" + strHD + "\" and 支護類型 = \"" + strZH + "\" and 圖形類型 = \"" + strPic + "\"", conn); OleDbDataReader dr = command.ExecuteReader(); byte[] buff = null; if (dr.Read()) { buff = (byte[])dr["繪圖知識"]; } if (System.IO.File.Exists(strSavePath)) System.IO.File.Delete(strSavePath); System.IO.FileStream stream = new System.IO.FileStream(strSavePath, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(stream); bw.Write(buff); bw.Close(); stream.Close(); MessageBox.Show("產生完畢。"); } #endregion // 關閉串連、釋放資源 public void CloseConn() { if (conn.State == ConnectionState.Open) { conn.Close(); conn.Dispose(); } } }}
下面是網友的一個例子,主要功能是把Word文檔存入Access,需要的時候再匯出來
網址:http://social.microsoft.com/Forums/zh-CN/visualcshartzhchs/thread/d4a419b1-c51b-4e87-a151-9ca858897570
完整代碼:在Office 2007,W2k3 SP2,VS2008 SP1下測試通過using System;using System.ComponentModel;using System.Data;using System.Data.OleDb;using System.Drawing;using System.IO;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication7{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string strCnn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\c\WindowsFormsApplication7\aspxWeb.mdb;Persist Security Info=True"; private void button2_Click(object sender, EventArgs e) { if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { this.textBox1.Text = this.openFileDialog1.FileName; } } private void button1_Click(object sender, EventArgs e) { if (this.textBox1.Text.Equals(String.Empty)) { MessageBox.Show("先選擇檔案。"); return; } FileStream fs = new FileStream(this.textBox1.Text, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] buffer = br.ReadBytes((int)fs.Length); OleDbConnection myConnection = new OleDbConnection(strCnn); OleDbCommand command = new OleDbCommand("INSERT INTO TestTable (Title,Description,nr)" + "VALUES (@Title,@Description,@nr)", myConnection); command.Parameters.AddWithValue("@Title", "a"); command.Parameters.AddWithValue("@Description", "mengxianhui@dotnet.aspx.cc"); command.Parameters.AddWithValue("@nr", buffer); //開啟串連,執行查詢 myConnection.Open(); command.ExecuteNonQuery(); myConnection.Close(); br.Close(); fs.Close(); MessageBox.Show("儲存完畢。"); } private void button3_Click(object sender, EventArgs e) { //構建資料庫連接,SQL語句,建立參數 OleDbConnection myConnection = new OleDbConnection(strCnn); myConnection.Open(); OleDbCommand command = new OleDbCommand("select top 1 * from TestTable Order By id DESC", myConnection); OleDbDataReader dr = command.ExecuteReader(); byte[] buff = null; if (dr.Read()) { buff = (byte[])dr["nr"]; } String p = Application.ExecutablePath; p = p.Substring(0,p.LastIndexOf("\\")); p += "\\m.doc"; this.textBox2.Text = p ; if (File.Exists(p)) File.Delete(p); myConnection.Close(); System.IO.FileStream stream = new System.IO.FileStream(p, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(stream); bw.Write(buff); bw.Close(); stream.Close(); MessageBox.Show("產生完畢。"); } }}