您的位置:首页 > 其它

ADO.net学习总结三

2009-09-08 18:55 267 查看
2.OLE DB .Net 提供者

using System.Data.OleDb

访问Microsoft Acess,第三方数据库和非关系统数据

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Data;
5 using System.Data.OleDb;
6
7 namespace DevSDK.Data
8 {
9 public class OleDb
10 {
11 private string _ConnectionString = "";
12
13 private string _CommandText = "";
14
15 private OleDbConnection Conn = null;
16
17 private DataSet _DSAFDataSet = null;
18 /// <summary>
19 /// 获取或设置数据库连接字串
20 /// </summary>
21 public string ConnectionString { get { return this._ConnectionString; } set { this._ConnectionString = value; } }
22 /// <summary>
23 /// 获取或设置SQL语句
24 /// </summary>
25 public string CommandText { get { return this._CommandText; } set { this._CommandText = value; } }
26
27 public OleDb()
28 {
29 this._DSAFDataSet = new DataSet();
30 }
31 /// <summary>
32 /// 执行 SQL 语句,并把执行结果集填充到一个DataSet容器中
33 /// </summary>
34 /// <returns></returns>
35 public DataSet Execute()
36 {
37 if (string.IsNullOrEmpty(_ConnectionString)) { throw (new Exception("无传入数据连接字串")); }
38
39 if (string.IsNullOrEmpty(_CommandText)) { throw (new Exception("无传入SQL语句")); }
40
41 if (!DSAFConnectionTest()) { throw (new Exception("尝试数据库连接失败")); }
42
43 try
44 {
45 Conn = new OleDbConnection(); Conn.ConnectionString = _ConnectionString;
46
47 OleDbDataAdapter OleDbDa = new OleDbDataAdapter();
48
49 OleDbDa.SelectCommand.CommandText = _CommandText; OleDbDa.SelectCommand.Connection = Conn;
50
51 OleDbDa.Fill(_DSAFDataSet); OleDbDa.Dispose();
52 }
53 catch (OleDbException OleDbEx) { throw OleDbEx; }
54 catch (Exception ex) { throw ex; }
55
56 return _DSAFDataSet;
57 }
58 /// <summary>
59 /// 执行 SQL 语句,并返回受影响的行数
60 /// </summary>
61 /// <returns></returns>
62 public int ExecuteNonQuery()
63 {
64 if (string.IsNullOrEmpty(_ConnectionString)) { throw (new Exception("无传入数据连接字串")); }
65
66 if (string.IsNullOrEmpty(_CommandText)) { throw (new Exception("无传入SQL语句")); }
67
68 if (!DSAFConnectionTest()) { throw (new Exception("尝试数据库连接失败")); }
69
70 int iQuery = 0; OleDbTransaction Transaction = null;
71
72 try
73 {
74 OleDbCommand Comm = new OleDbCommand(); Conn = new OleDbConnection();
75
76 Conn.ConnectionString = _ConnectionString; Comm.Connection = Conn; Comm.CommandText = _CommandText;
77
78 Transaction = Conn.BeginTransaction(); Comm.Transaction = Transaction;
79
80 iQuery = Comm.ExecuteNonQuery(); Transaction.Commit(); return iQuery;
81 }
82 catch (OleDbException OleDbEx) { Transaction.Rollback(); throw OleDbEx; }
83 catch (Exception ex) { throw ex; }
84 finally { Transaction.Dispose(); Conn.Close(); Conn.Dispose(); }
85 }
86 /// <summary>
87 /// 执行 SQL 语句,并返回结果集中的第一行第一列。忽略其它行或列
88 /// </summary>
89 /// <returns></returns>
90 public object ExecuteScalar()
91 {
92 if (string.IsNullOrEmpty(_ConnectionString)) { throw (new Exception("无传入数据连接字串")); }
93
94 if (string.IsNullOrEmpty(_CommandText)) { throw (new Exception("无传入SQL语句")); }
95
96 if (!DSAFConnectionTest()) { throw (new Exception("尝试数据库连接失败")); }
97
98 object iQuery = null; OleDbTransaction Transaction = null;
99
100 try
101 {
102 OleDbCommand Comm = new OleDbCommand(); Conn = new OleDbConnection();
103
104 Conn.ConnectionString = _ConnectionString; Comm.Connection = Conn; Comm.CommandText = _CommandText;
105
106 Transaction = Conn.BeginTransaction(); Comm.Transaction = Transaction;
107
108 iQuery = Comm.ExecuteScalar(); Transaction.Commit(); return iQuery;
109 }
110 catch (OleDbException OleDbEx) { Transaction.Rollback(); throw OleDbEx; }
111 catch (Exception ex) { throw ex; }
112 finally { Transaction.Dispose(); Conn.Close(); Conn.Dispose(); }
113 }
114 /// <summary>
115 /// 测试数据库连接
116 /// </summary>
117 /// <returns></returns>
118 private bool DSAFConnectionTest()
119 {
120 try
121 {
122 Conn = new OleDbConnection(); Conn.ConnectionString = _ConnectionString;
123
124 Conn.Open(); Conn.Close();
125 }
126 catch { return (false); }
127
128 return (true);
129 }
130
131 /// <summary>
132 /// 释放由 DevSDK.Data.OleDb 使用的所有资源
133 /// </summary>
134 public void Dispose()
135 {
136 if (Conn != null) { this.Conn.Dispose(); }
137
138 if (_DSAFDataSet != null) { this._DSAFDataSet = null; }
139 }
140 }
141 }
142

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sealyna/archive/2009/09/08/4532646.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: