下面给出了一个C#操作MS SQL Server 数据库的通用类,通过该类可以对数据库进行任何操作,包括执行SQL语句、执行存储过程。以下是其详细实现过程,希望大家共同修改优化之。稍后将介绍如何使用它实现N层的程序设计。
配置web.config文件的链接参数
Code
1<appSettings>
2 <!--
3 connStr参数设置,事例说明:
4 (1)Sql server数据库,例如“server=local;database=test;uid=sa;pwd=;”
5 (2)Access数据库,例如“data\ex.mdb; user id='admin';Jet OLEDB:database password='admin';”
6 -->
7 <add key="connStr" value="server=127.0.0.1;database=DbName;uid=sa;pwd=;" />
8 </appSettings>
9
10
11
12C#代码
13
14
15
16using System;
17using System.Data;
18using System.Data.SqlClient;
19
20namespace Com.LXJ.Database
21{
22 /**//// <summary>
23 /// ConnDB 的摘要说明。
24 /// </summary>
25 public class ConnDB
26 {
27 protected SqlConnection Connection;
28 private string connectionString;
29
30 /**//// <summary>
31 /// 默认构造函数
32 /// </summary>
33 public ConnDB()
34 {
35 string connStr;
36 connStr = System.Configuration.ConfigurationSettings.AppSettings["connStr"].ToString();
37
38 connectionString = connStr;
39 Connection = new SqlConnection(connectionString);
40 }
41
42
43 /**//// <summary>
44 /// 带参数的构造函数
45 /// </summary>
46 /// <param name="newConnectionString">数据库联接字符串</param>
47 public ConnDB(string newConnectionString)
48 {
49 connectionString = newConnectionString;
50 Connection = new SqlConnection(connectionString);
51 }
52
53
54 /**//// <summary>
55 /// 完成SqlCommand对象的实例化
56 /// </summary>
57 /// <param name="storedProcName"></param>
58 /// <param name="parameters"></param>
59 /// <returns></returns>
60 private SqlCommand BuildCommand(string storedProcName,IDataParameter[] parameters)
61 {
62 SqlCommand command = BuildQueryCommand(storedProcName,parameters);
63 command.Parameters.Add(new SqlParameter("ReturnValue",SqlDbType.Int,4,ParameterDirection.ReturnValue,false,0,0,string.Empty,DataRowVersion.Default,null));
64 return command;
65 }
66
67
68 /**//// <summary>
69 /// 创建新的SQL命令对象(存储过程)
70 /// </summary>
71 /// <param name="storedProcName"></param>
72 /// <param name="parameters"></param>
73 /// <returns></returns>
74 private SqlCommand BuildQueryCommand(string storedProcName,IDataParameter[] parameters)
75 {
76 SqlCommand command = new SqlCommand(storedProcName,Connection);
77 command.CommandType = CommandType.StoredProcedure;
78 foreach (SqlParameter parameter in parameters)
79 {
80 command.Parameters.Add(parameter);
81 }
82 return command;
83 }
84
85
86 /**//// <summary>
87 /// 执行存储过程,无返回值
88 /// </summary>
89 /// <param name="storedProcName"></param>
90 /// <param name="parameters"></param>
91 public void ExecuteProcedure(string storedProcName,IDataParameter[] parameters)
92 {
93 Connection.Open();
94 SqlCommand command;
95 command=BuildQueryCommand(storedProcName,parameters);
96 command.ExecuteNonQuery();
97 Connection.Close();
98 }
99
100
101 /**//// <summary>
102 /// 执行存储过程,返回执行操作影响的行数目
103 /// </summary>
104 /// <param name="storedProcName"></param>
105 /// <param name="parameters"></param>
106 /// <param name="rowsAffected"></param>
107 /// <returns></returns>
108 public int RunProcedure(string storedProcName,IDataParameter[] parameters,out int rowsAffected)
109 {
110 int result;
111 Connection.Open();
112 SqlCommand command = BuildCommand(storedProcName,parameters);
113 rowsAffected = command.ExecuteNonQuery();
114 result = (int)command.Parameters["ReturnValue"].Value;
115 Connection.Close();
116
117 return result;
118 }
119
120
121 /**//// <summary>
122 /// 重载RunProcedure把执行存储过程的结果放在SqlDataReader中
123 /// </summary>
124 /// <param name="storedProcName"></param>
125 /// <param name="parameters"></param>
126 /// <returns></returns>
127 public SqlDataReader RunProcedure(string storedProcName,IDataParameter[] parameters)
128 {
129 SqlDataReader returnReader;
130 Connection.Open();
131 SqlCommand command = BuildQueryCommand(storedProcName,parameters);
132 command.CommandType = CommandType.StoredProcedure;
133 returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
134 return returnReader;
135 }
136
137
138 /**//// <summary>
139 /// 重载RunProcedure把执行存储过程的结果存储在DataSet中和表tableName为可选参数
140 /// </summary>
141 /// <param name="storedProcName"></param>
142 /// <param name="parameters"></param>
143 /// <param name="tableName"></param>
144 /// <returns></returns>
145 public DataSet RunProcedure(string storedProcName,IDataParameter[] parameters,params string[] tableName)
146 {
147 DataSet dataSet = new DataSet();
148 Connection.Open();
149 SqlDataAdapter sqlDA = new SqlDataAdapter();
150 sqlDA.SelectCommand = BuildQueryCommand(storedProcName,parameters);
151 string flag;
152 flag = "";
153 for(int i=0;i<tableName.Length;i++)
154 flag = tableName[i];
155 if (flag!="")
156 sqlDA.Fill(dataSet,tableName[0]);
157 else
158 sqlDA.Fill(dataSet);
159 Connection.Close();
160 return dataSet;
161 }
162
163
164 /**//// <summary>
165 /// 执行SQL语句,返回数据到DataSet中
166 /// </summary>
167 /// <param name="sql"></param>
168 /// <returns></returns>
169 public DataSet ReturnDataSet(string sql)
170 {
171 DataSet dataSet=new DataSet();
172 Connection.Open();
173 SqlDataAdapter sqlDA=new SqlDataAdapter(sql,Connection);
174 sqlDA.Fill(dataSet,"objDataSet");
175 Connection.Close();
176 return dataSet;
177 }
178
179
180 /**//// <summary>
181 /// 执行SQL语句,返回 DataReader
182 /// </summary>
183 /// <param name="sql"></param>
184 /// <returns></returns>
185 public SqlDataReader ReturnDataReader(String sql)
186 {
187 Connection.Open();
188 SqlCommand command = new SqlCommand(sql,Connection);
189 SqlDataReader dataReader = command.ExecuteReader();
190
191 return dataReader;
192 }
193
194
195 /**//// <summary>
196 /// 执行SQL语句,返回记录数
197 /// </summary>
198 /// <param name="sql"></param>
199 /// <returns></returns>
200 public int ReturnRecordCount(string sql)
201 {
202 int recordCount = 0;
203
204 Connection.Open();
205 SqlCommand command = new SqlCommand(sql,Connection);
206 SqlDataReader dataReader = command.ExecuteReader();
207
208 while(dataReader.Read())
209 {
210 recordCount++;
211 }
212 dataReader.Close();
213 Connection.Close();
214
215 return recordCount;
216 }
217
218
219 /**//// <summary>
220 /// 执行SQL语句
221 /// </summary>
222 /// <param name="sql"></param>
223 /// <returns></returns>
224 public bool EditDatabase(string sql)
225 {
226 bool successState = false;
227
228 Connection.Open();
229 SqlTransaction myTrans = Connection.BeginTransaction();
230 SqlCommand command = new SqlCommand(sql,Connection,myTrans);
231 try
232 {
233 command.ExecuteNonQuery();
234 myTrans.Commit();
235 successState = true;
236 }
237 catch
238 {
239 myTrans.Rollback();
240 }
241 finally
242 {
243 Connection.Close();
244 }
245
246 return successState;
247 }
248
249
250 /**//// <summary>
251 /// 关闭数据库联接
252 /// </summary>
253 public void Close()
254 {
255 Connection.Close();
256 }
257
258 }//end class
259}//end namespace
260
来源:https://www.cnblogs.com/dick/archive/2009/06/01/1493502.html