我的数据访问类-如何利用微软的DataAccess1
时间很短,把自己很久以前写的拿出来,与大家分享,希望大家不要笑话。
当时写这篇文章是自己想实现一种数据总线的技术,这是国家的863中的软件计划(打算用Java实现,这也是我后来从我们同学那里知道的)但我的同学选择了O/R 技术来实现。
这是一个系列的文章,帮助大家一步一步建立自己的数据库访问类,和有效的利用这个数据库访问类。鉴于时间和精力的问题,关于xml的这部分我没有代码实现。但可以利用里面反馈回来的DataSet来生成你自己的Xml数据。
下面是整个的数据访问源码,希望对大家有所帮助。
1
说明:该类对数据库连接字符串进行了加密处理
2
using System;
3
using System.Data;
4
using System.Data.SqlClient;
5
using System.Configuration;
6
using System.Security;
7
using System.Security.Cryptography;
8
using System.Text;
9
using System.IO;
10
namespace NewsManage.DataAccess
11
{
12
#region 数据访问操作类
13
public class Database : IDisposable
14
{
15
// connection to data source
16
//连接到数据源
17
private SqlConnection con;
18
private string connstr;
19
20
#region 打开连接操作
21
/// <summary>
22
/// Open the connection.
23
/// 打开connection连接
24
/// </summary>
25
private void Open()
26
{
27
// open connection
28
//打开连接
29
if (con == null)
30
{
31
Encrypt sqlEncrypt=new Encrypt();
32
//开个玩笑,呵呵 //sqlEncrypt.DecryptKey="像猪一样生活";
33
sqlEncrypt.DecryptKey="elqy";
34
sqlEncrypt.InputString=ConfigurationSettings.AppSettings["ConnectionString"];
35
sqlEncrypt.DesDecrypt();
36
connstr=sqlEncrypt.OutString;
37
con = new SqlConnection(connstr);
38
con.Open();
39
}
40
}
41
#endregion
42
#region 关闭连接操作
43
/// <summary>
44
/// Close the connection.
45
/// 关闭连接
46
/// </summary>
47
public void Close()
48
{
49
if (con != null)
50
con.Close();
51
}
52
#endregion
53
#region 释放资源操作
54
/// <summary>
55
/// Release resources.
56
/// 释放资源
57
/// </summary>
58
public void Dispose()
59
{
60
// make sure connection is closed
61
if (con != null)
62
{
63
con.Dispose();
64
con = null;
65
}
66
}
67
#endregion
68
#region 参数输入
69
/// <summary>
70
/// Make input param.
71
/// </summary>
72
/// <param name="ParamName">Name of param.</param>
73
/// <param name="DbType">Param type.</param>
74
/// <param name="Size">Param size.</param>
75
/// <param name="Value">Param value.</param>
76
/// <returns>New parameter.</returns>
77
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
78
{
79
80
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
81
}
82
#endregion
83
#region 参数输出
84
/// <summary>
85
/// Make input param.
86
/// </summary>
87
/// <param name="ParamName">Name of param.</param>
88
/// <param name="DbType">Param type.</param>
89
/// <param name="Size">Param size.</param>
90
/// <returns>New parameter.</returns>
91
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
92
{
93
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
94
}
95
#endregion
96
#region 参数添加,处理输入输出的参数
97
/// <summary>
98
/// Make stored procedure param.
99
/// </summary>
100
/// <param name="ParamName">Name of param.</param>
101
/// <param name="DbType">Param type.</param>
102
/// <param name="Size">Param size.</param>
103
/// <param name="Direction">Parm direction.</param>
104
/// <param name="Value">Param value.</param>
105
/// <returns>New parameter.</returns>
106
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
107
{
108
SqlParameter param;
109
110
if(Size > 0)
111
param = new SqlParameter(ParamName, DbType, Size);
112
else
113
param = new SqlParameter(ParamName, DbType);
114
115
param.Direction = Direction;
116
if (!(Direction == ParameterDirection.Output && Value == null))
117
param.Value = Value;
118
119
return param;
120
}
121
122
#endregion
123
#region 命令的创建,设定command的类型为存储过程
124
/// <summary>
125
/// Create command object used to call stored procedure.
126
/// </summary>
127
/// <param name="procName">Name of stored procedure.</param>
128
/// <param name="prams">Params to stored procedure.</param>
129
/// <returns>Command object.</returns>
130
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
131
{
132
// make sure connection is open
133
Open();
134
135
//command = new SqlCommand( sprocName, new SqlConnection( ConfigManager.DALConnectionString ) );
136
SqlCommand cmd = new SqlCommand(procName, con);
137
cmd.CommandType = CommandType.StoredProcedure;
138
139
// add proc parameters
140
if (prams != null)
141
{
142
foreach (SqlParameter parameter in prams)
143
cmd.Parameters.Add(parameter);
144
}
145
146
// return param
147
cmd.Parameters.Add(
148
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
149
ParameterDirection.ReturnValue, false, 0, 0,
150
string.Empty, DataRowVersion.Default, null));
151
152
return cmd;
153
}
154
#endregion
155
#region 方法RunProc,(存储过程名),无返回结果!
156
/// <summary>
157
/// Run stored procedure.
158
/// 适用于那些不需要返回值得存储过程
159
/// </summary>
160
/// <param name="procName">Name of stored procedure.</param>
161
/// <returns>Stored procedure return value.</returns>
162
public int RunProc(string procName)
163
{
164
SqlCommand cmd = CreateCommand(procName, null);
165
cmd.ExecuteNonQuery();
166
this.Close();
167
return (int)cmd.Parameters["ReturnValue"].Value;
168
}
169
#endregion
170
#region 重载1,(存储过程名,参数列表),无返回结果
171
/// <summary>
172
/// Run stored procedure.
173
/// </summary>
174
/// <param name="procName">Name of stored procedure.</param>
175
/// <param name="prams">Stored procedure params.</param>
176
/// <returns>Stored procedure return value.</returns>
177
public int RunProc(string procName, SqlParameter[] prams)
178
{
179
SqlCommand cmd = CreateCommand(procName, prams);
180
cmd.ExecuteNonQuery();
181
this.Close();
182
return (int)cmd.Parameters["ReturnValue"].Value;
183
}
184
#endregion
185
#region 重载2,(存储过程名,返回dataReader)
186
/// <summary>
187
/// Run stored procedure.
188
/// </summary>
189
/// <param name="procName">Name of stored procedure.</param>
190
/// <param name="dataReader">Return result of procedure.</param>
191
public void RunProc(string procName, out SqlDataReader dataReader)
192
{
193
SqlCommand cmd = CreateCommand(procName, null);
194
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
195
}
196
#endregion
197
#region 重载3,(存储过程名,参数列表,返回dataReader)
198
/// <summary>
199
/// Run stored procedure.
200
/// </summary>
201
/// <param name="procName">Name of stored procedure.</param>
202
/// <param name="prams">Stored procedure params.</param>
203
/// <param name="dataReader">Return result of procedure.</param>
204
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
205
{
206
SqlCommand cmd = CreateCommand(procName, prams);
207
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
208
}
209
#endregion
210
#region 重载4,(存储过程名,参数列表,返回dataSet)
211
/// <summary>
212
/// Run stored procedure.
213
/// </summary>
214
/// <param name="procName">Name of stored procedure.</param>
215
/// <param name="prams">Stored procedure params.</param>
216
/// <param name="dataset">Return result of procedure.</param>
217
public DataSet RunProcds(string procName, SqlParameter[] prams)
218
{
219
SqlCommand cmd = CreateCommand(procName, prams);
220
DataSet dataset=new DataSet();
221
SqlDataAdapter sqladapter=new SqlDataAdapter();
222
sqladapter.SelectCommand=cmd;
223
sqladapter.Fill(dataset,procName);
224
con.Close();
225
return dataset;
226
//dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
227
}
228
#endregion
229
#region 重载5,(存储过程名,返回dataSet)
230
/// <summary>
231
/// Run stored procedure.
232
/// </summary>
233
/// <param name="procName">Name of stored procedure.</param>
234
/// <param name="prams">Stored procedure params.</param>
235
/// <param name="dataset">Return result of procedure.</param>
236
public DataSet RunProcds(string procName)
237
{
238
SqlCommand cmd = CreateCommand(procName,null);
239
DataSet dataset=new DataSet();
240
SqlDataAdapter sqladapter=new SqlDataAdapter();
241
sqladapter.SelectCommand=cmd;
242
sqladapter.Fill(dataset,procName);
243
con.Close();
244
return dataset;
245
//dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
246
}
247
#endregion
248
}
249
250
#endregion
251
#region 加密类,放在一起是因为主要为数据库连接字符串服务。
252
public class Encrypt
253
{
254
#region 私有成员
255
/// <summary>
256
/// 输入字符串
257
/// </summary>
258
private string inputString=null;
259
/// <summary>
260
/// 输出字符串
261
/// </summary>
262
private string outString=null;
263
/// <summary>
264
/// 输入文件路径
265
/// </summary>
266
private string inputFilePath=null;
267
/// <summary>
268
/// 输出文件路径
269
/// </summary>
270
private string outFilePath=null;
271
/// <summary>
272
/// 加密密钥
273
/// </summary>
274
private string encryptKey=null;
275
/// <summary>
276
/// 解密密钥
277
/// </summary>
278
private string decryptKey=null;
279
/// <summary>
280
/// 提示信息
281
/// </summary>
282
private string noteMessage=null;
283
#endregion
284
#region 公共属性
285
/// <summary>
286
/// 输入字符串
287
/// </summary>
288
public string InputString
289
{
290
get{return inputString;}
291
set{inputString=value;}
292
}
293
/// <summary>
294
/// 输出字符串
295
/// </summary>
296
public string OutString
297
{
298
get{return outString;}
299
set{outString=value;}
300
}
301
/// <summary>
302
/// 输入文件路径
303
/// </summary>
304
public string InputFilePath
305
{
306
get{return inputFilePath;}
307
set{inputFilePath=value;}
308
}
309
/// <summary>
310
/// 输出文件路径
311
/// </summary>
312
public string OutFilePath
313
{
314
get{return outFilePath;}
315
set{outFilePath=value;}
316
}
317
/// <summary>
318
/// 加密密钥
319
/// </summary>
320
public string EncryptKey
321
{
322
get{return encryptKey;}
323
set{encryptKey=value;}
324
}
325
/// <summary>
326
/// 解密密钥
327
/// </summary>
328
public string DecryptKey
329
{
330
get{return decryptKey;}
331
set{decryptKey=value;}
332
}
333
/// <summary>
334
/// 错误信息
335
/// </summary>
336
public string NoteMessage
337
{
338
get{return noteMessage;}
339
set{noteMessage=value;}
340
}
341
#endregion
342
#region DES加密字符串
343
/// <summary>
344
/// 加密字符串
345
/// 注意:密钥必须为8位
346
/// </summary>
347
/// <param name="strText">字符串</param>
348
/// <param name="encryptKey">密钥</param>
349
public void DesEncrypt()
350
{
351
byte[] byKey=null;
352
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
353
try
354
{
355
byKey = System.Text.Encoding.UTF8.GetBytes(this.encryptKey.Substring(0,8));
356
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
357
byte[] inputByteArray = Encoding.UTF8.GetBytes(this.inputString);
358
MemoryStream ms = new MemoryStream();
359
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;
360
cs.Write(inputByteArray, 0, inputByteArray.Length);
361
cs.FlushFinalBlock();
362
this.outString=Convert.ToBase64String(ms.ToArray());
363
}
364
catch(System.Exception error)
365
{
366
this.noteMessage=error.Message;
367
}
368
}
369
#endregion
370
#region DES解密字符串
371
/// <summary>
372
/// 解密字符串
373
/// </summary>
374
/// <param name="this.inputString">加了密的字符串</param>
375
/// <param name="decryptKey">密钥</param>
376
public void DesDecrypt()
377
{
378
byte[] byKey = null;
379
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
380
byte[] inputByteArray = new Byte[this.inputString.Length];
381
try
382
{
383
byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0,8));
384
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
385
inputByteArray = Convert.FromBase64String(this.inputString);
386
MemoryStream ms = new MemoryStream();
387
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
388
cs.Write(inputByteArray, 0, inputByteArray.Length);
389
cs.FlushFinalBlock();
390
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
391
this.outString=encoding.GetString(ms.ToArray());
392
}
393
catch(System.Exception error)
394
{
395
this.noteMessage=error.Message;
396
}
397
}
398
#endregion
399
#region DES加密文件
400
/// <summary>
401
/// DES加密文件
402
/// </summary>
403
/// <param name="this.inputFilePath">源文件路径</param>
404
/// <param name="this.outFilePath">输出文件路径</param>
405
/// <param name="encryptKey">密钥</param>
406
public void FileDesEncrypt()
407
{
408
byte[] byKey=null;
409
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
410
try
411
{
412
byKey = System.Text.Encoding.UTF8.GetBytes(this.encryptKey.Substring(0,8));
413
FileStream fin = new FileStream(this.inputFilePath, FileMode.Open, FileAccess.Read);
414
FileStream fout = new FileStream(this.outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
415
fout.SetLength(0);
416
//Create variables to help with read and write.
417
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
418
long rdlen = 0; //This is the total number of bytes written.
419
long totlen = fin.Length; //This is the total length of the input file.
420
int len; //This is the number of bytes to be written at a time.
421
DES des = new DESCryptoServiceProvider();
422
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
423
424
425
//Read from the input file, then encrypt and write to the output file.
426
while(rdlen < totlen)
427
{
428
len = fin.Read(bin, 0, 100);
429
encStream.Write(bin, 0, len);
430
rdlen = rdlen + len;
431
}
432
433
encStream.Close();
434
fout.Close();
435
fin.Close();
436
437
438
439
}
440
catch(System.Exception error)
441
{
442
this.noteMessage=error.Message.ToString();
443
444
}
445
}
446
#endregion
447
#region DES解密文件
448
/// <summary>
449
/// 解密文件
450
/// </summary>
451
/// <param name="this.inputFilePath">加密了的文件路径</param>
452
/// <param name="this.outFilePath">输出文件路径</param>
453
/// <param name="decryptKey">密钥</param>
454
public void FileDesDecrypt()
455
{
456
byte[] byKey = null;
457
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
458
try
459
{
460
byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0,8));
461
FileStream fin = new FileStream(this.inputFilePath, FileMode.Open, FileAccess.Read);
462
FileStream fout = new FileStream(this.outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
463
fout.SetLength(0);
464
//Create variables to help with read and write.
465
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
466
long rdlen = 0; //This is the total number of bytes written.
467
long totlen = fin.Length; //This is the total length of the input file.
468
int len; //This is the number of bytes to be written at a time.
469
DES des = new DESCryptoServiceProvider();
470
CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
471
472
473
//Read from the input file, then encrypt and write to the output file.
474
while(rdlen < totlen)
475
{
476
len = fin.Read(bin, 0, 100);
477
encStream.Write(bin, 0, len);
478
rdlen = rdlen + len;
479
}
480
481
encStream.Close();
482
fout.Close();
483
fin.Close();
484
}
485
catch(System.Exception error)
486
{
487
this.noteMessage=error.Message.ToString();
488
}
489
}
490
#endregion
491
#region MD5
492
/// <summary>
493
/// MD5加密
494
/// </summary>
495
/// <param name="strText">text</param>
496
/// <returns>md5 Encrypt string</returns>
497
public void MD5Encrypt()
498
{
499
MD5 md5 = new MD5CryptoServiceProvider();
500
//这里使用的参数为inputString
501
byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(this.inputString));
502
//输出的参数为outString
503
this.outString=System.Text.Encoding.Default.GetString(result);
504
}
505
#endregion
506
}
507
#endregion
508
}
说明:该类对数据库连接字符串进行了加密处理2
using System;3
using System.Data;4
using System.Data.SqlClient;5
using System.Configuration;6
using System.Security;7
using System.Security.Cryptography;8
using System.Text;9
using System.IO;10
namespace NewsManage.DataAccess11
{12
#region 数据访问操作类13
public class Database : IDisposable 14
{15
// connection to data source16
//连接到数据源17
private SqlConnection con;18
private string connstr;19

20
#region 打开连接操作21
/// <summary>22
/// Open the connection.23
/// 打开connection连接24
/// </summary>25
private void Open() 26
{27
// open connection28
//打开连接29
if (con == null) 30
{31
Encrypt sqlEncrypt=new Encrypt();32
//开个玩笑,呵呵 //sqlEncrypt.DecryptKey="像猪一样生活";33
sqlEncrypt.DecryptKey="elqy";34
sqlEncrypt.InputString=ConfigurationSettings.AppSettings["ConnectionString"];35
sqlEncrypt.DesDecrypt();36
connstr=sqlEncrypt.OutString;37
con = new SqlConnection(connstr);38
con.Open();39
} 40
}41
#endregion42
#region 关闭连接操作43
/// <summary>44
/// Close the connection.45
/// 关闭连接46
/// </summary>47
public void Close() 48
{49
if (con != null)50
con.Close();51
}52
#endregion53
#region 释放资源操作54
/// <summary>55
/// Release resources.56
/// 释放资源57
/// </summary>58
public void Dispose() 59
{60
// make sure connection is closed61
if (con != null) 62
{63
con.Dispose();64
con = null;65
} 66
}67
#endregion68
#region 参数输入69
/// <summary>70
/// Make input param.71
/// </summary>72
/// <param name="ParamName">Name of param.</param>73
/// <param name="DbType">Param type.</param>74
/// <param name="Size">Param size.</param>75
/// <param name="Value">Param value.</param>76
/// <returns>New parameter.</returns>77
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value) 78
{79

80
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);81
} 82
#endregion83
#region 参数输出84
/// <summary>85
/// Make input param.86
/// </summary>87
/// <param name="ParamName">Name of param.</param>88
/// <param name="DbType">Param type.</param>89
/// <param name="Size">Param size.</param>90
/// <returns>New parameter.</returns>91
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size) 92
{93
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);94
} 95
#endregion96
#region 参数添加,处理输入输出的参数97
/// <summary>98
/// Make stored procedure param.99
/// </summary>100
/// <param name="ParamName">Name of param.</param>101
/// <param name="DbType">Param type.</param>102
/// <param name="Size">Param size.</param>103
/// <param name="Direction">Parm direction.</param>104
/// <param name="Value">Param value.</param>105
/// <returns>New parameter.</returns>106
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value) 107
{108
SqlParameter param;109

110
if(Size > 0)111
param = new SqlParameter(ParamName, DbType, Size);112
else113
param = new SqlParameter(ParamName, DbType);114

115
param.Direction = Direction;116
if (!(Direction == ParameterDirection.Output && Value == null))117
param.Value = Value;118

119
return param;120
}121

