SqlDbHelper.cs
002 |
using System.Collections.Generic; |
005 |
using System.Data.SqlClient; |
006 |
using System.Configuration; |
008 |
namespace ADODoNETDemo |
011 |
/// 针对SQL Server数据库操作的通用类 |
016 |
public class SqlDbHelper |
018 |
private string connectionString; |
022 |
public string ConnectionString |
024 |
set { connectionString = value; } |
030 |
: this(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString) |
037 |
/// <param name="connectionString">数据库连接字符串</param> |
038 |
public SqlDbHelper(string connectionString) |
040 |
this.connectionString = connectionString; |
045 |
/// <param name="sql">要执行的查询SQL文本命令</param> |
046 |
/// <returns>返回查询结果集</returns> |
047 |
public DataTable ExecuteDataTable(string sql) |
049 |
return ExecuteDataTable(sql, CommandType.Text, null); |
054 |
/// <param name="sql">要执行的SQL语句</param> |
055 |
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> |
056 |
/// <returns>返回查询结果集</returns> |
057 |
public DataTable ExecuteDataTable(string sql, CommandType commandType) |
059 |
return ExecuteDataTable(sql, commandType, null); |
064 |
/// <param name="sql">要执行的SQL语句</param> |
065 |
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> |
066 |
/// <param name="parameters">Transact-SQL 语句或存储过程的参数数组</param> |
067 |
/// <returns></returns> |
068 |
public DataTable ExecuteDataTable(string sql, CommandType commandType, SqlParameter[] parameters) |
070 |
DataTable data = new DataTable(); |
071 |
using (SqlConnection connection = new SqlConnection(connectionString)) |
073 |
using (SqlCommand command = new SqlCommand(sql, connection)) |
075 |
command.CommandType = commandType; |
077 |
if (parameters != null) |
079 |
foreach (SqlParameter parameter in parameters) |
081 |
command.Parameters.Add(parameter); |
085 |
SqlDataAdapter adapter = new SqlDataAdapter(command); |
095 |
/// <param name="sql">要执行的查询SQL文本命令</param> |
096 |
/// <returns></returns> |
097 |
public SqlDataReader ExecuteReader(string sql) |
099 |
return ExecuteReader(sql, CommandType.Text, null); |
104 |
/// <param name="sql">要执行的SQL语句</param> |
105 |
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> |
106 |
/// <returns></returns> |
107 |
public SqlDataReader ExecuteReader(string sql, CommandType commandType) |
109 |
return ExecuteReader(sql, commandType, null); |
114 |
/// <param name="sql">要执行的SQL语句</param> |
115 |
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> |
116 |
/// <param name="parameters">Transact-SQL 语句或存储过程的参数数组</param> |
117 |
/// <returns></returns> |
118 |
public SqlDataReader ExecuteReader(string sql, CommandType commandType, SqlParameter[] parameters) |
120 |
SqlConnection connection = new SqlConnection(connectionString); |
121 |
SqlCommand command = new SqlCommand(sql, connection); |
123 |
if (parameters != null) |
125 |
foreach (SqlParameter parameter in parameters) |
127 |
command.Parameters.Add(parameter); |
132 |
return command.ExecuteReader(CommandBehavior.CloseConnection); |
137 |
/// <param name="sql">要执行的查询SQL文本命令</param> |
138 |
/// <returns></returns> |
139 |
public Object ExecuteScalar(string sql) |
141 |
return ExecuteScalar(sql, CommandType.Text, null); |
146 |
/// <param name="sql">要执行的SQL语句</param> |
147 |
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> |
148 |
/// <returns></returns> |
149 |
public Object ExecuteScalar(string sql, CommandType commandType) |
151 |
return ExecuteScalar(sql, commandType, null); |
156 |
/// <param name="sql">要执行的SQL语句</param> |
157 |
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> |
158 |
/// <param name="parameters">Transact-SQL 语句或存储过程的参数数组</param> |
159 |
/// <returns></returns> |
160 |
public Object ExecuteScalar(string sql, CommandType commandType, SqlParameter[] parameters) |
162 |
object result = null; |
163 |
using (SqlConnection connection = new SqlConnection(connectionString)) |
165 |
using (SqlCommand command = new SqlCommand(sql, connection)) |
167 |
command.CommandType = commandType; |
169 |
if (parameters != null) |
171 |
foreach (SqlParameter parameter in parameters) |
173 |
command.Parameters.Add(parameter); |
177 |
result = command.ExecuteScalar(); |
185 |
/// <param name="sql">要执行的查询SQL文本命令</param> |
186 |
/// <returns></returns> |
187 |
public int ExecuteNonQuery(string sql) |
189 |
return ExecuteNonQuery(sql, CommandType.Text, null); |
194 |
/// <param name="sql">要执行的SQL语句</param> |
195 |
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> |
196 |
/// <returns></returns> |
197 |
public int ExecuteNonQuery(string sql, CommandType commandType) |
199 |
return ExecuteNonQuery(sql, commandType, null); |
204 |
/// <param name="sql">要执行的SQL语句</param> |
205 |
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> |
206 |
/// <param name="parameters">Transact-SQL 语句或存储过程的参数数组</param> |
207 |
/// <returns></returns> |
208 |
public int ExecuteNonQuery(string sql, CommandType commandType, SqlParameter[] parameters) |
211 |
using (SqlConnection connection = new SqlConnection(connectionString)) |
213 |
using (SqlCommand command = new SqlCommand(sql, connection)) |
215 |
command.CommandType = commandType; |
217 |
if (parameters != null) |
219 |
foreach (SqlParameter parameter in parameters) |
221 |
command.Parameters.Add(parameter); |
225 |
count = command.ExecuteNonQuery(); |
231 |
/// 返回当前连接的数据库中所有由用户创建的数据库 |
233 |
/// <returns></returns> |
234 |
public DataTable GetTables() |
236 |
DataTable data = null; |
237 |
using (SqlConnection connection = new SqlConnection(connectionString)) |
240 |
data = connection.GetSchema("Tables"); |
[代码] ADODotNetCRUD.cs
002 |
using System.Collections.Generic; |
005 |
using System.Data.SqlClient; |
008 |
namespace ADODoNETDemo |
013 |
public class ADODotNetCRUD |
018 |
/// <returns></returns> |
021 |
string sql = "select count(1) from UserInfo"; |
022 |
SqlDbHelper db = new SqlDbHelper(); |
023 |
return int.Parse(db.ExecuteScalar(sql).ToString()); |
028 |
/// <param name="info">用户实体</param> |
029 |
/// <returns></returns> |
030 |
public bool Create(UserInfo info) |
032 |
string sql = "insert UserInfo(UserName,RealName,Age,Sex,Mobile,Email,Phone)values(@UserName,@RealName,@Age,@Sex,@Mobile,@Email,@Phone)"; |
033 |
SqlParameter[] paramters = new SqlParameter[]{ |
034 |
new SqlParameter("@UserName",info.UserName), |
035 |
new SqlParameter("@RealName",info.RealName), |
036 |
new SqlParameter("@Age",info.Age), |
037 |
new SqlParameter("@Sex",info.Sex), |
038 |
new SqlParameter("@Mobile",info.Mobile), |
039 |
new SqlParameter("@Email",info.Email), |
040 |
new SqlParameter("@Phone",info.Phone), |
042 |
SqlDbHelper db = new SqlDbHelper(); |
043 |
return db.ExecuteNonQuery(sql, CommandType.Text, paramters) > 0; |
048 |
/// <param name="userId">用户编号</param> |
049 |
/// <returns></returns> |
050 |
public UserInfo Read(int userId) |
052 |
string sql = "select * from UserInfo Where UserId="+userId; |
053 |
SqlDbHelper db = new SqlDbHelper(); |
054 |
DataTable data = db.ExecuteDataTable(sql); |
055 |
if (data.Rows.Count > 0) |
057 |
DataRow row = data.Rows[0]; |
058 |
UserInfo info = new UserInfo() |
060 |
UserId=int.Parse(row["UserId"].ToString()), |
061 |
UserName=row["UserName"].ToString(), |
062 |
Age=byte.Parse(row["Age"].ToString()), |
063 |
Email=row["Email"].ToString(), |
064 |
Mobile=row["Mobile"].ToString(), |
065 |
Phone=row["Phone"].ToString(), |
066 |
RealName=row["RealName"].ToString(), |
067 |
Sex=bool.Parse(row["Sex"].ToString()) |
079 |
/// <param name="info">用户实体</param> |
080 |
/// <returns></returns> |
081 |
public bool Update(UserInfo info) |
083 |
string sql = "update UserInfo set UserName=@UserName,RealName=@RealName,Age=@Age,Sex=@Sex,Mobile=@Mobile,Email=@Email,Phone=@Phone where UserID=@UserID"; |
084 |
SqlParameter[] paramters = new SqlParameter[]{ |
085 |
new SqlParameter("@UserName",info.UserName), |
086 |
new SqlParameter("@RealName",info.RealName), |
087 |
new SqlParameter("@Age",info.Age), |
088 |
new SqlParameter("@Sex",info.Sex), |
089 |
new SqlParameter("@Mobile",info.Mobile), |
090 |
new SqlParameter("@Email",info.Email), |
091 |
new SqlParameter("@Phone",info.Phone), |
092 |
new SqlParameter("@UserID",info.UserId), |
094 |
SqlDbHelper db = new SqlDbHelper(); |
095 |
return db.ExecuteNonQuery(sql, CommandType.Text, paramters) > 0; |
100 |
/// <param name="userId">用户编号</param> |
101 |
/// <returns></returns> |
102 |
public bool Delete(int userId) |
104 |
string sql = "delete from UserInfo where UserId=" + userId; |
105 |
SqlDbHelper db = new SqlDbHelper(); |
106 |
return db.ExecuteNonQuery(sql) > 0; |
111 |
/// <returns></returns> |
112 |
public int GetMaxUserId() |
114 |
string sql = "select max(userId) from UserInfo"; |
115 |
SqlDbHelper db = new SqlDbHelper(); |
116 |
return int.Parse(db.ExecuteScalar(sql).ToString()); |
[代码] 单元测试 ADODotNetTest.cs
02 |
using System.Collections.Generic; |
06 |
using NUnit.Framework; |
13 |
private ADODotNetCRUD instance = null; |
15 |
public void Initialize() |
17 |
instance = new ADODotNetCRUD(); |
23 |
/// <returns></returns> |
26 |
Assert.Greater(instance.Count(), 0); |
32 |
/// <param name="info">用户实体</param> |
33 |
/// <returns></returns> |
36 |
UserInfo info = new UserInfo() |
39 |
Email = "zzz@ccav.com", |
40 |
Mobile = "13812345678", |
41 |
Phone = "01012345678", |
42 |
RealName = "测试" + DateTime.Now.Millisecond.ToString(), |
44 |
UserName = "zhoufoxcn" + DateTime.Now.Millisecond.ToString() |
46 |
instance.Create(info); |
52 |
/// <param name="userId">用户编号</param> |
53 |
/// <returns></returns> |
56 |
UserInfo info = instance.Read(1); |
63 |
/// <param name="info">用户实体</param> |
64 |
/// <returns></returns> |
67 |
UserInfo info = instance.Read(1); |
68 |
info.RealName = "测试" + DateTime.Now.Millisecond.ToString(); |
69 |
instance.Update(info); |
75 |
/// <param name="userId">用户编号</param> |
76 |
/// <returns></returns> |
77 |
public void DeleteByID() |
79 |
int userId = instance.GetMaxUserId(); |
80 |
instance.Delete(userId); |
posted @ 2011-03-18 17:12
水木 阅读(227)
评论(0) 编辑 收藏