设为首页 收藏本站
查看: 756|回复: 0

[经验分享] SQL Server数据库访问通用类

[复制链接]

尚未签到

发表于 2015-6-28 11:41:33 | 显示全部楼层 |阅读模式
  看完了周金桥老师《asp.net夜话》中关于三层架构的章节后,总算对三层架构有了一定的了解,书中写到了一个SQL Server数据库访问通用类,自我感觉写得非常好,所以花了点时间亲自敲了一边,注释写得很详细,稍微有点asp.net编程基础的人应该都能够看得懂,在下面的代码中除了返回ExecuteDataReader()方法外,都考虑了异常情况,这样的代码保证了即使出现异常,数据库连接对象也会得到关闭,这是通过using关键字来实现的。在这里给大家分享:
  




  1 using System;
  2  using System.Data;
  3  using System.Configuration;
  4  using System.Data.SqlClient;
  5  ///
  6  ///针对sql server数据库操作的通用类
  7  ///
  8  public class SqlHelper
  9 {
10     public SqlHelper()
11     {
12         //
13         //TODO: 在此处添加构造函数逻辑
14         //
15      }
16     private string connectionstring;
17     ///
18     /// 设置连接字符串
19     ///
20      public string connectionstring
21     {
22         set { connectionstring = value; }
23     }
24     ///
25     /// 构造函数
26     ///
27     /// 数据库连接字符串
28     ///
29      public SqlHelper(string connectionstring)
30     {
31         this .connectionstring =connectionstring ;
32     }
33
34
35
36     ///
37     /// 执行一个查询,并返回结果集
38     ///
39     /// 要执行的查询sql文本命令
40     /// 返回查询结果集
41      public DataTable ExecuteDataTable(string sql)
42     {
43         return ExecuteDataTable(sql,CommandType.Text ,null);
44     }
45     ///
46     /// 执行一个查询,并返回查询结果
47     ///
48     /// 要执行的sql语句
49     /// 要执行的查询语句的类型,如存储过程或sql文本命令
50     /// 返回查询结果集
51     public DataTable ExecuteDataTable(string sql,CommandType commandType)
52     {
53         return ExecuteDataTable(sql ,commandType ,null );
54     }
55     ///
56     /// 执行一个查询,并且返回查询结果
57     ///
58     /// 要执行的sql语句
59     /// 要执行的查询语句的类型,如存储过程或sql文本命令
60     /// Transact-SQL语句或存储过程的参数数组
61     ///
62     public DataTable ExecuteDataTable(string sql, CommandType commandType, SqlParameter[] parameters)
63     {
64         DataTable data = new DataTable();//实例化datatable,用于装载查询结果集
65         using (SqlConnection connection= new SqlConnection(connectionstring))
66         {
67             using (SqlCommand command = new SqlCommand(sql, connection))
68             {
69                 command.CommandType = commandType;//设置command的commandtype为制定的commandtype
70                 //如果同时传入的参数,则添加这些参数
71                 if(parameters !=null )
72                 {
73                     foreach (SqlParameter parameter in parameters)
74                     {
75                         command.Parameters.Add(parameters);
76                     }
77                 }
78                 //通过包含查询SQL的sqlcommand实例来实例化sqldataadapter
79                 SqlDataAdapter adapter = new SqlDataAdapter(command);
80                 adapter.Fill(data);//填充DataTable
81             }
82         }
83         return data;
84     }
85
86
87
88     ///
89     /// 返回一个sqldatareader对象的实例
90     ///
91     /// 要执行的查询sql文本命令
92     ///
93     public SqlDataReader ExecuteReader(string sql)
94     {
95         return ExecuteReader(sql ,CommandType.Text ,null );
96     }
97     ///
98     /// 返回一个sqldatareader对象的实例
99     ///
100     /// 要执行的查询sql文本命令
101     /// 要执行的查询语句的类型,如存储过程或sql文本命令
102     ///
103     public SqlDataReader ExecuteReader(string sql,CommandType  commandtype)
104     {
105         return ExecuteReader(sql,commandtype,null );
106     }
107     public SqlDataReader ExecuteReader(string sql, CommandType commandtype, SqlParameter[] parameters)
108     {
109         SqlConnection connection = new SqlConnection(connectionstring);
110         SqlCommand command = new SqlCommand(sql,connection);
111         //如果传入了参数,则添加这些参数
112         if (parameters != null)
113         {
114             foreach (SqlParameter parameters in parameters)
115             {
116                 command.Parameters.Add(parameters );
117             }
118         }
119         connection.Open();
120         //CommandBehavior .CloseConnection参数指示关闭Reader对象时关闭与其关联的connection对象
121         return command.ExecuteReader(CommandBehavior .CloseConnection);
122     }
123
124
125
126     ///
127     /// 执行一个查询,返回查询结果集的第一行,忽略其他的行和列
128     ///
129     /// 要执行的查询sql文本命令
130     ///
131     public Object ExecuteScalar(string sql)
132     {
133         return ExecuteScalar(sql,CommandType .Text ,null);
134     }
135     ///
136     ///  执行一个查询,返回查询结果集的第一行,忽略其他的行和列
137     ///
138     /// 要执行的SQL语句
139     /// 要执行的查询语句的类型,如存储过程或sql文本命令
140     ///
141     public Object ExecuteScalar(string sql,CommandType commandtype)
142     {
143         return ExecuteScalar(sql, CommandType.Text, null);
144     }
145     public Object ExecuteScalar(string sql,CommandType commandtype,SqlParameter[] parameter)
146     {
147         object result = null;
148         using (SqlConnection connection = new SqlConnection(connectionstring))
149         {
150             using (SqlCommand command = new SqlCommand(sql, connection))
151             {
152                 command.CommandType = commandtype;//设置command的commandtype为指定的commandtype
153                 //如果同时传入了参数,则添加这些参数
154                 if (parameter != null)
155                 {
156                     foreach (SqlParameter parameters in parameter)
157                     {
158                         command.Parameters.Add(parameter);
159                     }
160                 }
161                 connection.Open();//打开数据库
162                 result = command.ExecuteScalar();
163             }
164         }
165         return result;//返回查询结果的第一行和第一列,忽略其他行和列
166     }
167
168
169     ///
170     /// 对数据库实行增删改操作
171     ///
172     /// 要执行的查询Sql文本命令
173     ///
174     public int ExecuteNonQuery(string sql)
175     {
176         return ExecuteNonQuery(sql,CommandType .Text ,null );
177     }
178     ///
179     /// 对数据库实行增删改操作
180     ///
181     /// 要执行的查询Sql文本命令
182     /// 要执行的查询语句的类型,如存储过程或sql文本命令
183     ///
184     public int ExecuteNonQuery(string sql,CommandType commandtype)
185     {
186         return ExecuteNonQuery(sql, CommandType.Text, null);
187     }
188     ///
189     /// 对数据库实行增删改操作
190     ///
191     /// 要执行的查询Sql文本命令
192     /// 要执行的查询语句的类型,如存储过程或sql文本命令
193     /// 要执行的查询语句的类型,如存储过程或sql文本命令
194     ///
195     public int ExecuteNonQuery(string sql,CommandType commandtype,SqlParameter[] parameters )
196     {
197         int count = 0;
198         using (SqlConnection connection = new SqlConnection(connectionstring))
199         {
200             using (SqlCommand command = new SqlCommand(sql, connection))
201             {
202                 command.CommandType = commandtype;//设置command的commandtype为制定commandtype
203                 //如果同时传入了参数,则添加这些参数
204                 if (parameters != null)
205                 {
206                     foreach (SqlParameter parameter in parameters)
207                     {
208                         command.Parameters.Add(parameters);
209                     }
210                 }
211                 connection.Open();//打开数据库连接
212                 count = command.EndExecuteNonQuery();
213             }
214         }
215         return count;//执行增删改查操作后,返回数据库中受影响的行数
216     }
217
218
219
220     ///
221     /// 返回当前连接的数据库中所有由用户创建的数据库
222     ///
223     ///
224     public DataTable GetTables()
225     {
226         DataTable data = null;
227         using (SqlConnection connection = new SqlConnection(connectionstring))
228         {
229             connection.Open();//打开数据库连接
230             data = connection.GetSchema("Tables");
231         }
232         return data;
233     }
234 }
235

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-81158-1-1.html 上篇帖子: SQL SERVER 系列(1)那些我们错过的细节 下篇帖子: SQL Server之1:全文搜索(1)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表