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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

74

75

133

134

142

143

171

172

201

202

245

246

290

291

304

305

306

307






































































































































































































【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述