122
#endregion123
#region 命令的创建,设定command的类型为存储过程124
/// <summary>125
/// Create command object used to call stored procedure.126
/// </summary>127
/// <param name="procName">Name of stored procedure.</param>128
/// <param name="prams">Params to stored procedure.</param>129
/// <returns>Command object.</returns>130
private SqlCommand CreateCommand(string procName, SqlParameter[] prams) 131
{132
// make sure connection is open133
Open();134

135
//command = new SqlCommand( sprocName, new SqlConnection( ConfigManager.DALConnectionString ) );136
SqlCommand cmd = new SqlCommand(procName, con);137
cmd.CommandType = CommandType.StoredProcedure;138

139
// add proc parameters140
if (prams != null) 141
{142
foreach (SqlParameter parameter in prams)143
cmd.Parameters.Add(parameter);144
}145
146
// return param147
cmd.Parameters.Add(148
new SqlParameter("ReturnValue", SqlDbType.Int, 4,149
ParameterDirection.ReturnValue, false, 0, 0,150
string.Empty, DataRowVersion.Default, null));151

152
return cmd;153
}154
#endregion155
#region 方法RunProc,(存储过程名),无返回结果!156
/// <summary>157
/// Run stored procedure.158
/// 适用于那些不需要返回值得存储过程159
/// </summary>160
/// <param name="procName">Name of stored procedure.</param>161
/// <returns>Stored procedure return value.</returns>162
public int RunProc(string procName) 163
{164
SqlCommand cmd = CreateCommand(procName, null);165
cmd.ExecuteNonQuery();166
this.Close();167
return (int)cmd.Parameters["ReturnValue"].Value;168
}169
#endregion170
#region 重载1,(存储过程名,参数列表),无返回结果171
/// <summary>172
/// Run stored procedure.173
/// </summary>174
/// <param name="procName">Name of stored procedure.</param>175
/// <param name="prams">Stored procedure params.</param>176
/// <returns>Stored procedure return value.</returns>177
public int RunProc(string procName, SqlParameter[] prams) 178
{179
SqlCommand cmd = CreateCommand(procName, prams);180
cmd.ExecuteNonQuery();181
this.Close();182
return (int)cmd.Parameters["ReturnValue"].Value;183
}184
#endregion185
#region 重载2,(存储过程名,返回dataReader)186
/// <summary>187
/// Run stored procedure.188
/// </summary>189
/// <param name="procName">Name of stored procedure.</param>190
/// <param name="dataReader">Return result of procedure.</param>191
public void RunProc(string procName, out SqlDataReader dataReader) 192
{193
SqlCommand cmd = CreateCommand(procName, null);194
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);195
}196
#endregion197
#region 重载3,(存储过程名,参数列表,返回dataReader)198
/// <summary>199
/// Run stored procedure.200
/// </summary>201
/// <param name="procName">Name of stored procedure.</param>202
/// <param name="prams">Stored procedure params.</param>203
/// <param name="dataReader">Return result of procedure.</param>204
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader) 205
{206
SqlCommand cmd = CreateCommand(procName, prams);207
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);208
}209
#endregion 210
#region 重载4,(存储过程名,参数列表,返回dataSet)211
/// <summary>212
/// Run stored procedure.213
/// </summary>214
/// <param name="procName">Name of stored procedure.</param>215
/// <param name="prams">Stored procedure params.</param>216
/// <param name="dataset">Return result of procedure.</param>217
public DataSet RunProcds(string procName, SqlParameter[] prams) 218
{219
SqlCommand cmd = CreateCommand(procName, prams);220
DataSet dataset=new DataSet();221
SqlDataAdapter sqladapter=new SqlDataAdapter();222
sqladapter.SelectCommand=cmd;223
sqladapter.Fill(dataset,procName);224
con.Close();225
return dataset;226
//dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);227
}228
#endregion229
#region 重载5,(存储过程名,返回dataSet)230
/// <summary>231
/// Run stored procedure.232
/// </summary>233
/// <param name="procName">Name of stored procedure.</param>234
/// <param name="prams">Stored procedure params.</param>235
/// <param name="dataset">Return result of procedure.</param>236
public DataSet RunProcds(string procName) 237
{238
SqlCommand cmd = CreateCommand(procName,null);239
DataSet dataset=new DataSet();240
SqlDataAdapter sqladapter=new SqlDataAdapter();241
sqladapter.SelectCommand=cmd;242
sqladapter.Fill(dataset,procName);243
con.Close();244
return dataset;245
//dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);246
}247
#endregion248
}249

250
#endregion251
#region 加密类,放在一起是因为主要为数据库连接字符串服务。252
public class Encrypt253
{254
#region 私有成员255
/// <summary>256
/// 输入字符串257
/// </summary>258
private string inputString=null;259
/// <summary>260
/// 输出字符串261
/// </summary>262
private string outString=null;263
/// <summary>264
/// 输入文件路径265
/// </summary>266
private string inputFilePath=null;267
/// <summary>268
/// 输出文件路径269
/// </summary>270
private string outFilePath=null;271
/// <summary>272
/// 加密密钥273
/// </summary>274
private string encryptKey=null;275
/// <summary>276
/// 解密密钥277
/// </summary>278
private string decryptKey=null;279
/// <summary>280
/// 提示信息281
/// </summary>282
private string noteMessage=null;283
#endregion284
#region 公共属性285
/// <summary>286
/// 输入字符串287
/// </summary>288
public string InputString289
{290
get{return inputString;}291
set{inputString=value;}292
}293
/// <summary>294
/// 输出字符串295
/// </summary>296
public string OutString297
{298
get{return outString;}299
set{outString=value;}300
}301
/// <summary>302
/// 输入文件路径303
/// </summary>304
public string InputFilePath305
{306
get{return inputFilePath;}307
set{inputFilePath=value;}308
}309
/// <summary>310
/// 输出文件路径311
/// </summary>312
public string OutFilePath313
{314
get{return outFilePath;}315
set{outFilePath=value;}316
}317
/// <summary>318
/// 加密密钥319
/// </summary>320
public string EncryptKey321
{322
get{return encryptKey;}323
set{encryptKey=value;}324
}325
/// <summary>326
/// 解密密钥327
/// </summary>328
public string DecryptKey329
{330
get{return decryptKey;}331
set{decryptKey=value;}332
}333
/// <summary>334
/// 错误信息335
/// </summary>336
public string NoteMessage337
{338
get{return noteMessage;}339
set{noteMessage=value;}340
}341
#endregion342
#region DES加密字符串343
/// <summary>344
/// 加密字符串345
/// 注意:密钥必须为8位346
/// </summary>347
/// <param name="strText">字符串</param>348
/// <param name="encryptKey">密钥</param>349
public void DesEncrypt()350
{351
byte[] byKey=null; 352
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};353
try354
{355
byKey = System.Text.Encoding.UTF8.GetBytes(this.encryptKey.Substring(0,8));356
DESCryptoServiceProvider des = new DESCryptoServiceProvider();357
byte[] inputByteArray = Encoding.UTF8.GetBytes(this.inputString);358
MemoryStream ms = new MemoryStream();359
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;360
cs.Write(inputByteArray, 0, inputByteArray.Length);361
cs.FlushFinalBlock();362
this.outString=Convert.ToBase64String(ms.ToArray());363
}364
catch(System.Exception error)365
{366
this.noteMessage=error.Message;367
}368
}369
#endregion370
#region DES解密字符串371
/// <summary>372
/// 解密字符串373
/// </summary>374
/// <param name="this.inputString">加了密的字符串</param>375
/// <param name="decryptKey">密钥</param>376
public void DesDecrypt()377
{378
byte[] byKey = null; 379
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; 380
byte[] inputByteArray = new Byte[this.inputString.Length];381
try382
{383
byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0,8));384
DESCryptoServiceProvider des = new DESCryptoServiceProvider();385
inputByteArray = Convert.FromBase64String(this.inputString);386
MemoryStream ms = new MemoryStream();387
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);388
cs.Write(inputByteArray, 0, inputByteArray.Length);389
cs.FlushFinalBlock();390
System.Text.Encoding encoding = new System.Text.UTF8Encoding();391
this.outString=encoding.GetString(ms.ToArray());392
}393
catch(System.Exception error)394
{395
this.noteMessage=error.Message;396
}397
}398
#endregion399
#region DES加密文件400
/// <summary>401
/// DES加密文件402
/// </summary>403
/// <param name="this.inputFilePath">源文件路径</param>404
/// <param name="this.outFilePath">输出文件路径</param>405
/// <param name="encryptKey">密钥</param>406
public void FileDesEncrypt()407
{408
byte[] byKey=null; 409
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};410
try411
{412
byKey = System.Text.Encoding.UTF8.GetBytes(this.encryptKey.Substring(0,8));413
FileStream fin = new FileStream(this.inputFilePath, FileMode.Open, FileAccess.Read);414
FileStream fout = new FileStream(this.outFilePath, FileMode.OpenOrCreate, FileAccess.Write);415
fout.SetLength(0);416
//Create variables to help with read and write.417
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.418
long rdlen = 0; //This is the total number of bytes written.419
long totlen = fin.Length; //This is the total length of the input file.420
int len; //This is the number of bytes to be written at a time.421
DES des = new DESCryptoServiceProvider(); 422
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);423

424

425
//Read from the input file, then encrypt and write to the output file.426
while(rdlen < totlen)427
{428
len = fin.Read(bin, 0, 100);429
encStream.Write(bin, 0, len);430
rdlen = rdlen + len; 431
}432

433
encStream.Close(); 434
fout.Close();435
fin.Close(); 436

437

438

439
}440
catch(System.Exception error)441
{442
this.noteMessage=error.Message.ToString();443

444
}445
}446
#endregion447
#region DES解密文件448
/// <summary>449
/// 解密文件450
/// </summary>451
/// <param name="this.inputFilePath">加密了的文件路径</param>452
/// <param name="this.outFilePath">输出文件路径</param>453
/// <param name="decryptKey">密钥</param>454
public void FileDesDecrypt()455
{456
byte[] byKey = null; 457
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; 458
try459
{460
byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0,8)); 461
FileStream fin = new FileStream(this.inputFilePath, FileMode.Open, FileAccess.Read);462
FileStream fout = new FileStream(this.outFilePath, FileMode.OpenOrCreate, FileAccess.Write);463
fout.SetLength(0);464
//Create variables to help with read and write.465
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.466
long rdlen = 0; //This is the total number of bytes written.467
long totlen = fin.Length; //This is the total length of the input file.468
int len; //This is the number of bytes to be written at a time.469
DES des = new DESCryptoServiceProvider(); 470
CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);471

472

473
//Read from the input file, then encrypt and write to the output file.474
while(rdlen < totlen)475
{476
len = fin.Read(bin, 0, 100);477
encStream.Write(bin, 0, len);478
rdlen = rdlen + len; 479
}480

481
encStream.Close(); 482
fout.Close();483
fin.Close(); 484
}485
catch(System.Exception error)486
{487
this.noteMessage=error.Message.ToString();488
}489
}490
#endregion491
#region MD5492
/// <summary>493
/// MD5加密494
/// </summary>495
/// <param name="strText">text</param>496
/// <returns>md5 Encrypt string</returns>497
public void MD5Encrypt()498
{499
MD5 md5 = new MD5CryptoServiceProvider();500
//这里使用的参数为inputString501
byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(this.inputString)); 502
//输出的参数为outString503
this.outString=System.Text.Encoding.Default.GetString(result);504
}505
#endregion506
}507
#endregion508
}希望上面的代码大家都能够读懂,主要是我增加了数据访问类中的RunProcds的方法,这样可以反馈回大家需要的DataSet来了。另外,顺便添加了一个数据加密类。
这次的数据访问就到这里,下次我们来实现数据监视的实现,依然是用到这个类,只需要一些修改就可以满足我们的需求了。如果大家喜欢我的文章请留言。
声明:这段代码完全可以供大家无偿使用,但如果您在开发中想直接使用这段代码请您写信通知我。



浙公网安备 33010602011771号