John.Commons.DBUtility for .NET 2.0(MS SQL 2005)
John.Commons.DBUtility for .NET 2.0(MS SQL 2005) (←点击下载)
1using System;
2using System.Configuration;
3using System.Data;
4using System.Data.SqlClient;
5using System.Collections;
6using System.Collections.Generic;
7
8namespace John.Commons.DBUtility
9{
10 /// <summary>
11 /// 数据访问基础类(基于SQLServer)
12 /// </summary>
13 public abstract class DbHelper4SQL
14 {
15 public DbHelper4SQL()
16 { }
17
18 私有方法 私有方法
91
92 参数缓存 参数缓存
112
113 /// <summary>
114 /// 执行一个SQL命令,返回影响的记录数
115 /// </summary>
116 /// <param name="sqlConnString">数据库连接字符串</param>
117 /// <param name="cmdText">命令文本</param>
118 /// <param name="cmdType">命令类型</param>
119 /// <returns>int 影响的记录数</returns>
120 public static int ExecuteNonQuery(string sqlConnString, string cmdText)
121
122 /// <summary>
123 /// 执行一个SQL命令,返回影响的记录数
124 /// </summary>
125 /// <param name="sqlConnString">数据库连接字符串</param>
126 /// <param name="cmdText">命令文本</param>
127 /// <param name="cmdType">命令类型</param>
128 /// <returns>int 影响的记录数</returns>
129 public static int ExecuteNonQuery(string sqlConnString, string cmdText, CommandType cmdType, params SqlParameter[] cmdParms)
130
131 /// <summary>
132 /// 执行一个SQL命令,返回结果集中第一行的第一列
133 /// </summary>
134 /// <param name="sqlConnString">数据库连接字符串</param>
135 /// <param name="cmdText">命令文本</param>
136 /// <returns>object 对象实例</returns>
137 public static object ExecuteScalar(string sqlConnString, string cmdText)
138
139 /// <summary>
140 /// 执行一个SQL命令,返回结果集中第一行的第一列
141 /// </summary>
142 /// <param name="sqlConnString">数据库连接字符串</param>
143 /// <param name="cmdText">命令文本</param>
144 /// <param name="cmdType">命令类型</param>
145 /// <param name="cmdParms">命令参数</param>
146 /// <returns>object 对象实例</returns>
147 public static object ExecuteScalar(string sqlConnString, string cmdText, CommandType cmdType, params SqlParameter[] cmdParms)
148
149 /// <summary>
150 /// 执行一个SQL命令,是否存在该记录,不存在返回F
151 /// </summary>
152 /// <param name="sqlConnString">数据库连接字符串</param>
153 /// <param name="cmdText">命令文本</param>
154 /// <param name="cmdType">命令类型</param>
155 /// <param name="cmdParms">命令参数</param>
156 /// <returns>bool</returns>
157 public static bool Exists(string sqlConnString, string cmdText, CommandType cmdType, params SqlParameter[] cmdParms)
158
159 /// <summary>
160 /// 执行一个SQL命令,返回SqlDataReader
161 /// </summary>
162 /// <param name="sqlConnString">数据库连接字符串</param>
163 /// <param name="cmdText">命令文本</param>
164 /// <returns>SqlDataReader</returns>
165 public static SqlDataReader ExecuteReader(string sqlConnString, string cmdText)
166
167 /// <summary>
168 /// 执行一个SQL命令,返回SqlDataReader
169 /// </summary>
170 /// <param name="sqlConnString">数据库连接字符串</param>
171 /// <param name="cmdText">命令文本</param>
172 /// <param name="cmdType">命令类型</param>
173 /// <param name="cmdParms">命令参数</param>
174 /// <returns>SqlDataReader</returns>
175 public static SqlDataReader ExecuteReader(string sqlConnString, string cmdText, CommandType cmdType, params SqlParameter[] cmdParms)
176
177 /// <summary>
178 /// 执行一个SQL命令,返回DataSet
179 /// </summary>
180 /// <param name="sqlConnString">数据库连接字符串</param>
181 /// <param name="cmdText">命令文本</param>
182 /// <returns>DataSet</returns>
183 public static DataSet ExecuteDataSet(string sqlConnString, string cmdText)
184
185 /// <summary>
186 /// 执行一个SQL命令,返回DataSet
187 /// </summary>
188 /// <param name="sqlConnString">数据库连接字符串</param>
189 /// <param name="cmdText">命令文本</param>
190 /// <param name="cmdType">命令类型</param>
191 /// <param name="cmdParms">命令参数</param>
192 /// <returns></returns>
193 public static DataSet ExecuteDataSet(string sqlConnString, string cmdText, CommandType cmdType, IDataParameter[] parameters)
194
195 /// <summary>
196 /// 执行多条SQL语句,实现数据库事务sqlConnString
197 /// </summary>
198 /// <param name="sqlConnString">数据库连接字符串</param>
199 /// <param name="sqlStringList">SQL语句列表</param>
200 /// <returns>int 影响的记录数</returns>
201 public static int ExecuteSqlTran(string sqlConnString, IList<string> sqlStringList)
202
203 /// <summary>
204 /// 执行多条SQL命令,实现数据库事务sqlConnString(单条sql命令,多组参数)
205 /// </summary>
206 /// <param name="sqlConnString">数据库连接字符串</param>
207 /// <param name="cmdText">命令文本</param>
208 /// <param name="cmdType">命令类型</param>
209 /// <param name="cmdParmsList">命令参数列表</param>
210 /// <returns>int 影响的记录数</returns>
211 public static int ExecuteSqlTran(string sqlConnString, string cmdText, CommandType cmdType, IList<SqlParameter[]> cmdParmsList)
212
213 /// <summary>
214 /// 执行多条SQL语句,实现数据库事务sqlConnString
215 /// </summary>
216 /// <param name="sqlConnString">数据库连接字符串</param>
217 /// <param name="cmdTextList">命令文本列表</param>
218 /// <param name="cmdTypeList">命令文本列表</param>
219 /// <param name="cmdParmsList">命令参数列表</param>
220 /// <returns>int 影响的记录数</returns>
221 public static int ExecuteSqlTran(string sqlConnString, IList<string> cmdTextList, IList<CommandType> cmdTypeList, IList<SqlParameter[]> cmdParmsList)
222
223 /// <summary>
224 /// 执行存储过程,返存储过程定义返回的一个整数
225 /// </summary>
226 /// <param name="sqlConnString">数据库连接字符串</param>
227 /// <param name="storedProcName">存储过程名</param>
228 /// <param name="parameters">存储过程参数</param>
229 /// <returns>影响的行数</returns>
230 public static int RunProcedureNonQuery(string sqlConnString, string storedProcName, IDataParameter[] parameters)
231
232 }
233}
2using System.Configuration;
3using System.Data;
4using System.Data.SqlClient;
5using System.Collections;
6using System.Collections.Generic;
7
8namespace John.Commons.DBUtility
9{
10 /// <summary>
11 /// 数据访问基础类(基于SQLServer)
12 /// </summary>
13 public abstract class DbHelper4SQL
14 {
15 public DbHelper4SQL()
16 { }
17
18 私有方法 私有方法
91
92 参数缓存 参数缓存
112
113 /// <summary>
114 /// 执行一个SQL命令,返回影响的记录数
115 /// </summary>
116 /// <param name="sqlConnString">数据库连接字符串</param>
117 /// <param name="cmdText">命令文本</param>
118 /// <param name="cmdType">命令类型</param>
119 /// <returns>int 影响的记录数</returns>
120 public static int ExecuteNonQuery(string sqlConnString, string cmdText)
121
122 /// <summary>
123 /// 执行一个SQL命令,返回影响的记录数
124 /// </summary>
125 /// <param name="sqlConnString">数据库连接字符串</param>
126 /// <param name="cmdText">命令文本</param>
127 /// <param name="cmdType">命令类型</param>
128 /// <returns>int 影响的记录数</returns>
129 public static int ExecuteNonQuery(string sqlConnString, string cmdText, CommandType cmdType, params SqlParameter[] cmdParms)
130
131 /// <summary>
132 /// 执行一个SQL命令,返回结果集中第一行的第一列
133 /// </summary>
134 /// <param name="sqlConnString">数据库连接字符串</param>
135 /// <param name="cmdText">命令文本</param>
136 /// <returns>object 对象实例</returns>
137 public static object ExecuteScalar(string sqlConnString, string cmdText)
138
139 /// <summary>
140 /// 执行一个SQL命令,返回结果集中第一行的第一列
141 /// </summary>
142 /// <param name="sqlConnString">数据库连接字符串</param>
143 /// <param name="cmdText">命令文本</param>
144 /// <param name="cmdType">命令类型</param>
145 /// <param name="cmdParms">命令参数</param>
146 /// <returns>object 对象实例</returns>
147 public static object ExecuteScalar(string sqlConnString, string cmdText, CommandType cmdType, params SqlParameter[] cmdParms)
148
149 /// <summary>
150 /// 执行一个SQL命令,是否存在该记录,不存在返回F
151 /// </summary>
152 /// <param name="sqlConnString">数据库连接字符串</param>
153 /// <param name="cmdText">命令文本</param>
154 /// <param name="cmdType">命令类型</param>
155 /// <param name="cmdParms">命令参数</param>
156 /// <returns>bool</returns>
157 public static bool Exists(string sqlConnString, string cmdText, CommandType cmdType, params SqlParameter[] cmdParms)
158
159 /// <summary>
160 /// 执行一个SQL命令,返回SqlDataReader
161 /// </summary>
162 /// <param name="sqlConnString">数据库连接字符串</param>
163 /// <param name="cmdText">命令文本</param>
164 /// <returns>SqlDataReader</returns>
165 public static SqlDataReader ExecuteReader(string sqlConnString, string cmdText)
166
167 /// <summary>
168 /// 执行一个SQL命令,返回SqlDataReader
169 /// </summary>
170 /// <param name="sqlConnString">数据库连接字符串</param>
171 /// <param name="cmdText">命令文本</param>
172 /// <param name="cmdType">命令类型</param>
173 /// <param name="cmdParms">命令参数</param>
174 /// <returns>SqlDataReader</returns>
175 public static SqlDataReader ExecuteReader(string sqlConnString, string cmdText, CommandType cmdType, params SqlParameter[] cmdParms)
176
177 /// <summary>
178 /// 执行一个SQL命令,返回DataSet
179 /// </summary>
180 /// <param name="sqlConnString">数据库连接字符串</param>
181 /// <param name="cmdText">命令文本</param>
182 /// <returns>DataSet</returns>
183 public static DataSet ExecuteDataSet(string sqlConnString, string cmdText)
184
185 /// <summary>
186 /// 执行一个SQL命令,返回DataSet
187 /// </summary>
188 /// <param name="sqlConnString">数据库连接字符串</param>
189 /// <param name="cmdText">命令文本</param>
190 /// <param name="cmdType">命令类型</param>
191 /// <param name="cmdParms">命令参数</param>
192 /// <returns></returns>
193 public static DataSet ExecuteDataSet(string sqlConnString, string cmdText, CommandType cmdType, IDataParameter[] parameters)
194
195 /// <summary>
196 /// 执行多条SQL语句,实现数据库事务sqlConnString
197 /// </summary>
198 /// <param name="sqlConnString">数据库连接字符串</param>
199 /// <param name="sqlStringList">SQL语句列表</param>
200 /// <returns>int 影响的记录数</returns>
201 public static int ExecuteSqlTran(string sqlConnString, IList<string> sqlStringList)
202
203 /// <summary>
204 /// 执行多条SQL命令,实现数据库事务sqlConnString(单条sql命令,多组参数)
205 /// </summary>
206 /// <param name="sqlConnString">数据库连接字符串</param>
207 /// <param name="cmdText">命令文本</param>
208 /// <param name="cmdType">命令类型</param>
209 /// <param name="cmdParmsList">命令参数列表</param>
210 /// <returns>int 影响的记录数</returns>
211 public static int ExecuteSqlTran(string sqlConnString, string cmdText, CommandType cmdType, IList<SqlParameter[]> cmdParmsList)
212
213 /// <summary>
214 /// 执行多条SQL语句,实现数据库事务sqlConnString
215 /// </summary>
216 /// <param name="sqlConnString">数据库连接字符串</param>
217 /// <param name="cmdTextList">命令文本列表</param>
218 /// <param name="cmdTypeList">命令文本列表</param>
219 /// <param name="cmdParmsList">命令参数列表</param>
220 /// <returns>int 影响的记录数</returns>
221 public static int ExecuteSqlTran(string sqlConnString, IList<string> cmdTextList, IList<CommandType> cmdTypeList, IList<SqlParameter[]> cmdParmsList)
222
223 /// <summary>
224 /// 执行存储过程,返存储过程定义返回的一个整数
225 /// </summary>
226 /// <param name="sqlConnString">数据库连接字符串</param>
227 /// <param name="storedProcName">存储过程名</param>
228 /// <param name="parameters">存储过程参数</param>
229 /// <returns>影响的行数</returns>
230 public static int RunProcedureNonQuery(string sqlConnString, string storedProcName, IDataParameter[] parameters)
231
232 }
233}