1.数据库加载驱动和操作接口 IDbProvider
代码
1
/*
*
2 *
3 * 2009-4-22
4 *
5 *
6 * 数据库操作加载驱动接口,
7 * 提供了数据库操作的各种命令
8 * */
9 using System;
10 using System.Collections.Generic;
11 using System.Linq;
12 using System.Text;
13 using System.Data;
14
15 namespace CommonData.Data
16 {
17 public interface IDbProvider:IDisposable
18 {
19 string ConnectionString { get ; set ; }
20
21 IDbConnection Connection { get ; set ; }
22
23 IDbCommand Command { get ; set ; }
24
25 IDbDataAdapter Adapter { get ; set ; }
26
27 void Open();
28
29 void Close();
30
31 // --------------------控制事务
32 void BeginTransaction();
33
34 void RollBack();
35
36 void Commit();
37
38
39 // -------------------数据提供加载驱动
40 IDbProvider CreateInstance();
41 }
42 }
43
2 *
3 * 2009-4-22
4 *
5 *
6 * 数据库操作加载驱动接口,
7 * 提供了数据库操作的各种命令
8 * */
9 using System;
10 using System.Collections.Generic;
11 using System.Linq;
12 using System.Text;
13 using System.Data;
14
15 namespace CommonData.Data
16 {
17 public interface IDbProvider:IDisposable
18 {
19 string ConnectionString { get ; set ; }
20
21 IDbConnection Connection { get ; set ; }
22
23 IDbCommand Command { get ; set ; }
24
25 IDbDataAdapter Adapter { get ; set ; }
26
27 void Open();
28
29 void Close();
30
31 // --------------------控制事务
32 void BeginTransaction();
33
34 void RollBack();
35
36 void Commit();
37
38
39 // -------------------数据提供加载驱动
40 IDbProvider CreateInstance();
41 }
42 }
43
该接口封装了数据库连接语句,数据库连接对象,数据库操作命令,数据库适配器几个属性,并且都是使用的接口类型。在现实的过程中 因为数据库选择的不同,会导致后面接口实现类的代码不同,如果程序都是使用接口来操作,就不用担心数据库的修改而大量更改代码,只需 要修改实现类的操作过程就可以了。
这个接口还提供了几个方法:
void Open(); 这个用于打开数据库的连接
void Close(); 这个用于关闭数据库连接
数据库操作永远不可能只有一个原子操作,很多情况下都需要一连 串的原子操作组合,这个时候我们就必须考虑事务操作情况。下面的方法可以提供相应的事务操作
void BeginTransaction();
void RollBack();
void Commit();
2.数据库加载驱动和操作实现类SqlProvider
代码
1
/*
*
2 *
3 * 2009-4-22
4 *
5 *
6 * 数据提供加载驱动类
7 * */
8 using System;
9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Text;
12 using System.Data;
13 using System.Data.SqlClient;
14 using CommonData.Data;
15
16 namespace CommonData.Data
17 {
18 public class SqlProvider:IDbProvider
19 {
20 private string connectionString = " server=.\\SQLEXPRESS;database=Fengling;Integrated Security=true " ;
21 /// <summary>
22 /// 数据库连接字符串
23 /// </summary>
24 public string ConnectionString
25 {
26 get
27 {
28 if (connectionString == null )
29 {
30 & nbsp;connectionString = " server=.\\SQLEXPRESS;database=Fengling;Integrated Security=true " ;
31 }
32 return connectionString;
33 }
34 set
35 {
36 connectionString&nb sp; = value;
37 }
38 }
39
40 private IDbConnection connection = null ;
41 /// <summary>
42 /// 数据库连接对象
43 /// </summary>
44 public IDbConnection Connection
45 {
46 get
47 {
48 if (connection == null )
49 {
50 & nbsp;connection = new SqlConnection(connectionString);
51 }
52 return connection;
53 }
54 set
55 {
56 connection = value;
57 }
58 }
59
60 private IDbCommand command = null;
61 /// <summary>
62 /// 数据库命令操作对象
63 /// </summary>
64 public IDbCommand Command
65 {
66 get
67 {
68 if (command == null)
69 {
70 & nbsp;command = new SqlCommand();
71 & nbsp;command.Connection = connection;
72 }
73 return command;
74 }
75 set
76 {
77 command = value;
78 }
79 }
80
81 private IDbDataAdapter adapter = null;
82 /// <summary>
83 /// 数据库适配器对象
84 /// </summary>
85 public IDbDataAdapter Adapter
86 {
87 get
88 {
89 if (adapter == null)
90 {
91 & nbsp;adapter = new SqlDataAdapter(command as SqlCommand);
92 }
93 return adapter;
94 }
95 set
96 {
97 adapter = value;
98 }
99 }
100
101 /// <summary>
102 /// 数据库事务对象
103 /// </summary>
104 private IDbTransaction transaction = null;
105
106 /// <summary>
107 /// 打开数据库连接
108 /// </summary>
109 public void Open()
110 {
111 if (connection != null)
112 {
113 connection.Open ();
114 }
115 }
116
117 /// <summary>
118 /// 关闭数据库连接
119 /// </summary>
120 public void Close()
121 {
122 if (connection != null)
123 {
124 connection.Close ();
125 }
126 }
127
128 /// <summary>
129 /// 开始事务
130 /// </summary>
131 public void BeginTransaction()
132 {
133 transaction = connection.BeginTransaction();
134 command.Transaction = transaction;
135 }
136
137 /// <summary>
138 /// 事务回滚
139 /// </summary>
140 public void RollBack()
141 {
142 if (transaction != null)
143 {
144 transaction.Rollbac k();
145 command.Transaction = null;
146 transaction.Dispose ();
147 }
148 }
149
150 /// <summary>
151 /// 事务提交
152 /// </summary>
153 public void Commit()
154 {
155 if (transaction != null)
156 {
157 transaction.Commit ();
158 command.Transaction = null;
159 transaction.Dispose ();
160 }
161 }
162
163 /// <summary>
164 /// 创建数据库加载驱动
165 /// </summary>
166 /// <returns></returns>
167 public IDbProvider CreateInstance()
168 {
169 return new SqlProvider();
170 }
171
172 /// <summary>
173 /// 释放内存空间
174 /// </summary>
175 public void Dispose()
176 {
177 GC.SuppressFinalize(this);
178 }
179 }
180 }
181
2 *
3 * 2009-4-22
4 *
5 *
6 * 数据提供加载驱动类
7 * */
8 using System;
9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Text;
12 using System.Data;
13 using System.Data.SqlClient;
14 using CommonData.Data;
15
16 namespace CommonData.Data
17 {
18 public class SqlProvider:IDbProvider
19 {
20 private string connectionString = " server=.\\SQLEXPRESS;database=Fengling;Integrated Security=true " ;
21 /// <summary>
22 /// 数据库连接字符串
23 /// </summary>
24 public string ConnectionString
25 {
26 get
27 {
28 if (connectionString == null )
29 {
30 & nbsp;connectionString = " server=.\\SQLEXPRESS;database=Fengling;Integrated Security=true " ;
31 }
32 return connectionString;
33 }
34 set
35 {
36 connectionString&nb sp; = value;
37 }
38 }
39
40 private IDbConnection connection = null ;
41 /// <summary>
42 /// 数据库连接对象
43 /// </summary>
44 public IDbConnection Connection
45 {
46 get
47 {
48 if (connection == null )
49 {
50 & nbsp;connection = new SqlConnection(connectionString);
51 }
52 return connection;
53 }
54 set
55 {
56 connection = value;
57 }
58 }
59
60 private IDbCommand command = null;
61 /// <summary>
62 /// 数据库命令操作对象
63 /// </summary>
64 public IDbCommand Command
65 {
66 get
67 {
68 if (command == null)
69 {
70 & nbsp;command = new SqlCommand();
71 & nbsp;command.Connection = connection;
72 }
73 return command;
74 }
75 set
76 {
77 command = value;
78 }
79 }
80
81 private IDbDataAdapter adapter = null;
82 /// <summary>
83 /// 数据库适配器对象
84 /// </summary>
85 public IDbDataAdapter Adapter
86 {
87 get
88 {
89 if (adapter == null)
90 {
91 & nbsp;adapter = new SqlDataAdapter(command as SqlCommand);
92 }
93 return adapter;
94 }
95 set
96 {
97 adapter = value;
98 }
99 }
100
101 /// <summary>
102 /// 数据库事务对象
103 /// </summary>
104 private IDbTransaction transaction = null;
105
106 /// <summary>
107 /// 打开数据库连接
108 /// </summary>
109 public void Open()
110 {
111 if (connection != null)
112 {
113 connection.Open ();
114 }
115 }
116
117 /// <summary>
118 /// 关闭数据库连接
119 /// </summary>
120 public void Close()
121 {
122 if (connection != null)
123 {
124 connection.Close ();
125 }
126 }
127
128 /// <summary>
129 /// 开始事务
130 /// </summary>
131 public void BeginTransaction()
132 {
133 transaction = connection.BeginTransaction();
134 command.Transaction = transaction;
135 }
136
137 /// <summary>
138 /// 事务回滚
139 /// </summary>
140 public void RollBack()
141 {
142 if (transaction != null)
143 {
144 transaction.Rollbac k();
145 command.Transaction = null;
146 transaction.Dispose ();
147 }
148 }
149
150 /// <summary>
151 /// 事务提交
152 /// </summary>
153 public void Commit()
154 {
155 if (transaction != null)
156 {
157 transaction.Commit ();
158 command.Transaction = null;
159 transaction.Dispose ();
160 }
161 }
162
163 /// <summary>
164 /// 创建数据库加载驱动
165 /// </summary>
166 /// <returns></returns>
167 public IDbProvider CreateInstance()
168 {
169 return new SqlProvider();
170 }
171
172 /// <summary>
173 /// 释放内存空间
174 /// </summary>
175 public void Dispose()
176 {
177 GC.SuppressFinalize(this);
178 }
179 }
180 }
181
SqlProvider 实现了接口IDbProvider,这里采用的是连接sql Server 数据库。其中数据库连接语句可以配置在Web.config中,这里 只是做测试用。至于实现接口的过程不必再讲解。