编写OracleMembershipProvider,让SharePoint2007使用Oralce中的用户数据实现Form验证。 (第四天)
继续完善代码,以下代码与之前的比较多了如下特性:
* 所有属性均可在web.config中配置
* 数据库操作异常写入日志
* 多了一个LogTools类,用于日志相关操作
* 自定义多个属性增加灵活性,比如可以用任意表名,
* 确定了表结构,并给出Oracle下的表创建语句( 使用本语句需要做简单修改,替换用户和表空间名)
(因为调试部署费时,所以代码均未经过测试,待代码编写完整后再测试)

OracleMembershipProvider
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Web;
5
using System.Web.Security;
6
using System.Data;
7
using System.Data.OleDb;
8
using System.Collections;
9
using System.Configuration;
10
11
/**//*表创建语句 (创建于:Oracle 8i)
12
CREATE TABLE "ANTUUSER"."SHAREPOINTUSERS"("PKID" VARCHAR2(50)
13
NOT NULL, "USERNAME" VARCHAR2(255) NOT NULL, "APPLICATIONNAME"
14
VARCHAR2(255) NOT NULL, "EMAIL" VARCHAR2(128) NOT NULL,
15
"COMMENT" VARCHAR2(255) NOT NULL, "PASSWORD" VARCHAR2(128) NOT
16
NULL, "PASSWORDQUESTION" VARCHAR2(255) NOT NULL,
17
"PASSWORDANSWER" VARCHAR2(255) NOT NULL, "ISAPPROVED" NUMBER(1)
18
NOT NULL, "LASTACTIVITYDATE" DATE NOT NULL, "LASTLOGINDATE" DATE
19
NOT NULL, "LASTPASSWORDCHANGEDATE" DATE NOT NULL, "CREATIONDATE"
20
DATE NOT NULL, "ISONLINE" NUMBER(1) NOT NULL, "ISLOCKEDOUT"
21
NUMBER(1) NOT NULL, "LASTLOCKEDOUTDATE" DATE NOT NULL,
22
"FAILEDPWDATTEMPTCOUNT" NUMBER(5) NOT NULL,
23
"FAILEDPWDATTEMPTWINSTART" DATE NOT NULL,
24
"FAILEDPWDANSWERATTEMPTCOUNT" NUMBER(5) NOT NULL,
25
"FAILEDPWDANSWERATTEMPTWINSTART" DATE NOT NULL)
26
*/
27
namespace BoooLee
28

