C#实战项目第二集
第二讲只要向大家介绍用户登录模块的实现,在这一讲中主要向大家介绍了实体类、简单工厂类、加密类的使用,由于在项目中这些都是基本类库框架所以没有再做太多的介绍,所以我将这些类库的实现罗列出来供大家学习参考。
以下是我的讲座的下载地址:
http://www.verycd.com/topics/121681/
http://www.verycd.com/topics/34358/
http://www.verycd.com/topics/87555/

1
2
using System;
3
using System.Collections.Generic;
4
using System.Text;
5
using System.Data;
6
using System.Configuration;
7
using System.Data.Common;
8
using System.Data.SqlClient;
9
using System.Data.OleDb;
10
using System.Data.Odbc;
11
using System.Data.OracleClient;
12
using System.IO;
13
14
namespace WebHelper.DB
15
{
16
/// <summary>
17
/// DatabaseHelper是一个对数据库的封装库,主要针对小型数据库开发
18
/// 有着很好的跨数据库功能,但针对专有数据库优化不足
19
/// </summary>
20
public class DatabaseHelper : IDisposable
21
{
22
private string strConnectionString;
23
private DbConnection objConnection;
24
private DbCommand objCommand;
25
private DbProviderFactory objFactory = null;
26
private bool boolHandleErrors;
27
private string strLastError;
28
private bool boolLogError;
29
private string strLogFile;
30
31
/// <summary>
32
/// Initializes a new instance of the <see cref="DatabaseHelper"/> class.
33
/// </summary>
34
/// <param name="connectionstring">The connectionstring.</param>
35
/// <param name="provider">The provider.</param>
36
public DatabaseHelper(string connectionstring, Providers provider)
37
{
38
strConnectionString = connectionstring;
39
switch (provider)
40
{
41
case Providers.SqlServer:
42
objFactory = SqlClientFactory.Instance;
43
break;
44
case Providers.OleDb:
45
objFactory = OleDbFactory.Instance;
46
break;
47
case Providers.Oracle:
48
objFactory = OracleClientFactory.Instance;
49
break;
50
case Providers.ODBC:
51
objFactory = OdbcFactory.Instance;
52
break;
53
case Providers.ConfigDefined:
54
string providername = ConfigurationManager.ConnectionStrings["connectionstring"].ProviderName;
55
switch (providername)
56
{
57
case "System.Data.SqlClient":
58
objFactory = SqlClientFactory.Instance;
59
break;
60
case "System.Data.OleDb":
61
objFactory = OleDbFactory.Instance;
62
break;
63
case "System.Data.OracleClient":
64
objFactory = OracleClientFactory.Instance;
65
break;
66
case "System.Data.Odbc":
67
objFactory = OdbcFactory.Instance;
68
break;
69
}
70
break;
71
72
}
73
objConnection = objFactory.CreateConnection();
74
objCommand = objFactory.CreateCommand();
75
76
objConnection.ConnectionString = strConnectionString;
77
objCommand.Connection = objConnection;
78
}
79
80
/// <summary>
81
/// Initializes a new instance of the <see cref="DatabaseHelper"/> class.
82
/// </summary>
83
/// <param name="provider">The provider.</param>
84
public DatabaseHelper(Providers provider)
85
: this(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString, provider)
86
{
87
}
88
89
/// <summary>
90
/// Initializes a new instance of the <see cref="DatabaseHelper"/> class.
91
/// </summary>
92
/// <param name="connectionstring">The connectionstring.</param>
93
public DatabaseHelper(string connectionstring)
94
: this(connectionstring, Providers.SqlServer)
95
{
96
}
97
98
/// <summary>
99
/// Initializes a new instance of the <see cref="DatabaseHelper"/> class.
100
/// </summary>
101
public DatabaseHelper()
102
: this(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString, Providers.ConfigDefined)
103
{
104
}
105
106
/// <summary>
107
/// Gets or sets a value indicating whether [handle errors].
108
/// </summary>
109
/// <value><c>true</c> if [handle errors]; otherwise, <c>false</c>.</value>
110
public bool HandleErrors
111
{
112
get
113
{
114
return boolHandleErrors;
115
}
116
set
117
{
118
boolHandleErrors = value;
119
}
120
}
121
122
/// <summary>
123
/// Gets the last error.
124
/// </summary>
125
/// <value>The last error.</value>
126
public string LastError
127
{
128
get
129
{
130
return strLastError;
131
}
132
}
133
134
/// <summary>
135
/// Gets or sets a value indicating whether [log errors].
136
/// </summary>
137
/// <value><c>true</c> if [log errors]; otherwise, <c>false</c>.</value>
138
public bool LogErrors
139
{
140
get
141
{
142
return boolLogError;
143
}
144
set
145
{
146
boolLogError = value;
147
}
148
}
149
150
/// <summary>
151
/// Gets or sets the log file.
152
/// </summary>
153
/// <value>The log file.</value>
154
public string LogFile
155
{
156
get
157
{
158
return strLogFile;
159
}
160
set
161
{
162
strLogFile = value;
163
}
164
}
165
166
/// <summary>
167
/// Adds the parameter.
168
/// </summary>
169
/// <param name="name">The name.</param>
170
/// <param name="value">The value.</param>
171
/// <returns></returns>
172
public int AddParameter(string name, object value)
173
{
174
DbParameter p = objFactory.CreateParameter();
175
p.ParameterName = name;
176
p.Value = value;
177
return objCommand.Parameters.Add(p);
178
}
179
180
/// <summary>
181
/// Adds the parameter.
182
/// </summary>
183
/// <param name="parameter">The parameter.</param>
184
/// <returns></returns>
185
public int AddParameter(DbParameter parameter)
186
{
187
return objCommand.Parameters.Add(parameter);
188
}
189
190
/// <summary>
191
/// Gets the command.
192
/// </summary>
193
/// <value>The command.</value>
194
public DbCommand Command
195
{
196
get
197
{
198
return objCommand;
199
}
200
}
201
202
/// <summary>
203
/// Begins the transaction.
204
/// </summary>
205
public void BeginTransaction()
206
{
207
if (objConnection.State == System.Data.ConnectionState.Closed)
208
{
209
objConnection.Open();
210
}
211
objCommand.Transaction = objConnection.BeginTransaction();
212
}
213
214
/// <summary>
215
/// Commits the transaction.
216
/// </summary>
217
public void CommitTransaction()
218
{
219
objCommand.Transaction.Commit();
220
objConnection.Close();
221
}
222
223
/// <summary>
224
/// Rollbacks the transaction.
225
/// </summary>
226
public void RollbackTransaction()
227
{
228
objCommand.Transaction.Rollback();
229
objConnection.Close();
230
}
231
232
/// <summary>
233
/// Executes the non query.
234
/// </summary>
235
/// <param name="query">The query.</param>
236
/// <returns></returns>
237
public int ExecuteNonQuery(string query)
238
{
239
return ExecuteNonQuery(query, CommandType.Text, ConnectionState.CloseOnExit);
240
}
241
242
/// <summary>
243
/// Executes the non query.
244
/// </summary>
245
/// <param name="query">The query.</param>
246
/// <param name="commandtype">The commandtype.</param>
247
/// <returns></returns>
248
public int ExecuteNonQuery(string query, CommandType commandtype)
249
{
250
return ExecuteNonQuery(query, commandtype, ConnectionState.CloseOnExit);
251
}
252
253
/// <summary>
254
/// Executes the non query.
255
/// </summary>
256
/// <param name="query">The query.</param>
257
/// <param name="connectionstate">The connectionstate.</param>
258
/// <returns></returns>
259
public int ExecuteNonQuery(string query, ConnectionState connectionstate)
260
{
261
return ExecuteNonQuery(query, CommandType.Text, connectionstate);
262
}
263
264
/// <summary>
265
/// Executes the non query.
266
/// </summary>
267
/// <param name="query">The query.</param>
268
/// <param name="commandtype">The commandtype.</param>
269
/// <param name="connectionstate">The connectionstate.</param>
270
/// <returns></returns>
271
public int ExecuteNonQuery(string query, CommandType commandtype, ConnectionState connectionstate)
272
{
273
objCommand.CommandText = query;
274
objCommand.CommandType = commandtype;
275
int i = -1;
276
try
277
{
278
if (objConnection.State == System.Data.ConnectionState.Closed)
279
{
280
objConnection.Open();
281
}
282
i = objCommand.ExecuteNonQuery();
283
}
284
catch (Exception ex)
285
{
286
HandleExceptions(ex);
287
}
288
finally
289
{
290
objCommand.Parameters.Clear();
291
if (connectionstate == ConnectionState.CloseOnExit)
292
{
293
objConnection.Close();
294
}
295
}
296
297
return i;
298
}
299
300
/// <summary>
301
/// Executes the scalar.
302
/// </summary>
303
/// <param name="query">The query.</param>
304
/// <returns></returns>
305
public object ExecuteScalar(string query)
306
{
307
return ExecuteScalar(query, CommandType.Text, ConnectionState.CloseOnExit);
308
}
309
310
/// <summary>
311
/// Executes the scalar.
312
/// </summary>
313
/// <param name="query">The query.</param>
314
/// <param name="commandtype">The commandtype.</param>
315
/// <returns></returns>
316
public object ExecuteScalar(string query, CommandType commandtype)
317
{
318
return ExecuteScalar(query, commandtype, ConnectionState.CloseOnExit);
319
}
320
321
/// <summary>
322
/// Executes the scalar.
323
/// </summary>
324
/// <param name="query">The query.</param>
325
/// <param name="connectionstate">The connectionstate.</param>
326
/// <returns></returns>
327
public object ExecuteScalar(string query, ConnectionState connectionstate)
328
{
329
return ExecuteScalar(query, CommandType.Text, connectionstate);
330
}
331
332
/// <summary>
333
/// Executes the scalar.
334
/// </summary>
335
/// <param name="query">The query.</param>
336
/// <param name="commandtype">The commandtype.</param>
337
/// <param name="connectionstate">The connectionstate.</param>
338
/// <returns></returns>
339
public object ExecuteScalar(string query, CommandType commandtype, ConnectionState connectionstate)
340
{
341
objCommand.CommandText = query;
342
objCommand.CommandType = commandtype;
343
object o = null;
344
try
345
{
346
if (objConnection.State == System.Data.ConnectionState.Closed)
347
{
348
objConnection.Open();
349
}
350
o = objCommand.ExecuteScalar();
351
}
352
catch (Exception ex)
353
{
354
HandleExceptions(ex);
355
}
356
finally
357
{
358
objCommand.Parameters.Clear();
359
if (connectionstate == ConnectionState.CloseOnExit)
360
{
361
objConnection.Close();
362
}
363
}
364
365
return o;
366
}
367
368
/// <summary>
369
/// Executes the reader.
370
/// </summary>
371
/// <param name="query">The query.</param>
372
/// <returns></returns>
373
public DbDataReader ExecuteReader(string query)
374
{
375
return ExecuteReader(query, CommandType.Text, ConnectionState.CloseOnExit);
376
}
377
378
/// <summary>
379
/// Executes the reader.
380
/// </summary>
381
/// <param name="query">The query.</param>
382
/// <param name="commandtype">The commandtype.</param>
383
/// <returns></returns>
384
public DbDataReader ExecuteReader(string query, CommandType commandtype)
385
{
386
return ExecuteReader(query, commandtype, ConnectionState.CloseOnExit);
387
}
388
389
/// <summary>
390
/// Executes the reader.
391
/// </summary>
392
/// <param name="query">The query.</param>
393
/// <param name="connectionstate">The connectionstate.</param>
394
/// <returns></returns>
395
public DbDataReader ExecuteReader(string query, ConnectionState connectionstate)
396
{
397
return ExecuteReader(query, CommandType.Text, connectionstate);
398
}
399
400
/// <summary>
401
/// Executes the reader.
402
/// </summary>
403
/// <param name="query">The query.</param>
404
/// <param name="commandtype">The commandtype.</param>
405
/// <param name="connectionstate">The connectionstate.</param>
406
/// <returns></returns>
407
public DbDataReader ExecuteReader(string query, CommandType commandtype, ConnectionState connectionstate)
408
{
409
objCommand.CommandText = query;
410
objCommand.CommandType = commandtype;
411
DbDataReader reader = null;
412
try
413
{
414
if (objConnection.State == System.Data.ConnectionState.Closed)
415
{
416
objConnection.Open();
417
}
418
if (connectionstate == ConnectionState.CloseOnExit)
419
{
420
reader = objCommand.ExecuteReader(CommandBehavior.CloseConnection);
421
}
422
else
423
{
424
reader = objCommand.ExecuteReader();
425
}
426
427
}
428
catch (Exception ex)
429
{
430
HandleExceptions(ex);
431
}
432
finally
433
{
434
objCommand.Parameters.Clear();
435
}
436
437
return reader;
438
}
439
440
/// <summary>
441
/// Executes the data set.
442
/// </summary>
443
/// <param name="query">The query.</param>
444
/// <returns></returns>
445
public DataSet ExecuteDataSet(string query)
446
{
447
return ExecuteDataSet(query, CommandType.Text, ConnectionState.CloseOnExit);
448
}
449
450
/// <summary>
451
/// Executes the data set.
452
/// </summary>
453
/// <param name="query">The query.</param>
454
/// <param name="commandtype">The commandtype.</param>
455
/// <returns></returns>
456
public DataSet ExecuteDataSet(string query, CommandType commandtype)
457
{
458
return ExecuteDataSet(query, commandtype, ConnectionState.CloseOnExit);
459
}
460
461
/// <summary>
462
/// Executes the data set.
463
/// </summary>
464
/// <param name="query">The query.</param>
465
/// <param name="connectionstate">The connectionstate.</param>
466
/// <returns></returns>
467
public DataSet ExecuteDataSet(string query, ConnectionState connectionstate)
468
{
469
return ExecuteDataSet(query, CommandType.Text, connectionstate);
470
}
471
472
/// <summary>
473
/// Executes the data set.
474
/// </summary>
475
/// <param name="query">The query.</param>
476
/// <param name="commandtype">The commandtype.</param>
477
/// <param name="connectionstate">The connectionstate.</param>
478
/// <returns></returns>
479
public DataSet ExecuteDataSet(string query, CommandType commandtype, ConnectionState connectionstate)
480
{
481
DbDataAdapter adapter = objFactory.CreateDataAdapter();
482
objCommand.CommandText = query;
483
objCommand.CommandType = commandtype;
484
adapter.SelectCommand = objCommand;
485
DataSet ds = new DataSet();
486
try
487
{
488
adapter.Fill(ds);
489
}
490
catch (Exception ex)
491
{
492
HandleExceptions(ex);
493
}
494
finally
495
{
496
objCommand.Parameters.Clear();
497
if (connectionstate == ConnectionState.CloseOnExit)
498
{
499
if (objConnection.State == System.Data.ConnectionState.Open)
500
{
501
objConnection.Close();
502
}
503
}
504
}
505
return ds;
506
}
507
/// <summary>
508
/// 将DataReader对象转换为DataTable
509
/// </summary>
510
/// <param name="reader">
511
/// DataReader对象
512
/// </param>
513
/// <returns></returns>
514
public DataTable ReaderToTable(DbDataReader reader)
515
{
516
DataTable newTable = new DataTable();
517
DataColumn col;
518
DataRow row;
519
for (int i = 0; i < reader.FieldCount - 1; i++)
520
{
521
col = new DataColumn();
522
col.ColumnName = reader.GetName(i);
523
col.DataType = reader.GetFieldType(i);
524
newTable.Columns.Add(col);
525
}
526
527
while (reader.Read())
528
{
529
row = newTable.NewRow();
530
for (int j = 0; j < reader.FieldCount - 1; j++)
531
{
532
row[j] = reader[j];
533
}
534
newTable.Rows.Add(row);
535
}
536
return newTable;
537
}
538
539
/// <summary>
540
/// Handles the exceptions.
541
/// </summary>
542
/// <param name="ex">The ex.</param>
543
private void HandleExceptions(Exception ex)
544
{
545
if (LogErrors)
546
{
547
WriteToLog(ex.Message);
548
}
549
if (HandleErrors)
550
{
551
strLastError = ex.Message;
552
}
553
else
554
{
555
throw ex;
556
}
557
}
558
559
/// <summary>
560
/// Writes to log.
561
/// </summary>
562
/// <param name="msg">The MSG.</param>
563
private void WriteToLog(string msg)
564
{
565
StreamWriter writer = File.AppendText(LogFile);
566
writer.WriteLine(DateTime.Now.ToString() + " - " + msg);
567
writer.Close();
568
}
569
570
/// <summary>
571
/// 执行与释放或重置非托管资源相关的应用程序定义的任务。
572
/// </summary>
573
public void Dispose()
574
{
575
objConnection.Close();
576
objConnection.Dispose();
577
objCommand.Dispose();
578
}
579
580
}
581
/// <summary>
582
/// 指定数据提供者的类型
583
/// </summary>
584
public enum Providers
585
{
586
SqlServer, OleDb, Oracle, ODBC, ConfigDefined
587
}
588
/// <summary>
589
/// 指定连接状态
590
/// </summary>
591
public enum ConnectionState
592
{
593
KeepOpen, CloseOnExit
594
}
595
}

