4.使用ADO.NET实现(三层架构篇-使用Table传递数据)(1)
4.1 解决方案框架
解决方案(.sln)包含以下几个项目:
(1)类库项目HomeShop.DbUtility,数据访问实用工具;
(2)类库项目HomeShop.Model,实体层;
(3)类库项目HomeShop.DAL,数据访问层;
(4)类库项目HomeShop.BLL,业务逻辑层;
(5)WinForm项目HomeShop.WinForm,界面层。
4.2 数据访问实用工具HomeShop.DbUtility
SqlDbHelper.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data;//新添命名空间
6 using System.Configuration;//新添命名空间
7 using System.Data.SqlClient;//新添命名空间
8
9 namespace HomeShop.DbUtility
10 {
11 //功能:SQL Server数据访问基础类;
12 //作者:夏春涛;
13 //日期:2011-11-30;
14 public class SqlDbHelper
15 {
16 //数据库连接字符串
17 private string connectionString = "";
18 public string ConnectionString
19 {
20 get {return connectionString; }
21 }
22
23 //数据库连接对象
24 private SqlConnection connection;
25
26 //默认构造函数,从应用程序配置文件中读取第1个数据库连接字符串
27 public SqlDbHelper()
28 {
29 this.connectionString = ConfigurationManager.ConnectionStrings[1].ConnectionString;
30 this.connection = new SqlConnection(this.connectionString);
31 }
32
33 //重载构造函数,根据入参初始化数据库连接字符串
34 public SqlDbHelper(string connectionString)
35 {
36 this.connectionString = connectionString;
37 this.connection = new SqlConnection(this.connectionString);
38 }
39
40 //执行SQL命令(Select),获取返回数据-重载函数
41 public DataTable ExecuteQuery(string selectCommandText)
42 {
43 return ExecuteQuery(selectCommandText, null);
44 }
45
46 //执行SQL命令(Select),获取返回数据
47 public DataTable ExecuteQuery(string selectCommandText, params SqlParameter[] cmdParameters)
48 {
49 connection.Open();
50 SqlCommand command = new SqlCommand(selectCommandText, connection);
51 if (null != cmdParameters)
52 {
53 for (int i = 0; i < cmdParameters.Length; i++)
54 {
55 command.Parameters.Add(cmdParameters[i]);
56 }
57 }
58 SqlDataAdapter adapter = new SqlDataAdapter(command);
59 DataTable dataTable = new DataTable();
60 adapter.Fill(dataTable);
61 connection.Close();
62
63 return dataTable;
64 }
65
66 //执行SQL命令(Insert/Delete/Update),以数据库事务方式
67 public int ExecuteNonQueryTrans(string cmdText, params SqlParameter[] cmdParameters)
68 {
69 int rowsCountAffected = 0;
70 connection.Open();
71 SqlCommand command = new SqlCommand(cmdText, connection);
72 if (null != cmdParameters)
73 {
74 for (int i = 0; i < cmdParameters.Length; i++)
75 {
76 command.Parameters.Add(cmdParameters[i]);
77 }
78 }
79 //开始数据库事务
80 SqlTransaction trans = connection.BeginTransaction();
81 command.Transaction = trans;
82 try
83 {
84 rowsCountAffected += command.ExecuteNonQuery();
85 trans.Commit();//提交数据库事务
86 }
87 catch
88 {
89 trans.Rollback();//回滚数据库事务
90 throw;
91 }
92 connection.Close();
93
94 return rowsCountAffected;
95 }
96
97 //执行多条SQL(Insert/Delete/Update),以数据库事务方式-重载函数
98 public int ExecuteNonQueryTrans(params SqlCmdTextAndParams[] cmdTextAndParams)
99 {
100 List<SqlCmdTextAndParams> list = new List<SqlCmdTextAndParams>();
101 for(int i=0;i<cmdTextAndParams.Length;i++)
102 {
103 list.Add(cmdTextAndParams[i]);
104 }
105
106 return ExecuteNonQueryTrans(list);
107 }
108
109 //执行多条SQL(Insert/Delete/Update),以数据库事务方式
110 public int ExecuteNonQueryTrans(List<SqlCmdTextAndParams> listCmdTextAndParams)
111 {
112 int rowsCountAffected = 0;
113 connection.Open();
114 //开始数据库事务
115 SqlTransaction trans = connection.BeginTransaction();
116 SqlCommand command = new SqlCommand();
117 command.Connection = connection;
118 command.Transaction = trans;
119 try
120 {
121 //循环执行每一个CommandTextAndParameters对象
122 foreach (SqlCmdTextAndParams cmdTextAndParams in listCmdTextAndParams)
123 {
124 command.CommandText = cmdTextAndParams.CommandText;
125 foreach (SqlParameter param in cmdTextAndParams.CommandParameters)
126 {
127 command.Parameters.Add(param);
128 }
129 rowsCountAffected += command.ExecuteNonQuery();
130 //清空参数列表,避免参数重名问题
131 command.Parameters.Clear();
132 }
133 trans.Commit();//提交数据库事务
134 }
135 catch
136 {
137 trans.Rollback();//回滚数据库事务
138 throw;
139 }
140 connection.Close();
141
142 return rowsCountAffected;
143 }
144
145 //开始(生成)一个数据库事务
146 public SqlTransaction BeginTransaction()
147 {
148 connection.Open();
149 SqlTransaction trans = connection.BeginTransaction();
150
151 return trans;
152 }
153
154 //在指定的数据库事务中执行SQL命令,最后要手工提交事务
155 public int ExecuteNonQueryTrans(SqlTransaction inTransaction, string cmdText, params SqlParameter[] cmdParameters)
156 {
157 int rowsCountAffected = 0;
158 SqlCommand command = new SqlCommand(cmdText, inTransaction.Connection);
159 if (null != cmdParameters)
160 {
161 for (int i = 0; i < cmdParameters.Length; i++)
162 {
163 command.Parameters.Add(cmdParameters[i]);
164 }
165 }
166 command.Transaction = inTransaction;
167 rowsCountAffected += command.ExecuteNonQuery();
168 command.Parameters.Clear();//清空参数列表,避免参数重名问题
169
170 return rowsCountAffected;
171 }
172
173 //关闭数据库连接
174 public void Close()
175 {
176 if (ConnectionState.Open == this.connection.State )
177 this.connection.Close();
178 }
179
180 /****************************************************************************************/
181 }
182 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data;//新添命名空间
6 using System.Configuration;//新添命名空间
7 using System.Data.SqlClient;//新添命名空间
8
9 namespace HomeShop.DbUtility
10 {
11 //功能:SQL Server数据访问基础类;
12 //作者:夏春涛;
13 //日期:2011-11-30;
14 public class SqlDbHelper
15 {
16 //数据库连接字符串
17 private string connectionString = "";
18 public string ConnectionString
19 {
20 get {return connectionString; }
21 }
22
23 //数据库连接对象
24 private SqlConnection connection;
25
26 //默认构造函数,从应用程序配置文件中读取第1个数据库连接字符串
27 public SqlDbHelper()
28 {
29 this.connectionString = ConfigurationManager.ConnectionStrings[1].ConnectionString;
30 this.connection = new SqlConnection(this.connectionString);
31 }
32
33 //重载构造函数,根据入参初始化数据库连接字符串
34 public SqlDbHelper(string connectionString)
35 {
36 this.connectionString = connectionString;
37 this.connection = new SqlConnection(this.connectionString);
38 }
39
40 //执行SQL命令(Select),获取返回数据-重载函数
41 public DataTable ExecuteQuery(string selectCommandText)
42 {
43 return ExecuteQuery(selectCommandText, null);
44 }
45
46 //执行SQL命令(Select),获取返回数据
47 public DataTable ExecuteQuery(string selectCommandText, params SqlParameter[] cmdParameters)
48 {
49 connection.Open();
50 SqlCommand command = new SqlCommand(selectCommandText, connection);
51 if (null != cmdParameters)
52 {
53 for (int i = 0; i < cmdParameters.Length; i++)
54 {
55 command.Parameters.Add(cmdParameters[i]);
56 }
57 }
58 SqlDataAdapter adapter = new SqlDataAdapter(command);
59 DataTable dataTable = new DataTable();
60 adapter.Fill(dataTable);
61 connection.Close();
62
63 return dataTable;
64 }
65
66 //执行SQL命令(Insert/Delete/Update),以数据库事务方式
67 public int ExecuteNonQueryTrans(string cmdText, params SqlParameter[] cmdParameters)
68 {
69 int rowsCountAffected = 0;
70 connection.Open();
71 SqlCommand command = new SqlCommand(cmdText, connection);
72 if (null != cmdParameters)
73 {
74 for (int i = 0; i < cmdParameters.Length; i++)
75 {
76 command.Parameters.Add(cmdParameters[i]);
77 }
78 }
79 //开始数据库事务
80 SqlTransaction trans = connection.BeginTransaction();
81 command.Transaction = trans;
82 try
83 {
84 rowsCountAffected += command.ExecuteNonQuery();
85 trans.Commit();//提交数据库事务
86 }
87 catch
88 {
89 trans.Rollback();//回滚数据库事务
90 throw;
91 }
92 connection.Close();
93
94 return rowsCountAffected;
95 }
96
97 //执行多条SQL(Insert/Delete/Update),以数据库事务方式-重载函数
98 public int ExecuteNonQueryTrans(params SqlCmdTextAndParams[] cmdTextAndParams)
99 {
100 List<SqlCmdTextAndParams> list = new List<SqlCmdTextAndParams>();
101 for(int i=0;i<cmdTextAndParams.Length;i++)
102 {
103 list.Add(cmdTextAndParams[i]);
104 }
105
106 return ExecuteNonQueryTrans(list);
107 }
108
109 //执行多条SQL(Insert/Delete/Update),以数据库事务方式
110 public int ExecuteNonQueryTrans(List<SqlCmdTextAndParams> listCmdTextAndParams)
111 {
112 int rowsCountAffected = 0;
113 connection.Open();
114 //开始数据库事务
115 SqlTransaction trans = connection.BeginTransaction();
116 SqlCommand command = new SqlCommand();
117 command.Connection = connection;
118 command.Transaction = trans;
119 try
120 {
121 //循环执行每一个CommandTextAndParameters对象
122 foreach (SqlCmdTextAndParams cmdTextAndParams in listCmdTextAndParams)
123 {
124 command.CommandText = cmdTextAndParams.CommandText;
125 foreach (SqlParameter param in cmdTextAndParams.CommandParameters)
126 {
127 command.Parameters.Add(param);
128 }
129 rowsCountAffected += command.ExecuteNonQuery();
130 //清空参数列表,避免参数重名问题
131 command.Parameters.Clear();
132 }
133 trans.Commit();//提交数据库事务
134 }
135 catch
136 {
137 trans.Rollback();//回滚数据库事务
138 throw;
139 }
140 connection.Close();
141
142 return rowsCountAffected;
143 }
144
145 //开始(生成)一个数据库事务
146 public SqlTransaction BeginTransaction()
147 {
148 connection.Open();
149 SqlTransaction trans = connection.BeginTransaction();
150
151 return trans;
152 }
153
154 //在指定的数据库事务中执行SQL命令,最后要手工提交事务
155 public int ExecuteNonQueryTrans(SqlTransaction inTransaction, string cmdText, params SqlParameter[] cmdParameters)
156 {
157 int rowsCountAffected = 0;
158 SqlCommand command = new SqlCommand(cmdText, inTransaction.Connection);
159 if (null != cmdParameters)
160 {
161 for (int i = 0; i < cmdParameters.Length; i++)
162 {
163 command.Parameters.Add(cmdParameters[i]);
164 }
165 }
166 command.Transaction = inTransaction;
167 rowsCountAffected += command.ExecuteNonQuery();
168 command.Parameters.Clear();//清空参数列表,避免参数重名问题
169
170 return rowsCountAffected;
171 }
172
173 //关闭数据库连接
174 public void Close()
175 {
176 if (ConnectionState.Open == this.connection.State )
177 this.connection.Close();
178 }
179
180 /****************************************************************************************/
181 }
182 }
SqlCmdTextAndParams.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data.SqlClient;//新添命名空间
6
7 namespace HomeShop.DbUtility
8 {
9 //用于保存一条SQL命令及对应参数的类
10 public class SqlCmdTextAndParams
11 {
12 public SqlCmdTextAndParams(string commandText, params SqlParameter[] commandParamters)
13 {
14 this.CommandText = commandText;
15 this.CommandParameters = commandParamters;
16 }
17
18 public string CommandText { set; get; }
19
20 public SqlParameter[] CommandParameters { set; get; }
21 }
22 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data.SqlClient;//新添命名空间
6
7 namespace HomeShop.DbUtility
8 {
9 //用于保存一条SQL命令及对应参数的类
10 public class SqlCmdTextAndParams
11 {
12 public SqlCmdTextAndParams(string commandText, params SqlParameter[] commandParamters)
13 {
14 this.CommandText = commandText;
15 this.CommandParameters = commandParamters;
16 }
17
18 public string CommandText { set; get; }
19
20 public SqlParameter[] CommandParameters { set; get; }
21 }
22 }
数据库文件:/Files/SummerRain/NetDbDevRoad/HomeShopDB.rar
完整源代码:/Files/SummerRain/NetDbDevRoad/4使用ADONET实现三层架构Table.rar