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