1
using System;
2
using System.Data;
3
using System.Data.SqlClient;
4
using System.Configuration;
5
using System.Web;
6
7
/**//// <summary>
8
/// DBbase 的摘要说明
9
/// </summary>
10
public class DBbase
11
...{
12
public DBbase()
13
...{
14
//
15
// TODO: 在此处添加构造函数逻辑
16
//
17
//该源码下载自www.51aspx.com(51aspx.com)
18
19
}
20
定义连接字符串strCon#region 定义连接字符串strCon
21
/**//// <summary>
22
/// 定义连接字符串strCon
23
/// </summary>
24
public static string strCon = "Data Source =(local);database = news2005;uid = sa;pwd =sa;";
25
//public static string strCon = System.Configuration.ConfigurationSettings.AppSettings["conStr"].ToString();
26
#endregion
27
28
实例化连接对象con#region 实例化连接对象con
29
/**//// <summary>
30
/// 实例化连接对象con
31
/// </summary>
32
SqlConnection con = new SqlConnection(strCon);
33
#endregion
34
35
检测连接是否打开#region 检测连接是否打开
36
/**//// <summary>
37
/// 检测连接的方法CheckConnection(),若连接是关闭的则打开SqlConnection连接
38
/// </summary>
39
public void CheckConnection()
40
...{
41
if (this.con.State == ConnectionState.Closed)
42
...{
43
this.con.Open();
44
}
45
}
46
#endregion
47
48
执行语句返回DataSet数据集#region 执行语句返回DataSet数据集
49
/**//// <summary>
50
/// 执行语句返回DataSet数据集
51
/// </summary>
52
/// <param name="strSQL">要执行的SQL语句</param>
53
/// <returns>DataSet集合</returns>
54
public DataSet ReturnDataSet(string strSQL)
55
...{
56
CheckConnection();
57
try
58
...{
59
SqlDataAdapter sda = new SqlDataAdapter(strSQL, con);
60
DataSet ds = new DataSet();
61
sda.Fill(ds);
62
return ds;
63
}
64
catch (Exception ex)
65
...{
66
throw new Exception(ex.Message);
67
}
68
finally
69
...{
70
con.Close();
71
}
72
}
73
#endregion
74
75
执行语句返回DataRow#region 执行语句返回DataRow
76
/**//// <summary>
77
/// 执行语句返回DataRow
78
/// </summary>
79
/// <param name="strSQL"> 要执行的SQL语句</param>
80
/// <returns>返回DataRow对象</returns>
81
public DataRow GetDataRow(string strSQL)
82
...{
83
CheckConnection();
84
try
85
...{
86
SqlDataAdapter sda = new SqlDataAdapter(strSQL, con);
87
DataSet ds = new DataSet();
88
sda.Fill(ds);
89
return ds.Tables[0].Rows[0];
90
}
91
catch (Exception ex)
92
...{
93
throw new Exception(ex.Message);
94
}
95
finally
96
...{
97
con.Close();
98
}
99
}
100
#endregion
101
102
执行SQL语句或存储过程的方法ExecuteNonQuery()#region 执行SQL语句或存储过程的方法ExecuteNonQuery()
103
/**//// <summary>
104
/// 执行存储过程或SQL语句的方法ExecuteNonQuery(),执行成功返回true,否则返回false
105
/// </summary>
106
/// <param name="IsPro">为true是执行存储过程,false执行SQL语句</param>
107
/// <param name="strSQL">要执行的SQL语句</param>
108
/// <returns>执行成功返回true,否则返回false</returns>
109
public bool ExecuteNonQuery(bool IsPro, string strSQL)
110
...{
111
CheckConnection();
112
try
113
...{
114
SqlCommand com = new SqlCommand(strSQL, con);
115
if (IsPro)
116
...{
117
com.CommandType = CommandType.StoredProcedure;
118
}
119
else
120
...{
121
com.CommandType = CommandType.Text;
122
}
123
com.CommandText = strSQL;
124
com.ExecuteNonQuery();
125
con.Close();
126
return true;
127
}
128
catch
129
...{
130
return false;
131
}
132
}
133
#endregion
134
135
执行SQL语句的方法ExecuteNonQuery()#region 执行SQL语句的方法ExecuteNonQuery()
136
/**//// <summary>
137
/// 执行SQL语句的方法ExecuteNonQuery()
138
/// </summary>
139
/// <param name="strSQL">要执行的SQL语句</param>
140
public void ExecuteNonQuery(string strSQL)
141
...{
142
CheckConnection();
143
try
144
...{
145
SqlCommand com = new SqlCommand(strSQL, con);
146
com.ExecuteNonQuery();
147
}
148
catch (Exception ex)
149
...{
150
throw new Exception(ex.Message);
151
}
152
finally
153
...{
154
con.Close();
155
}
156
}
157
#endregion
158
159
执行语句返回DataTable的方法#region 执行语句返回DataTable的方法
160
/**//// <summary>
161
/// 执行语句返回DataTable的方法
162
/// </summary>
163
/// <param name="strSQL">要执行的SQL语句</param>
164
/// <returns>返回DataTable对象</returns>
165
public DataTable ReturnTable(string strSQL)
166
...{
167
CheckConnection();
168
try
169
...{
170
SqlDataAdapter sda = new SqlDataAdapter(strSQL, con);
171
DataSet ds = new DataSet();
172
sda.Fill(ds);
173
return ds.Tables[0];
174
}
175
catch (Exception ex)
176
...{
177
throw new Exception(ex.Message);
178
}
179
finally
180
...{
181
con.Close();
182
}
183
}
184
#endregion
185
186
执行语句返回SqlDataReader对象#region 执行语句返回SqlDataReader对象
187
/**//// <summary>
188
/// 执行语句返回SqlDataReader对象
189
/// </summary>
190
/// <param name="strSQL">待执行的语句</param>
191
/// <returns>SqlDataReader对象</returns>
192
public SqlDataReader ReturnDataReader(string strSQL)
193
...{
194
CheckConnection();
195
try
196
...{
197
SqlCommand com = new SqlCommand(strSQL, con);
198
SqlDataReader myReader = com.ExecuteReader();
199
return myReader;
200
}
201
catch (Exception ex)
202
...{
203
throw new Exception(ex.Message);
204
}
205
finally
206
...{
207
}
208
}
209
#endregion
210
211
执行语句,返回该语句查询出的数据行的总数#region 执行语句,返回该语句查询出的数据行的总数
212
/**//// <summary>
213
/// 执行语句,返回该语句查询出的数据行的总数
214
/// </summary>
215
/// <param name="strSQL">要执行的SQL语句</param>
216
/// <returns>整型值--数据总行数</returns>
217
public int ReturnRowCount(string strSQL)
218
...{
219
CheckConnection();
220
try
221
...{
222
SqlDataAdapter da = new SqlDataAdapter(strSQL, con);
223
DataSet ds = new DataSet();
224
da.Fill(ds);
225
return ds.Tables[0].Rows.Count;
226
}
227
catch
228
...{
229
return 0;
230
}
231
}
232
#endregion
233
}