2
using System;3
using System.Collections.Generic;4
using System.Text;5
using System.Data;6
using System.Configuration;7
using System.Data.Common;8
using System.Data.SqlClient;9
using System.Data.OleDb;10
using System.Data.Odbc;11
using System.Data.OracleClient;12
using System.IO;13

14
namespace WebHelper.DB15
{16
/// <summary>17
/// DatabaseHelper是一个对数据库的封装库,主要针对小型数据库开发18
/// 有着很好的跨数据库功能,但针对专有数据库优化不足19
/// </summary>20
public class DatabaseHelper : IDisposable21
{22
private string strConnectionString;23
private DbConnection objConnection;24
private DbCommand objCommand;25
private DbProviderFactory objFactory = null;26
private bool boolHandleErrors;27
private string strLastError;28
private bool boolLogError;29
private string strLogFile;30

31
/// <summary>32
/// Initializes a new instance of the <see cref="DatabaseHelper"/> class.33
/// </summary>34
/// <param name="connectionstring">The connectionstring.</param>35
/// <param name="provider">The provider.</param>36
public DatabaseHelper(string connectionstring, Providers provider)37
{38
strConnectionString = connectionstring;39
switch (provider)40
{41
case Providers.SqlServer:42
objFactory = SqlClientFactory.Instance;43
break;44
case Providers.OleDb:45
objFactory = OleDbFactory.Instance;46
break;47
case Providers.Oracle:48
objFactory = OracleClientFactory.Instance;49
break;50
case Providers.ODBC:51
objFactory = OdbcFactory.Instance;52
break;53
case Providers.ConfigDefined:54
string providername = ConfigurationManager.ConnectionStrings["connectionstring"].ProviderName;55
switch (providername)56
{57
case "System.Data.SqlClient":58
objFactory = SqlClientFactory.Instance;59
break;60
case "System.Data.OleDb":61
objFactory = OleDbFactory.Instance;62
break;63
case "System.Data.OracleClient":64
objFactory = OracleClientFactory.Instance;65
break;66
case "System.Data.Odbc":67
objFactory = OdbcFactory.Instance;68
break;69
}70
break;71

72
}73
objConnection = objFactory.CreateConnection();74
objCommand = objFactory.CreateCommand();75

76
objConnection.ConnectionString = strConnectionString;77
objCommand.Connection = objConnection;78
}79

80
/// <summary>81
/// Initializes a new instance of the <see cref="DatabaseHelper"/> class.82
/// </summary>83
/// <param name="provider">The provider.</param>84
public DatabaseHelper(Providers provider)85
: this(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString, provider)86
{87
}88

89
/// <summary>90
/// Initializes a new instance of the <see cref="DatabaseHelper"/> class.91
/// </summary>92
/// <param name="connectionstring">The connectionstring.</param>93
public DatabaseHelper(string connectionstring)94
: this(connectionstring, Providers.SqlServer)95
{96
}97

98
/// <summary>99
/// Initializes a new instance of the <see cref="DatabaseHelper"/> class.100
/// </summary>101
public DatabaseHelper()102
: this(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString, Providers.ConfigDefined)103
{104
}105

106
/// <summary>107
/// Gets or sets a value indicating whether [handle errors].108
/// </summary>109
/// <value><c>true</c> if [handle errors]; otherwise, <c>false</c>.</value>110
public bool HandleErrors111
{112
get113
{114
return boolHandleErrors;115
}116
set117
{118
boolHandleErrors = value;119
}120
}121

122
/// <summary>123
/// Gets the last error.124
/// </summary>125
/// <value>The last error.</value>126
public string LastError127
{128
get129
{130
return strLastError;131
}132
}133

134
/// <summary>135
/// Gets or sets a value indicating whether [log errors].136
/// </summary>137
/// <value><c>true</c> if [log errors]; otherwise, <c>false</c>.</value>138
public bool LogErrors139
{140
get141
{142
return boolLogError;143
}144
set145
{146
boolLogError = value;147
}148
}149

150
/// <summary>151
/// Gets or sets the log file.152
/// </summary>153
/// <value>The log file.</value>154
public string LogFile155
{156
get157
{158
return strLogFile;159
}160
set161
{162
strLogFile = value;163
}164
}165

166
/// <summary>167
/// Adds the parameter.168
/// </summary>169
/// <param name="name">The name.</param>170
/// <param name="value">The value.</param>171
/// <returns></returns>172
public int AddParameter(string name, object value)173
{174
DbParameter p = objFactory.CreateParameter();175
p.ParameterName = name;176
p.Value = value;177
return objCommand.Parameters.Add(p);178
}179

180
/// <summary>181
/// Adds the parameter.182
/// </summary>183
/// <param name="parameter">The parameter.</param>184
/// <returns></returns>185
public int AddParameter(DbParameter parameter)186
{187
return objCommand.Parameters.Add(parameter);188
}189

190
/// <summary>191
/// Gets the command.192
/// </summary>193
/// <value>The command.</value>194
public DbCommand Command195
{196
get197
{198
return objCommand;199
}200
}201

202
/// <summary>203
/// Begins the transaction.204
/// </summary>205
public void BeginTransaction()206
{207
if (objConnection.State == System.Data.ConnectionState.Closed)208
{209
objConnection.Open();210
}211
objCommand.Transaction = objConnection.BeginTransaction();212
}213

214
/// <summary>215
/// Commits the transaction.216
/// </summary>217
public void CommitTransaction()218
{219
objCommand.Transaction.Commit();220
objConnection.Close();221
}222

223
/// <summary>224
/// Rollbacks the transaction.225
/// </summary>226
public void RollbackTransaction()227
{228
objCommand.Transaction.Rollback();229
objConnection.Close();230
}231

232
/// <summary>233
/// Executes the non query.234
/// </summary>235
/// <param name="query">The query.</param>236
/// <returns></returns>237
public int ExecuteNonQuery(string query)238
{239
return ExecuteNonQuery(query, CommandType.Text, ConnectionState.CloseOnExit);240
}241

242
/// <summary>243
/// Executes the non query.244
/// </summary>245
/// <param name="query">The query.</param>246
/// <param name="commandtype">The commandtype.</param>247
/// <returns></returns>248
public int ExecuteNonQuery(string query, CommandType commandtype)249
{250
return ExecuteNonQuery(query, commandtype, ConnectionState.CloseOnExit);251
}252

253
/// <summary>254
/// Executes the non query.255
/// </summary>256
/// <param name="query">The query.</param>257
/// <param name="connectionstate">The connectionstate.</param>258
/// <returns></returns>259
public int ExecuteNonQuery(string query, ConnectionState connectionstate)260
{261
return ExecuteNonQuery(query, CommandType.Text, connectionstate);262
}263

264
/// <summary>265
/// Executes the non query.266
/// </summary>267
/// <param name="query">The query.</param>268
/// <param name="commandtype">The commandtype.</param>269
/// <param name="connectionstate">The connectionstate.</param>270
/// <returns></returns>271
public int ExecuteNonQuery(string query, CommandType commandtype, ConnectionState connectionstate)272
{273
objCommand.CommandText = query;274
objCommand.CommandType = commandtype;275
int i = -1;276
try277
{278
if (objConnection.State == System.Data.ConnectionState.Closed)279
{280
objConnection.Open();281
}282
i = objCommand.ExecuteNonQuery();283
}284
catch (Exception ex)285
{286
HandleExceptions(ex);287
}288
finally289
{290
objCommand.Parameters.Clear();291
if (connectionstate == ConnectionState.CloseOnExit)292
{293
objConnection.Close();294
}295
}296

297
return i;298
}299

300
/// <summary>301
/// Executes the scalar.302
/// </summary>303
/// <param name="query">The query.</param>304
/// <returns></returns>305
public object ExecuteScalar(string query)306
{307
return ExecuteScalar(query, CommandType.Text, ConnectionState.CloseOnExit);308
}309

310
/// <summary>311
/// Executes the scalar.312
/// </summary>313
/// <param name="query">The query.</param>314
/// <param name="commandtype">The commandtype.</param>315
/// <returns></returns>316
public object ExecuteScalar(string query, CommandType commandtype)317
{318
return ExecuteScalar(query, commandtype, ConnectionState.CloseOnExit);319
}320

321
/// <summary>322
/// Executes the scalar.323
/// </summary>324
/// <param name="query">The query.</param>325
/// <param name="connectionstate">The connectionstate.</param>326
/// <returns></returns>327
public object ExecuteScalar(string query, ConnectionState connectionstate)328
{329
return ExecuteScalar(query, CommandType.Text, connectionstate);330
}331

332
/// <summary>333
/// Executes the scalar.334
/// </summary>335
/// <param name="query">The query.</param>336
/// <param name="commandtype">The commandtype.</param>337
/// <param name="connectionstate">The connectionstate.</param>338
/// <returns></returns>339
public object ExecuteScalar(string query, CommandType commandtype, ConnectionState connectionstate)340
{341
objCommand.CommandText = query;342
objCommand.CommandType = commandtype;343
object o = null;344
try345
{346
if (objConnection.State == System.Data.ConnectionState.Closed)347
{348
objConnection.Open();349
}350
o = objCommand.ExecuteScalar();351
}352
catch (Exception ex)353
{354
HandleExceptions(ex);355
}356
finally357
{358
objCommand.Parameters.Clear();359
if (connectionstate == ConnectionState.CloseOnExit)360
{361
objConnection.Close();362
}363
}364

365
return o;366
}367

368
/// <summary>369
/// Executes the reader.370
/// </summary>371
/// <param name="query">The query.</param>372
/// <returns></returns>373
public DbDataReader ExecuteReader(string query)374
{375
return ExecuteReader(query, CommandType.Text, ConnectionState.CloseOnExit);376
}377

378
/// <summary>379
/// Executes the reader.380
/// </summary>381
/// <param name="query">The query.</param>382
/// <param name="commandtype">The commandtype.</param>383
/// <returns></returns>384
public DbDataReader ExecuteReader(string query, CommandType commandtype)385
{386
return ExecuteReader(query, commandtype, ConnectionState.CloseOnExit);387
}388

389
/// <summary>390
/// Executes the reader.391
/// </summary>392
/// <param name="query">The query.</param>393
/// <param name="connectionstate">The connectionstate.</param>394
/// <returns></returns>395
public DbDataReader ExecuteReader(string query, ConnectionState connectionstate)396
{397
return ExecuteReader(query, CommandType.Text, connectionstate);398
}399

400
/// <summary>401
/// Executes the reader.402
/// </summary>403
/// <param name="query">The query.</param>404
/// <param name="commandtype">The commandtype.</param>405
/// <param name="connectionstate">The connectionstate.</param>406
/// <returns></returns>407
public DbDataReader ExecuteReader(string query, CommandType commandtype, ConnectionState connectionstate)408
{409
objCommand.CommandText = query;410
objCommand.CommandType = commandtype;411
DbDataReader reader = null;412
try413
{414
if (objConnection.State == System.Data.ConnectionState.Closed)415
{416
objConnection.Open();417
}418
if (connectionstate == ConnectionState.CloseOnExit)419
{420
reader = objCommand.ExecuteReader(CommandBehavior.CloseConnection);421
}422
else423
{424
reader = objCommand.ExecuteReader();425
}426

427
}428
catch (Exception ex)429
{430
HandleExceptions(ex);431
}432
finally433
{434
objCommand.Parameters.Clear();435
}436

437
return reader;438
}439

440
/// <summary>441
/// Executes the data set.442
/// </summary>443
/// <param name="query">The query.</param>444
/// <returns></returns>445
public DataSet ExecuteDataSet(string query)446
{447
return ExecuteDataSet(query, CommandType.Text, ConnectionState.CloseOnExit);448
}449

450
/// <summary>451
/// Executes the data set.452
/// </summary>453
/// <param name="query">The query.</param>454
/// <param name="commandtype">The commandtype.</param>455
/// <returns></returns>456
public DataSet ExecuteDataSet(string query, CommandType commandtype)457
{458
return ExecuteDataSet(query, commandtype, ConnectionState.CloseOnExit);459
}460

461
/// <summary>462
/// Executes the data set.463
/// </summary>464
/// <param name="query">The query.</param>465
/// <param name="connectionstate">The connectionstate.</param>466
/// <returns></returns>467
public DataSet ExecuteDataSet(string query, ConnectionState connectionstate)468
{469
return ExecuteDataSet(query, CommandType.Text, connectionstate);470
}471

472
/// <summary>473
/// Executes the data set.474
/// </summary>475
/// <param name="query">The query.</param>476
/// <param name="commandtype">The commandtype.</param>477
/// <param name="connectionstate">The connectionstate.</param>478
/// <returns></returns>479
public DataSet ExecuteDataSet(string query, CommandType commandtype, ConnectionState connectionstate)480
{481
DbDataAdapter adapter = objFactory.CreateDataAdapter();482
objCommand.CommandText = query;483
objCommand.CommandType = commandtype;484
adapter.SelectCommand = objCommand;485
DataSet ds = new DataSet();486
try487
{488
adapter.Fill(ds);489
}490
catch (Exception ex)491
{492
HandleExceptions(ex);493
}494
finally495
{496
objCommand.Parameters.Clear();497
if (connectionstate == ConnectionState.CloseOnExit)498
{499
if (objConnection.State == System.Data.ConnectionState.Open)500
{501
objConnection.Close();502
}503
}504
}505
return ds;506
}507
/// <summary>508
/// 将DataReader对象转换为DataTable509
/// </summary>510
/// <param name="reader">511
/// DataReader对象512
/// </param>513
/// <returns></returns>514
public DataTable ReaderToTable(DbDataReader reader)515
{516
DataTable newTable = new DataTable();517
DataColumn col;518
DataRow row;519
for (int i = 0; i < reader.FieldCount - 1; i++)520
{521
col = new DataColumn();522
col.ColumnName = reader.GetName(i);523
col.DataType = reader.GetFieldType(i);524
newTable.Columns.Add(col);525
}526

527
while (reader.Read())528
{529
row = newTable.NewRow();530
for (int j = 0; j < reader.FieldCount - 1; j++)531
{532
row[j] = reader[j];533
}534
newTable.Rows.Add(row);535
}536
return newTable;537
}538

539
/// <summary>540
/// Handles the exceptions.541
/// </summary>542
/// <param name="ex">The ex.</param>543
private void HandleExceptions(Exception ex)544
{545
if (LogErrors)546
{547
WriteToLog(ex.Message);548
}549
if (HandleErrors)550
{551
strLastError = ex.Message;552
}553
else554
{555
throw ex;556
}557
}558

559
/// <summary>560
/// Writes to log.561
/// </summary>562
/// <param name="msg">The MSG.</param>563
private void WriteToLog(string msg)564
{565
StreamWriter writer = File.AppendText(LogFile);566
writer.WriteLine(DateTime.Now.ToString() + " - " + msg);567
writer.Close();568
}569

570
/// <summary>571
/// 执行与释放或重置非托管资源相关的应用程序定义的任务。572
/// </summary>573
public void Dispose()574
{575
objConnection.Close();576
objConnection.Dispose();577
objCommand.Dispose();578
}579

580
}581
/// <summary>582
/// 指定数据提供者的类型583
/// </summary>584
public enum Providers585
{586
SqlServer, OleDb, Oracle, ODBC, ConfigDefined587
}588
/// <summary>589
/// 指定连接状态590
/// </summary>591
public enum ConnectionState592
{593
KeepOpen, CloseOnExit594
}595
} 1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.IO;
11
using System.Text;
12
using System.Security.Cryptography;
13
14
/// <summary>
15
/// DESEncryptor 的摘要说明
16
/// </summary>
17
namespace WebHelper.InterService
18
{
19
//此类的实例代码如下:
20
// DESEncryptor myDES = new DESEncryptor();
21
//myDES.EncryptKey = "12345678";
22
//myDES.InputString = "楚广明";
23
//myDES.DesEncrypt();
24
//Response.Write("楚广明加密后得到:"+myDES.OutString);
25
//string tempString = myDES.OutString;
26
27
//Response.Write("<br>");
28
29
//myDES.DecryptKey = "12345678";
30
//myDES.InputString = tempString;
31
//myDES.DesDecrypt();
32
//Response.Write(tempString + "解密后得到" + myDES.OutString);
33
34
//Response.Write("<br>");
35
//myDES.InputString = "楚广明";
36
//myDES.MD5Encrypt();
37
//Response.Write("楚广明MD5后得到:" + myDES.OutString);
38
39
40
//Response.Write("<br>");
41
public class DESEncryptor
42
{
43
44
私有成员
74
75
公共属性
133
134
构造函数
142
143
DES加密字符串
171
172
DES解密字符串
201
202
DES加密文件
245
246
DES解密文件
290
291
MD5
304
}
305
}
306
307
using System;2
using System.Data;3
using System.Configuration;4
using System.Web;5
using System.Web.Security;6
using System.Web.UI;7
using System.Web.UI.WebControls;8
using System.Web.UI.WebControls.WebParts;9
using System.Web.UI.HtmlControls;10
using System.IO;11
using System.Text;12
using System.Security.Cryptography;13

14
/// <summary>15
/// DESEncryptor 的摘要说明16
/// </summary>17
namespace WebHelper.InterService18
{19
//此类的实例代码如下:20
// DESEncryptor myDES = new DESEncryptor();21
//myDES.EncryptKey = "12345678";22
//myDES.InputString = "楚广明";23
//myDES.DesEncrypt();24
//Response.Write("楚广明加密后得到:"+myDES.OutString);25
//string tempString = myDES.OutString;26

27
//Response.Write("<br>");28

29
//myDES.DecryptKey = "12345678";30
//myDES.InputString = tempString;31
//myDES.DesDecrypt();32
//Response.Write(tempString + "解密后得到" + myDES.OutString);33

34
//Response.Write("<br>");35
//myDES.InputString = "楚广明";36
//myDES.MD5Encrypt();37
//Response.Write("楚广明MD5后得到:" + myDES.OutString);38

39

40
//Response.Write("<br>");41
public class DESEncryptor42
{43

44
私有成员74
75
公共属性133
134
构造函数142

143
DES加密字符串171

172
DES解密字符串201

202
DES加密文件245

246
DES解密文件290

291
MD5304
}305
}306

307

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using WebHelper.DB;
using WebHelper.InterService;
/// <summary>
/// User 的摘要说明
/// </summary>
public class User
{
private string _username;
private string _userpwd;
private DateTime _addtime;
private DESEncryptor myDES = new DESEncryptor();
public User()
{
myDES.EncryptKey = "admin123";
myDES.DecryptKey = "admin123";
}
/// <summary>
/// Construct
/// </summary>
public User (string username,string userpwd,DateTime addtime):this()
{
this._username = username;
this._userpwd = userpwd;
this._addtime = addtime;
}
/// <summary>
/// username
/// </summary>
public string username
{
get{ return _username;}
set{ _username = value; }
}
/// <summary>
/// userpwd
/// </summary>
public string userpwd
{
get
{
myDES.InputString = _userpwd;
myDES.DesDecrypt();
return myDES.OutString;
}
set
{
myDES.InputString = value;
myDES.DesEncrypt();
_userpwd = myDES.OutString;
}
}
/// <summary>
/// AddTime
/// </summary>
public DateTime AddTime
{
get{ return _addtime;}
set{ _addtime = value; }
}
public Boolean Add()
{
//if (_username != "" && _userpwd != "" && _addtime.ToString() != "")
//{
// DatabaseHelper db = new DatabaseHelper();
// db.BeginTransaction();
// db.AddParameter("@username", _username);
// db.AddParameter("@userpwd", _userpwd);
// db.AddParameter("@datetime", _addtime);
// int i = db.ExecuteNonQuery("insert into tb_User(username,userpwd,AddTime) values(@username,@userpwd,@datetime)",
// WebHelper.DB.ConnectionState.KeepOpen);
// if (i > 0)
// {
// db.CommitTransaction();
// return true;
// }
// else
// return false;
//}
//else
//{
// return false;
//}
if (_username != "" && _userpwd != "" && _addtime.ToString() != "")
{
DatabaseHelper db = new DatabaseHelper();
db.BeginTransaction();
db.AddParameter("@username", _username);
db.AddParameter("@userpwd", _userpwd);
db.AddParameter("@datetime", _addtime);
int i = db.ExecuteNonQuery("insert into tb_User(username,userpwd,AddTime) values(@username,@userpwd,@datetime)",
WebHelper.DB.ConnectionState.KeepOpen);
if (i > 0)
{
db.CommitTransaction();
return true;
}
else
return false;
}
else
{
return false;
}
}
public Boolean Delete()
{
if (_username != "" && _userpwd != "")
{
DatabaseHelper db = new DatabaseHelper();
db.BeginTransaction();
db.AddParameter("@username", _username);
db.AddParameter("@userpwd", _userpwd);
int i = db.ExecuteNonQuery("delete from tb_User where username=@username and userpwd=@userpwd",
WebHelper.DB.ConnectionState.KeepOpen);
if (i > 0)
{
db.CommitTransaction();
return true;
}
else
return false;
}
else
{
return false;
}
}
public Boolean Update(string strNewPwd)
{
if (_username != "" && _userpwd != "")
{
DatabaseHelper db = new DatabaseHelper();
db.BeginTransaction();
db.AddParameter("@username", _username);
db.AddParameter("@userpwd", _userpwd);
db.AddParameter("@newuserpwd",strNewPwd);
int i = db.ExecuteNonQuery("update tb_User set userpwd=@newuserpwd where username=@username and userpwd=@userpwd",
WebHelper.DB.ConnectionState.KeepOpen);
if (i > 0)
{
db.CommitTransaction();
return true;
}
else
return false;
}
else
{
return false;
}
}
public Boolean Select()
{
if (_username != "" && _userpwd != "")
{
DatabaseHelper db = new DatabaseHelper();
db.AddParameter("@username", _username);
db.AddParameter("@userpwd", _userpwd);
object i = db.ExecuteScalar("select count(*) from tb_User where username=@username and userpwd=@userpwd");
if ((int)i > 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}


浙公网安备 33010602011771号