{
29
public class OracleMembershipProvider : MembershipProvider
30
{
31
//OracleTools
32
private OracleTools OT = null;
33
34
//自定义属性
35
private string _UsersTableName;
36
//Oracle数据库连接串
37
private string _ConnectionString;
38
//是否允许事件日志
39
private bool _EnableEventLog;
40
41
//MembershipProvider接口属性,用于存放config.web中的MembershipProvider配置属性。
42
//使用自定义成员资格提供程序的应用程序的名称。
43
private string _ApplicationName;
44
//指示成员资格提供程序是否配置为允许用户重置其密码。
45
private bool _EnablePasswordReset;
46
//指示成员资格提供程序是否配置为允许用户检索其密码。
47
private bool _EnablePasswordRetrieval;
48
//获取锁定成员资格用户前允许的无效密码或无效密码提示问题答案尝试次数。
49
private int _MaxInvalidPasswordAttempts;
50
//获取有效密码中必须包含的最少特殊字符数。
51
private int _MinRequireNonAlphanumericCharacters;
52
//获取密码所要求的最小长度。
53
private int _MinRequiredPasswordLength;
54
//获取在锁定成员资格用户之前允许的最大无效密码或无效密码提示问题答案尝试次数的分钟数。
55
private int _PasswordAttemptWindow;
56
//获取一个值,该值指示在成员资格数据存储区中存储密码的格式。
57
private MembershipPasswordFormat _PasswordFormat;
58
//获取用于计算密码的正则表达式。
59
private string _PasswordStrengthRegularExpression;
60
//获取一个值,该值指示成员资格提供程序是否配置为要求用户在进行密码重置和检索时回答密码提示问题。
61
private bool _RequiresQuestionAndAnswer;
62
//获取一个值,指示成员资格提供程序是否配置为要求每个用户名具有唯一的电子邮件地址。
63
private bool _RequiresUniqueEmail;
64
65
/**//*
66
摘要:
67
初始化提供程序。
68
69
参数:
70
config: 名称/值对的集合,表示在配置中为该提供程序指定的、提供程序特定的属性。
71
name: 该提供程序的友好名称。
72
*/
73
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
74
{
75
base.Initialize(name, config);
76
77
//读取MembershipProvider接口属性配置
78
_ApplicationName = GetConfigValue(config["ConnectionString"],System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
79
_EnablePasswordReset = Convert.ToBoolean(GetConfigValue(config["EnablePasswordReset"], "false"));
80
_EnablePasswordRetrieval = Convert.ToBoolean(GetConfigValue(config["EnablePasswordRetrieval"], "false"));
81
_MaxInvalidPasswordAttempts=Convert.ToInt32(GetConfigValue(config["MaxInvalidPasswordAttempts"],"5");
82
_MinRequireNonAlphanumericCharacters=Convert.ToInt32(GetConfigValue(config["MinRequiredNonAlphanumericCharacters"],"1");
83
_MinRequiredPasswordLength=Convert.ToInt32(GetConfigValue(config["MinRequiredPasswordLength"],"7");
84
_PasswordAttemptWindow=Convert.ToInt32(GetConfigValue(config["PasswordAttemptWindow"],"10");
85
_PasswordFormat=(MembershipPasswordFormat)GetConfigValue(config["PasswordFormat"],MembershipPasswordFormat.Clear);
86
_PasswordStrengthRegularExpression=GetConfigValue(config["PasswordStrengthRegularExpression"],""));
87
_RequiresQuestionAndAnswer=Convert.ToBoolean(GetConfigValue(config["RequiresQuestionAndAnswer"],"false"));
88
_RequiresUniqueEmail=Convert.ToBoolean(GetConfigValue(config["RequiresUniqueEmail"],"true"));
89
90
//读取自定义属性
91
ConnectionStringSettings ConnectionStringSetting=ConfigurationManager.ConnectionStrings[config["ConnectionString"]];
92
_ConnectionString=ConnectionStringSetting.ConnectionString;
93
_UsersTableName=GetConfigValue(config["UsersTableName"],"SHAREPOINTUSERS");
94
_EnableEventLog=Convert.ToBoolean(GetConfigValue(config["EnableEventLog"],"true"];
95
//初始化OT
96
OT = new OracleTools();
97
OT.ConnectionString = _ConnectionString;
98
99
//为OT分配日志记录器
100
LogTools lt = new LogTools();
101
lt.EventLog = "Application";
102
lt.EventSource = "OracleMembershipProvider";
103
OT.LogTool = lt;
104
OT.EnableEventlog=_EnableEventLog;
105
}
106
107
/**//*
108
摘要:
109
使用自定义成员资格提供程序的应用程序的名称。
110
111
返回值:
112
使用自定义成员资格提供程序的应用程序的名称。
113
*/
114
public override string ApplicationName
115
{
116
get
117
{
118
return _ApplicationName;
119
}
120
set
121
{
122
;
123
}
124
}
125
126
/**//*
127
摘要:
128
处理更新成员资格用户密码的请求。
129
130
参数:
131
newPassword: 指定的用户的新密码。
132
oldPassword: 指定的用户的当前密码。
133
username: 为其更新密码的用户。
134
135
返回值:
136
如果密码更新成功,则为 true;否则为 false。
137
*/
138
public override bool ChangePassword(string username, string oldPassword, string newPassword)
139
{
140
string SqlString = "";
141
142
//更改用户密码
143
if (UserExist(username,oldPassword)>0)
144
{
145
SqlString = string.Format("update {0} set password={1} where username='{2}' and password='{3}'",_TableName, newPassword, username, oldPassword);
146
return OT.RunSqlNonQuery(SqlString);
147
}
148
149
return false;
150
}
151
152
/**//*
153
摘要:
154
处理更新成员资格用户的密码提示问题和答案的请求。
155
156
参数:
157
newPasswordQuestion: 指定的用户的新密码提示问题。
158
newPasswordAnswer: 指定的用户的新密码提示问题答案。
159
username: 要为其更改密码提示问题和答案的用户。
160
password: 指定的用户的密码。
161
162
返回值:
163
如果成功更新密码提示问题和答案,则为 true;否则,为 false。
164
*/
165
public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
166
{
167
string SqlString = "";
168
169
//更改用户密码问题和密码答案
170
if (UserExist(username,password)>0)
171
{
172
SqlString = string.Format("update {0} set question={1} , answer={2} where username='{3}' and password='{4}'", _TableName,newPasswordQuestion, newPasswordAnswer, username, password);
173
return OT.RunSqlNonQuery(SqlString);
174
}
175
176
return false;
177
}
178
179
/**//*
180
摘要:
181
将新的成员资格用户添加到数据源。
182
183
参数:
184
isApproved: 是否允许验证新用户。
185
passwordAnswer: 新用户的密码提示问题答案。
186
username: 新用户的用户名。
187
providerUserKey: 成员资格数据源中该用户的唯一标识符。
188
password: 新用户的密码。
189
passwordQuestion: 新用户的密码提示问题。
190
email: 新用户的电子邮件地址。
191
status: 一个 System.Web.Security.MembershipCreateStatus 枚举值,指示是否已成功创建用户。
192
193
返回值:
194
一个用新创建的用户的信息填充的 System.Web.Security.MembershipUser 对象。
195
*/
196
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
197
{
198
//TODO 实现创建用户代码
199
throw new Exception("The method or operation is not implemented.");
200
}
201
202
/**//*
203
摘要:
204
从成员资格数据源删除一个用户。
205
206
参数:
207
username: 要删除的用户的名称。
208
deleteAllRelatedData: 如果为 true,则从数据库中删除与该用户相关的数据;如果为 false,则将与该用户相关的数据保留在数据库。
209
210
返回值:
211
如果用户被成功删除,则为 true;否则为 false。
212
*/
213
public override bool DeleteUser(string username, bool deleteAllRelatedData)
214
{
215
string SqlString = "";
216
217
//删除用户
218
if (UserExist(username)>0)
219
{
220
SqlString = string.Format("delete from {0} where username='{1}'",_TableName, username);
221
return OT.RunSqlNonQuery(SqlString);
222
}
223
224
return false;
225
}
226
227
/**//*
228
摘要:
229
指示成员资格提供程序是否配置为允许用户重置其密码。
230
231
返回值:
232
如果成员资格提供程序支持密码重置,则为 true;否则为 false。默认为 true。
233
*/
234
public override bool EnablePasswordReset
235
{
236
get
237
{
238
return _EnablePasswordReset;
239
}
240
}
241
242
/**//*
243
摘要:
244
指示成员资格提供程序是否配置为允许用户检索其密码。
245
246
返回值:
247
如果成员资格提供程序配置为支持密码检索,则为 true,否则为 false。默认为 false。
248
*/
249
public override bool EnablePasswordRetrieval
250
{
251
get
252
{
253
return _EnablePasswordRetrieval;
254
}
255
}
256
257
/**//*
258
摘要:
259
获取一个成员资格用户的集合,这些用户的电子邮件地址包含要匹配的指定电子邮件地址。
260
261
参数:
262
totalRecords: 匹配用户的总数。
263
pageIndex: 要返回的结果页的索引。pageIndex 是从零开始的。
264
emailToMatch: 要搜索的电子邮件地址。
265
pageSize: 要返回的结果页的大小。
266
267
返回值:
268
包含一页 pageSizeSystem.Web.Security.MembershipUser 对象的 System.Web.Security.MembershipUserCollection 集合,这些对象从 pageIndex 指定的页开始。
269
*/
270
public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
271
{
272
string SqlString = "";
273
MembershipUserCollection muc = new MembershipUserCollection();
274
totalRecords = UserExistByMail(emailToMatch);
275
if (totalRecords>0)
276
{
277
SqlString = string.Format("select * from {0} where email='{1}'", _TableName,emailToMatch);
278
muc = ToMembershipUserCollection(OT.RunSqlDataTable(SqlString));
279
}
280
return muc;
281
}
282
283
/**//*
284
摘要:
285
获取一个成员资格用户的集合,这些用户的用户名包含要匹配的指定用户名。
286
287
参数:
288
totalRecords: 匹配用户的总数。
289
pageIndex: 要返回的结果页的索引。pageIndex 是从零开始的。
290
usernameToMatch: 要搜索的用户名。
291
pageSize: 要返回的结果页的大小。
292
293
返回值:
294
包含一页 pageSizeSystem.Web.Security.MembershipUser 对象的 System.Web.Security.MembershipUserCollection 集合,这些对象从 pageIndex 指定的页开始。
295
*/
296
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
297
{
298
string SqlString = "";
299
MembershipUserCollection muc = new MembershipUserCollection();
300
totalRecords = UserExist(usernameToMatch);
301
if (totalRecords > 0)
302
{
303
SqlString = string.Format("select * from {0} where username='{1}'", _TableName,usernameToMatch);
304
muc=ToMembershipUserCollection( OT.RunSqlDataTable(SqlString));
305
}
306
return muc;
307
}
308
309
/**//*
310
摘要:
311
获取数据源中的所有用户的集合,并显示在数据页中。
312
313
参数:
314
totalRecords: 匹配用户的总数。
315
pageIndex: 要返回的结果页的索引。pageIndex 是从零开始的。
316
pageSize: 要返回的结果页的大小。
317
318
返回值:
319
包含一页 pageSizeSystem.Web.Security.MembershipUser 对象的 System.Web.Security.MembershipUserCollection 集合,这些对象从 pageIndex 指定的页开始。
320
*/
321
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
322
{
323
string SqlString = "";
324
MembershipUserCollection muc = new MembershipUserCollection();
325
totalRecords = UsersTotalRecords();
326
if (totalRecords > 0)
327
{
328
SqlString =string.Format( "select * from {0}",_TableName);
329
muc = ToMembershipUserCollection(OT.RunSqlDataTable(SqlString));
330
}
331
return muc;
332
}
333
334
/**//*
335
摘要:
336
获取当前访问该应用程序的用户数。
337
338
返回值:
339
当前访问该应用程序的用户数。
340
*/
341
public override int GetNumberOfUsersOnline()
342
{
343
//TODO 获取在线人数
344
throw new Exception("The method or operation is not implemented.");
345
}
346
347
/**//*
348
摘要:
349
从数据源获取指定用户名所对应的密码。
350
351
参数:
352
username: 为其检索密码的用户。
353
answer: 用户的密码提示问题答案。
354
355
返回值:
356
指定用户名所对应的密码。
357
*/
358
public override string GetPassword(string username, string answer)
359
{
360
string SqlString = "";
361
string password="";
362
if (UserExist(username) > 0)
363
{
364
SqlString = string.Format("select password from {0} where username='{1}' and answer='{2}'", _TableName,username, answer);
365
password = OT.RunSqlScalar(SqlString);
366
}
367
return password;
368
}
369
370
/**//*
371
摘要:
372
从数据源获取用户的信息。提供一个更新用户最近一次活动的日期/时间戳的选项。
373
374
参数:
375
username: 要获取其信息的用户名。
376
userIsOnline: 如果为 true,则更新用户最近一次活动的日期/时间戳;如果为 false,则返回用户信息,但不更新用户最近一次活动的日期/时间戳。
377
378
返回值:
379
用数据源中指定用户的信息填充的 System.Web.Security.MembershipUser 对象。
380
*/
381
public override MembershipUser GetUser(string username, bool userIsOnline)
382
{
383
string SqlString = "";
384
MembershipUser mu = null;
385
386
if (UserExist(username) > 0)
387
{
388
SqlString = string.Format("select * from {0} where username='{1}'", _TableName,username);
389
mu=ToMembershipUser(OT.RunSqlDataTable(SqlString).Rows[0]);
390
}
391
392
return mu;
393
}
394
395
/**//*
396
摘要:
397
根据成员资格用户的唯一标识符从数据源获取该用户的信息。提供一个更新用户最近一次活动的日期/时间戳的选项。
398
399
参数:
400
providerUserKey: 要获取其信息的成员资格用户的唯一标识符。
401
userIsOnline: 如果为 true,则更新用户最近一次活动的日期/时间戳;如果为 false,则返回用户信息,但不更新用户最近一次活动的日期/时间戳。
402
403
返回值:
404
用数据源中指定用户的信息填充的 System.Web.Security.MembershipUser 对象。
405
*/
406
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
407
{
408
string SqlString = "";
409
MembershipUser mu = new MembershipUser();
410
411
if (UserExist(username) > 0)
412
{
413
SqlString = string.Format("select * from {0} where provideruserkey='{1}'", _TableName,providerUserKey.ToString());
414
mu = ToMembershipUser(OT.RunSqlDataTable(SqlString).Rows[0]);
415
}
416
417
return mu;
418
}
419
420
public override string GetUserNameByEmail(string email)
421
{
422
throw new Exception("The method or operation is not implemented.");
423
}
424
425
public override int MaxInvalidPasswordAttempts
426
{
427
get
428
{
429
return _MaxInvalidPasswordAttempts;
430
}
431
}
432
433
public override int MinRequiredNonAlphanumericCharacters
434
{
435
get
436
{
437
return _MinRequireNonAlphanumericCharacters;
438
}
439
440
}
441
442
public override int MinRequiredPasswordLength
443
{
444
get
445
{
446
return _MinRequiredPasswordLength;
447
}
448
}
449
450
public override int PasswordAttemptWindow
451
{
452
get
453
{
454
return _PasswordAttemptWindow;
455
}
456
}
457
458
public override MembershipPasswordFormat PasswordFormat
459
{
460
get
461
{
462
return _PasswordFormat;
463
}
464
}
465
466
public override string PasswordStrengthRegularExpression
467
{
468
get
469
{
470
return PasswordStrengthRegularExpression;
471
}
472
}
473
474
public override bool RequiresQuestionAndAnswer
475
{
476
get
477
{
478
return RequiresQuestionAndAnswer;
479
}
480
}
481
482
public override bool RequiresUniqueEmail
483
{
484
get
485
{
486
return RequiresUniqueEmail;
487
}
488
}
489
490
public override string ResetPassword(string username, string answer)
491
{
492
throw new Exception("The method or operation is not implemented.");
493
}
494
495
public override bool UnlockUser(string userName)
496
{
497
throw new Exception("The method or operation is not implemented.");
498
}
499
500
public override void UpdateUser(MembershipUser user)
501
{
502
throw new Exception("The method or operation is not implemented.");
503
}
504
505
public override bool ValidateUser(string username, string password)
506
{
507
throw new Exception("The method or operation is not implemented.");
508
}
509
510
非MembershipProvider成员#region 非MembershipProvider成员
511
512
/**//*
513
摘要:
514
判断用户是否存在
515
516
参数:
517
username:用户名
518
519
返回值:
520
找到用户返回true,没有找到用户false
521
*/
522
private int UserExist(string username)
523
{
524
string SqlString = "";
525
string r = "";
526
int Count = 0;
527
528
SqlString = string.Format("select count(*) from {0} where username='{1}'", _TableName,username);
529
r = OT.RunSqlScalar(SqlString);
530
try
531
{
532
Count = int.Parse(r);
533
}
534
catch (Exception)
535
{
536
Count = 0;
537
}
538
539
return Count;
540
}
541
542
/**//*
543
摘要:
544
判断用户是否存在
545
546
参数:
547
username:用户名
548
password:用户密码
549
550
返回值:
551
找到用户的用户数。
552
*/
553
private int UserExist(string username, string password)
554
{
555
string SqlString = "";
556
string r = "";
557
int Count = 0;
558
559
SqlString = string.Format("select count(*) from {0} where username='{1}' and password='{2}'",_TableName, username,password);
560
r = OT.RunSqlScalar(SqlString);
561
try
562
{
563
Count = int.Parse(r);
564
}
565
catch (Exception)
566
{
567
Count = 0;
568
}
569
570
return Count;
571
}
572
573
/**//*
574
摘要:
575
判断用户是否存在
576
577
参数:
578
email:用户名
579
580
返回值:
581
找到用户的用户数。
582
*/
583
private int UserExistByMail(string email)
584
{
585
string SqlString = "";
586
string r = "";
587
int Count = 0;
588
589
SqlString = string.Format("select count(*) from {0} where email='{1}'", _TableName,email);
590
r = OT.RunSqlScalar(SqlString);
591
try
592
{
593
Count = int.Parse(r);
594
}
595
catch (Exception)
596
{
597
Count = 0;
598
}
599
600
return Count;
601
}
602
603
/**//*
604
摘要:
605
转换用户数据表为MembershipUserCollection
606
607
参数:
608
userstable:用户表
609
610
返回值:
611
返回包含用户数据的MembershipUserCollection。
612
*/
613
private MembershipUserCollection ToMembershipUserCollection(DataTable userstable)
614
{
615
MembershipUserCollection muc = new MembershipUserCollection();
616
617
foreach (DataRow dr in userstable.Rows)
618
{
619
muc.Add(ToMembershipUser(dr));
620
}
621
622
return muc;
623
}
624
625
/**//*
626
摘要:
627
获得用户总数
628
629
返回值:
630
返回用户数。
631
*/
632
private int UsersTotalRecords()
633
{
634
string SqlString = "";
635
string r = "";
636
int Count = 0;
637
638
SqlString = string.Format("select count(*) from {0}",_TableName);
639
r = OT.RunSqlScalar(SqlString);
640
try
641
{
642
Count = int.Parse(r);
643
}
644
catch (Exception)
645
{
646
Count = 0;
647
}
648
649
return Count;
650
}
651
652
/**//*
653
摘要:
654
转换用户数据表为MembershipUserCollection
655
656
参数:
657
usersrecord:用户记录
658
659
返回值:
660
返回包含用户数据的MembershipUser。
661
*/
662
private MembershipUser ToMembershipUser(DataRow usersrecord)
663
{
664
MembershipUser mu = new MembershipUser();
665
try
666
{
667
mu.UserName = usersrecord["username"];
668
mu.Email = usersrecord["email"];
669
mu.PasswordQuestion = usersrecord["question"];
670
}
671
catch (Exception)
672
{
673
}
674
return mu;
675
}
676
677
/**//*
678
摘要:
679
获取配置文件
680
681
参数:
682
ConfigValue:配置文件中的参数设置
683
DefaultValue:缺省值
684
685
返回值:
686
如果ConfigValue值有效则返回ConfigValue,否则返回DefaultValue。
687
*/
688
private string GetConfigValue(string ConfigValue, string DefaultValue)
689
{
690
if (string.IsNullOrEmpty(ConfigValue))
691
return DefaultValue;
692
return ConfigValue;
693
}
694
#endregion
695
696
}
697
}
698

OracleTools
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Data;
5
using System.Data.OleDb;
6
7
namespace BoooLee
8

{
9
class OracleTools
10
{
11
/**//// <summary>
12
/// 私有变量
13
/// </summary>
14
private string _ConnectionString = ""; //数据库连接串
15
16
/**//// <summary>
17
/// 日志记录器
18
/// </summary>
19
private LogTools _LogTools = null;
20
21
/**//// <summary>
22
/// 是否允许记录日志
23
/// </summary>
24
private bool _EnableEventLog = false;
25
26
/**//// <summary>
27
/// 是否允许记录日志
28
/// </summary>
29
public bool EnableEventlog
30
{
31
get
32
{
33
return _EnableEventLog;
34
}
35
36
set
37
{
38
_EnableEventLog = value;
39
}
40
}
41
42
/**//// <summary>
43
/// 日志记录器
44
/// </summary>
45
public LogTools LogTool
46
{
47
get
48
{
49
return _LogTools;
50
}
51
52
set
53
{
54
_LogTools = value;
55
}
56
}
57
/**//// <summary>
58
/// 数据库连接串
59
/// </summary>
60
public string ConnectionString
61
{
62
get
63
{
64
return _ConnectionString;
65
}
66
67
set
68
{
69
_ConnectionString=value;
70
}
71
}
72
73
/**//// <summary>
74
/// 运行无返回值的Sql语句。
75
/// </summary>
76
/// <param name="SqlString">指定要运行的Sql语句。</param>
77
/// <param name="Conn">指定数据库连接。</param>
78
/// <returns>成功返回true,失败返回false。</returns>
79
public bool RunSqlNonQuery(string SqlString)
80
{
81
OleDbConnection Conn=null;
82
83
try
84
{
85
using (OleDbCommand Command = new OleDbCommand())
86
{
87
Command.CommandText = SqlString;
88
Conn = GetConnection();
89
Conn.Open();
90
Command.Connection = Conn;
91
Command.ExecuteNonQuery();
92
}
93
return true;
94
}
95
catch (Exception ex)
96
{
97
if (_LogTools != null && _EnableEventLog)
98
_LogTools.WriteToEventLog(ex, string.Format("RunSqlNonQuery(\"
{0}\")", SqlString));
99
return false;
100
}
101
finally
102
{
103
//关闭数据库连接
104
if (Conn != null && Conn.State==ConnectionState.Open)
105
Conn.Close();
106
}
107
}
108
109
/**//// <summary>
110
/// 运行返回一个值的Select语句。
111
/// </summary>
112
/// <param name="SqlString">指定要运行的Sql语句。</param>
113
/// <param name="Conn">指定数据库连接。</param>
114
/// <returns>成功返回Select语句结果,失败返回空字符串。</returns>
115
public string RunSqlScalar(string SqlString)
116
{
117
OleDbConnection Conn = null;
118
119
try
120
{
121
Conn = GetConnection();
122
Conn.Open();
123
124
using (OleDbCommand Command = new OleDbCommand())
125
{
126
Command.CommandText = SqlString;
127
Command.Connection = Conn;
128
return Command.ExecuteScalar().ToString();
129
}
130
}
131
catch (Exception ex)
132
{
133
if (_LogTools != null && _EnableEventLog)
134
_LogTools.WriteToEventLog(ex, string.Format("RunSqlScalar(\"
{0}\")",SqlString));
135
return "";
136
}
137
finally
138
{
139
if (Conn != null && Conn.State == ConnectionState.Open)
140
Conn.Close();
141
}
142
}
143
144
/**//// <summary>
145
/// 运行返回DataTable的Sql语句。
146
/// </summary>
147
/// <param name="SqlString">指定要运行的Sql语句。</param>
148
/// <param name="Conn">指定数据库连接。</param>
149
/// <returns>成功返回Select语句结果DataTable。</returns>
150
public DataTable RunSqlDataTable(string SqlString)
151
{
152
OleDbConnection Conn = null;
153
154
try
155
{
156
using (OleDbDataAdapter DataAdapter = new OleDbDataAdapter())
157
{
158
Conn = GetConnection();
159
Conn.Open();
160
OleDbCommand SC = new OleDbCommand();
161
SC.CommandText = SqlString;
162
SC.Connection = Conn;
163
DataAdapter.SelectCommand = SC;
164
DataTable dt = new DataTable();
165
DataAdapter.Fill(dt);
166
return dt;
167
}
168
}
169
catch (Exception ex)
170
{
171
if (_LogTools != null && _EnableEventLog)
172
_LogTools.WriteToEventLog(ex, string.Format("RunSqlDataTable(\"
{0}\")", SqlString));
173
return new DataTable();
174
}
175
finally
176
{
177
if (Conn != null && Conn.State == ConnectionState.Open)
178
Conn.Close();
179
}
180
}
181
182
/**//// <summary>
183
/// 返回一个OleDbConnection对象。
184
/// </summary>
185
/// <returns>成功返回的OleDbConnection对象,失败返回null。</returns>
186
public OleDbConnection GetConnection()
187
{
188
try
189
{
190
OleDbConnection result = new OleDbConnection();
191
result.ConnectionString = _ConnectionString;
192
return result;
193
}
194
catch (Exception ex)
195
{
196
if (_LogTools != null && _EnableEventLog)
197
_LogTools.WriteToEventLog(ex, "GetConnection()");
198
return null;
199
}
200
}
201
}
202
}
203

LogTools
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
namespace BoooLee
6

{
7
class LogTools
8
{
9
private string _eventSource = "";
10
private string _eventLog = "";
11
12
13
/**//// <summary>
14
/// 事件发生源
15
/// </summary>
16
public string EventSource
17
{
18
get
19
{
20
return _eventSource;
21
}
22
23
set
24
{
25
_eventSource = value;
26
}
27
}
28
29
/**//// <summary>
30
/// 日志名
31
/// </summary>
32
public string EventLog
33
{
34
get
35
{
36
return _eventLog;
37
}
38
39
set
40
{
41
_eventLog = value;
42
}
43
}
44
45
public void WriteToEventLog(Exception e, string action)
46
{
47
EventLog log = new EventLog();
48
log.Source = _eventSource;
49
log.Log = _eventLog;
50
51
string message = "数据源发生异常.\n\n";
52
message += "操作: " + action + "\n\n";
53
message += "异常: " + e.ToString();
54
55
log.WriteEntry(message);
56
}
57
}
58
}
59
随着编写的深入,从起初的只想学习一下MemshipProvider知识,变成了现在的要写一个实用的OracleMemshipProvider,这一切离不开关注本blog的朋友们,希望有兴趣的朋友参与进来,编写出基于其他数据库平台的MemshipProvider。比如:MySQLMemshipProvider等。
* 所有属性均可在web.config中配置
* 数据库操作异常写入日志
* 多了一个LogTools类,用于日志相关操作
* 自定义多个属性增加灵活性,比如可以用任意表名,
* 确定了表结构,并给出Oracle下的表创建语句( 使用本语句需要做简单修改,替换用户和表空间名)
(因为调试部署费时,所以代码均未经过测试,待代码编写完整后再测试)


1

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

596



597

598

599

600

601

602

603


604

605

606

607

608

609

610

611

612

613

614



615

616

617

618



619

620

621

622

623

624

625


626

627

628

629

630

631

632

633



634

635

636

637

638

639

640

641



642

643

644

645



646

647

648

649

650

651

652


653

654

655

656

657

658

659

660

661

662

663



664

665

666



667

668

669

670

671

672



673

674

675

676

677


678

679

680

681

682

683

684

685

686

687

688

689



690

691

692

693

694

695

696

697

698



1

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



1

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

随着编写的深入,从起初的只想学习一下MemshipProvider知识,变成了现在的要写一个实用的OracleMemshipProvider,这一切离不开关注本blog的朋友们,希望有兴趣的朋友参与进来,编写出基于其他数据库平台的MemshipProvider。比如:MySQLMemshipProvider等。
【推荐】国内首个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的设计模式综述