1using System;
2using System.Data.OleDb;
3using System.Data;
4using System.Windows.Forms;
5namespace DBAccess
6...{
7 /**//// <summary>
8 /// 對Access2003數據庫的訪問、數據獲取 9 /// </summary>
10 public class Access2003
11 ...{
12 static OleDbTransaction myts = null; // 事務
13 static OleDbConnection MyConnection = null; // 數據庫連接
14
15 數據庫打開、關閉#region 數據庫打開、關閉
16 /**//// <summary>
17 /// 打開數據庫
18 /// </summary>
19 public static void Open()
20 ...{
21 MyConnection = new OleDbConnection( GetConnectionStr() );
22 MyConnection.Open();
23 }
24
25 /**//// <summary>
26 /// 關閉數據庫
27 /// </summary>
28 public static void Close()
29 ...{
30 MyConnection.Close();
31 }
32 #endregion
33
34 開始、提交、回滾一個數據庫事務#region 開始、提交、回滾一個數據庫事務
35
36 /**//// <summary>
37 /// 開始一個事務
38 /// </summary>
39 public static void BeginTransaction()
40 ...{
41 myts = MyConnection.BeginTransaction();
42 }
43
44 /**//// <summary>
45 /// 提交一個事務
46 /// </summary>
47 public static void CommitTransaction()
48 ...{
49 if( myts == null )
50 ...{
51 return;
52 }
53 myts.Commit();
54 }
55
56 /**//// <summary>
57 /// 回滾一個事務
58 /// </summary>
59 public static void RollbackTransaction()
60 ...{
61 if( myts == null )
62 ...{
63 return;
64 }
65 myts.Rollback();
66 }
67 #endregion
68
69 獲取數據庫連接字符串#region 獲取數據庫連接字符串
70
71 /**//// <summary>
72 /// 獲取連接字符串
73 /// </summary>s
74 /// <returns>返回一個字符串,代表了連接信息</returns>
75 private static string GetConnectionStr()
76 ...{
77 string str = string.Empty;
78 str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ Application.StartupPath +"//Library.mdb;";
79 return str;
80 }
81 #endregion
82
83 查找數據#region 查找數據
84
85 /**//// <summary>
86 /// 執行一段SQL語句,查找相關數據
87 /// </summary>
88 /// <param name="sql">要執行的SQL語句</param>
89 /// <returns>一個DataTable實例</returns>
90 public static DataTable GetDataTable( string sql )
91 ...{
92 DataTable dt = new DataTable();
93 OleDbCommand odc = new OleDbCommand( sql, MyConnection );
94 OleDbDataAdapter oda = new OleDbDataAdapter( odc);
95 try
96 ...{
97 MyConnection.Open();
98 oda.Fill( dt );
99 return dt;
100 }
101 catch( Exception e )
102 ...{
103 MessageBox.Show( "出錯,原因是:" + e.Message, "出錯" );
104 return null;
105 }
106 finally
107 ...{
108 MyConnection.Close();
109 odc.Dispose();
110 oda.Dispose();
111 }
112 }
113 #endregion
114
115 執行SQL命令#region 執行SQL命令
116
117 /**//// <summary>
118 /// 執行一段SQL語句
119 /// </summary>
120 /// <param name="sql">要執行的SQL語句</param>
121 public static int DoCommand( string sql )
122 ...{
123 OleDbCommand odc = new OleDbCommand( sql, MyConnection );
124 try
125 ...{
126 odc.Transaction = myts;
127 odc.ExecuteNonQuery();
128 return 1;
129 }
130 catch( Exception e )
131 ...{
132 throw new Exception( "出錯,原因是:" + e.Message );
133 }
134 finally
135 ...{
136 odc.Dispose();
137 }
138 }
139#endregion
140 }
141}
142
2using System.Data.OleDb;
3using System.Data;
4using System.Windows.Forms;
5namespace DBAccess
6...{
7 /**//// <summary>
8 /// 對Access2003數據庫的訪問、數據獲取 9 /// </summary>
10 public class Access2003
11 ...{
12 static OleDbTransaction myts = null; // 事務
13 static OleDbConnection MyConnection = null; // 數據庫連接
14
15 數據庫打開、關閉#region 數據庫打開、關閉
16 /**//// <summary>
17 /// 打開數據庫
18 /// </summary>
19 public static void Open()
20 ...{
21 MyConnection = new OleDbConnection( GetConnectionStr() );
22 MyConnection.Open();
23 }
24
25 /**//// <summary>
26 /// 關閉數據庫
27 /// </summary>
28 public static void Close()
29 ...{
30 MyConnection.Close();
31 }
32 #endregion
33
34 開始、提交、回滾一個數據庫事務#region 開始、提交、回滾一個數據庫事務
35
36 /**//// <summary>
37 /// 開始一個事務
38 /// </summary>
39 public static void BeginTransaction()
40 ...{
41 myts = MyConnection.BeginTransaction();
42 }
43
44 /**//// <summary>
45 /// 提交一個事務
46 /// </summary>
47 public static void CommitTransaction()
48 ...{
49 if( myts == null )
50 ...{
51 return;
52 }
53 myts.Commit();
54 }
55
56 /**//// <summary>
57 /// 回滾一個事務
58 /// </summary>
59 public static void RollbackTransaction()
60 ...{
61 if( myts == null )
62 ...{
63 return;
64 }
65 myts.Rollback();
66 }
67 #endregion
68
69 獲取數據庫連接字符串#region 獲取數據庫連接字符串
70
71 /**//// <summary>
72 /// 獲取連接字符串
73 /// </summary>s
74 /// <returns>返回一個字符串,代表了連接信息</returns>
75 private static string GetConnectionStr()
76 ...{
77 string str = string.Empty;
78 str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ Application.StartupPath +"//Library.mdb;";
79 return str;
80 }
81 #endregion
82
83 查找數據#region 查找數據
84
85 /**//// <summary>
86 /// 執行一段SQL語句,查找相關數據
87 /// </summary>
88 /// <param name="sql">要執行的SQL語句</param>
89 /// <returns>一個DataTable實例</returns>
90 public static DataTable GetDataTable( string sql )
91 ...{
92 DataTable dt = new DataTable();
93 OleDbCommand odc = new OleDbCommand( sql, MyConnection );
94 OleDbDataAdapter oda = new OleDbDataAdapter( odc);
95 try
96 ...{
97 MyConnection.Open();
98 oda.Fill( dt );
99 return dt;
100 }
101 catch( Exception e )
102 ...{
103 MessageBox.Show( "出錯,原因是:" + e.Message, "出錯" );
104 return null;
105 }
106 finally
107 ...{
108 MyConnection.Close();
109 odc.Dispose();
110 oda.Dispose();
111 }
112 }
113 #endregion
114
115 執行SQL命令#region 執行SQL命令
116
117 /**//// <summary>
118 /// 執行一段SQL語句
119 /// </summary>
120 /// <param name="sql">要執行的SQL語句</param>
121 public static int DoCommand( string sql )
122 ...{
123 OleDbCommand odc = new OleDbCommand( sql, MyConnection );
124 try
125 ...{
126 odc.Transaction = myts;
127 odc.ExecuteNonQuery();
128 return 1;
129 }
130 catch( Exception e )
131 ...{
132 throw new Exception( "出錯,原因是:" + e.Message );
133 }
134 finally
135 ...{
136 odc.Dispose();
137 }
138 }
139#endregion
140 }
141}
142