编写OracleMembershipProvider,让SharePoint2007使用Oralce中的用户数据实现Form验证。 (第四天)...

继续完善代码,以下代码与之前的比较多了如下特性:

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

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

 27None.gifnamespace BoooLee
 28ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 29InBlock.gif    public class OracleMembershipProvider : MembershipProvider
 30ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 31InBlock.gif        //OracleTools
 32InBlock.gif        private OracleTools OT = null;
 33InBlock.gif        
 34InBlock.gif        //自定义属性
 35InBlock.gif        private string _UsersTableName;        
 36InBlock.gif        //Oracle数据库连接串
 37InBlock.gif        private string _ConnectionString;
 38InBlock.gif        //是否允许事件日志
 39InBlock.gif        private bool _EnableEventLog;
 40InBlock.gif
 41InBlock.gif        //MembershipProvider接口属性,用于存放config.web中的MembershipProvider配置属性。
 42InBlock.gif        //使用自定义成员资格提供程序的应用程序的名称。
 43InBlock.gif        private string _ApplicationName;
 44InBlock.gif        //指示成员资格提供程序是否配置为允许用户重置其密码。
 45InBlock.gif        private bool _EnablePasswordReset;
 46InBlock.gif        //指示成员资格提供程序是否配置为允许用户检索其密码。
 47InBlock.gif        private bool _EnablePasswordRetrieval;
 48InBlock.gif        //获取锁定成员资格用户前允许的无效密码或无效密码提示问题答案尝试次数。
 49InBlock.gif        private int _MaxInvalidPasswordAttempts;
 50InBlock.gif        //获取有效密码中必须包含的最少特殊字符数。
 51InBlock.gif        private int _MinRequireNonAlphanumericCharacters;
 52InBlock.gif        //获取密码所要求的最小长度。
 53InBlock.gif        private int _MinRequiredPasswordLength;
 54InBlock.gif        //获取在锁定成员资格用户之前允许的最大无效密码或无效密码提示问题答案尝试次数的分钟数。
 55InBlock.gif        private int _PasswordAttemptWindow;
 56InBlock.gif        //获取一个值,该值指示在成员资格数据存储区中存储密码的格式。
 57InBlock.gif        private MembershipPasswordFormat _PasswordFormat;
 58InBlock.gif        //获取用于计算密码的正则表达式。
 59InBlock.gif        private string _PasswordStrengthRegularExpression;
 60InBlock.gif        //获取一个值,该值指示成员资格提供程序是否配置为要求用户在进行密码重置和检索时回答密码提示问题。
 61InBlock.gif        private bool _RequiresQuestionAndAnswer;
 62InBlock.gif        //获取一个值,指示成员资格提供程序是否配置为要求每个用户名具有唯一的电子邮件地址。
 63InBlock.gif        private bool _RequiresUniqueEmail;
 64InBlock.gif        
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
 66InBlock.gif        摘要:
 67InBlock.gif        初始化提供程序。
 68InBlock.gif        
 69InBlock.gif        参数:
 70InBlock.gif        config: 名称/值对的集合,表示在配置中为该提供程序指定的、提供程序特定的属性。
 71InBlock.gif        name: 该提供程序的友好名称。
 72ExpandedSubBlockEnd.gif         */

 73InBlock.gif        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 75InBlock.gif            base.Initialize(name, config);
 76InBlock.gif            
 77InBlock.gif            //读取MembershipProvider接口属性配置
 78InBlock.gif            _ApplicationName = GetConfigValue(config["ConnectionString"],System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
 79InBlock.gif            _EnablePasswordReset = Convert.ToBoolean(GetConfigValue(config["EnablePasswordReset"], "false"));
 80InBlock.gif            _EnablePasswordRetrieval = Convert.ToBoolean(GetConfigValue(config["EnablePasswordRetrieval"], "false"));
 81InBlock.gif            _MaxInvalidPasswordAttempts=Convert.ToInt32(GetConfigValue(config["MaxInvalidPasswordAttempts"],"5");
 82InBlock.gif            _MinRequireNonAlphanumericCharacters=Convert.ToInt32(GetConfigValue(config["MinRequiredNonAlphanumericCharacters"],"1");
 83InBlock.gif            _MinRequiredPasswordLength=Convert.ToInt32(GetConfigValue(config["MinRequiredPasswordLength"],"7");
 84InBlock.gif            _PasswordAttemptWindow=Convert.ToInt32(GetConfigValue(config["PasswordAttemptWindow"],"10");
 85InBlock.gif            _PasswordFormat=(MembershipPasswordFormat)GetConfigValue(config["PasswordFormat"],MembershipPasswordFormat.Clear);
 86InBlock.gif            _PasswordStrengthRegularExpression=GetConfigValue(config["PasswordStrengthRegularExpression"],""));
 87InBlock.gif            _RequiresQuestionAndAnswer=Convert.ToBoolean(GetConfigValue(config["RequiresQuestionAndAnswer"],"false"));
 88InBlock.gif            _RequiresUniqueEmail=Convert.ToBoolean(GetConfigValue(config["RequiresUniqueEmail"],"true"));
 89InBlock.gif            
 90InBlock.gif            //读取自定义属性
 91InBlock.gif            ConnectionStringSettings ConnectionStringSetting=ConfigurationManager.ConnectionStrings[config["ConnectionString"]];
 92InBlock.gif            _ConnectionString=ConnectionStringSetting.ConnectionString;
 93InBlock.gif            _UsersTableName=GetConfigValue(config["UsersTableName"],"SHAREPOINTUSERS");
 94InBlock.gif            _EnableEventLog=Convert.ToBoolean(GetConfigValue(config["EnableEventLog"],"true"];
 95InBlock.gif            //初始化OT
 96InBlock.gif            OT = new OracleTools();
 97InBlock.gif            OT.ConnectionString = _ConnectionString;
 98InBlock.gif
 99InBlock.gif            //为OT分配日志记录器
100InBlock.gif            LogTools lt = new LogTools();
101InBlock.gif            lt.EventLog = "Application";
102InBlock.gif            lt.EventSource = "OracleMembershipProvider";
103InBlock.gif            OT.LogTool = lt;
104InBlock.gif            OT.EnableEventlog=_EnableEventLog;
105ExpandedSubBlockEnd.gif        }

106InBlock.gif
107ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
108InBlock.gif        摘要:
109InBlock.gif        使用自定义成员资格提供程序的应用程序的名称。
110InBlock.gif        
111InBlock.gif        返回值:
112InBlock.gif        使用自定义成员资格提供程序的应用程序的名称。
113ExpandedSubBlockEnd.gif        */

114InBlock.gif        public override string ApplicationName
115ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
116InBlock.gif            get
117ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
118InBlock.gif                return _ApplicationName;
119ExpandedSubBlockEnd.gif            }

120InBlock.gif            set
121ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
122InBlock.gif                ;
123ExpandedSubBlockEnd.gif            }

124ExpandedSubBlockEnd.gif        }

125InBlock.gif
126ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
127InBlock.gif        摘要:
128InBlock.gif        处理更新成员资格用户密码的请求。
129InBlock.gif
130InBlock.gif        参数:
131InBlock.gif        newPassword: 指定的用户的新密码。
132InBlock.gif        oldPassword: 指定的用户的当前密码。
133InBlock.gif        username: 为其更新密码的用户。
134InBlock.gif
135InBlock.gif        返回值:
136InBlock.gif        如果密码更新成功,则为 true;否则为 false。
137ExpandedSubBlockEnd.gif        */

138InBlock.gif        public override bool ChangePassword(string username, string oldPassword, string newPassword)
139ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
140InBlock.gif            string SqlString = "";
141InBlock.gif
142InBlock.gif            //更改用户密码
143InBlock.gif            if (UserExist(username,oldPassword)>0)
144ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
145InBlock.gif                SqlString = string.Format("update {0} set password={1} where username='{2}' and password='{3}'",_TableName, newPassword, username, oldPassword);
146InBlock.gif                return OT.RunSqlNonQuery(SqlString);
147ExpandedSubBlockEnd.gif            }

148InBlock.gif            
149InBlock.gif            return false;
150ExpandedSubBlockEnd.gif        }

151InBlock.gif
152ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
153InBlock.gif        摘要:
154InBlock.gif        处理更新成员资格用户的密码提示问题和答案的请求。
155InBlock.gif
156InBlock.gif        参数:
157InBlock.gif        newPasswordQuestion: 指定的用户的新密码提示问题。
158InBlock.gif        newPasswordAnswer: 指定的用户的新密码提示问题答案。
159InBlock.gif        username: 要为其更改密码提示问题和答案的用户。
160InBlock.gif        password: 指定的用户的密码。
161InBlock.gif
162InBlock.gif        返回值:
163InBlock.gif        如果成功更新密码提示问题和答案,则为 true;否则,为 false。
164ExpandedSubBlockEnd.gif         */

165InBlock.gif        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
166ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
167InBlock.gif            string SqlString = "";
168InBlock.gif
169InBlock.gif            //更改用户密码问题和密码答案
170InBlock.gif            if (UserExist(username,password)>0)
171ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
172InBlock.gif                SqlString = string.Format("update {0} set question={1} , answer={2} where username='{3}' and password='{4}'", _TableName,newPasswordQuestion, newPasswordAnswer, username, password);
173InBlock.gif                return OT.RunSqlNonQuery(SqlString);
174ExpandedSubBlockEnd.gif            }

175InBlock.gif
176InBlock.gif            return false;
177ExpandedSubBlockEnd.gif        }

178InBlock.gif
179ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
180InBlock.gif        摘要:
181InBlock.gif        将新的成员资格用户添加到数据源。
182InBlock.gif
183InBlock.gif        参数:
184InBlock.gif        isApproved: 是否允许验证新用户。
185InBlock.gif        passwordAnswer: 新用户的密码提示问题答案。
186InBlock.gif        username: 新用户的用户名。
187InBlock.gif        providerUserKey: 成员资格数据源中该用户的唯一标识符。
188InBlock.gif        password: 新用户的密码。
189InBlock.gif        passwordQuestion: 新用户的密码提示问题。
190InBlock.gif        email: 新用户的电子邮件地址。
191InBlock.gif        status: 一个 System.Web.Security.MembershipCreateStatus 枚举值,指示是否已成功创建用户。
192InBlock.gif
193InBlock.gif        返回值:
194InBlock.gif        一个用新创建的用户的信息填充的 System.Web.Security.MembershipUser 对象。
195ExpandedSubBlockEnd.gif        */

196InBlock.gif        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
197ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
198InBlock.gif            //TODO 实现创建用户代码
199InBlock.gif            throw new Exception("The method or operation is not implemented.");
200ExpandedSubBlockEnd.gif        }

201InBlock.gif
202ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
203InBlock.gif        摘要:
204InBlock.gif        从成员资格数据源删除一个用户。
205InBlock.gif
206InBlock.gif        参数:
207InBlock.gif        username: 要删除的用户的名称。
208InBlock.gif        deleteAllRelatedData: 如果为 true,则从数据库中删除与该用户相关的数据;如果为 false,则将与该用户相关的数据保留在数据库。
209InBlock.gif
210InBlock.gif        返回值:
211InBlock.gif        如果用户被成功删除,则为 true;否则为 false。
212ExpandedSubBlockEnd.gif        */

213InBlock.gif        public override bool DeleteUser(string username, bool deleteAllRelatedData)
214ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
215InBlock.gif            string SqlString = "";
216InBlock.gif
217InBlock.gif            //删除用户
218InBlock.gif            if (UserExist(username)>0)
219ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
220InBlock.gif                SqlString = string.Format("delete from {0} where username='{1}'",_TableName, username);
221InBlock.gif                return OT.RunSqlNonQuery(SqlString);
222ExpandedSubBlockEnd.gif            }

223InBlock.gif
224InBlock.gif            return false;
225ExpandedSubBlockEnd.gif        }

226InBlock.gif
227ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
228InBlock.gif        摘要:
229InBlock.gif        指示成员资格提供程序是否配置为允许用户重置其密码。
230InBlock.gif
231InBlock.gif        返回值:
232InBlock.gif        如果成员资格提供程序支持密码重置,则为 true;否则为 false。默认为 true。
233ExpandedSubBlockEnd.gif         */

234InBlock.gif        public override bool EnablePasswordReset
235ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
236InBlock.gif            get 
237ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
238InBlock.gif                return _EnablePasswordReset;
239ExpandedSubBlockEnd.gif            }

240ExpandedSubBlockEnd.gif        }

241InBlock.gif        
242ExpandedSubBlockStart.gifContractedSubBlock.gif         /**//*
243InBlock.gif        摘要:
244InBlock.gif        指示成员资格提供程序是否配置为允许用户检索其密码。
245InBlock.gif
246InBlock.gif        返回值:
247InBlock.gif        如果成员资格提供程序配置为支持密码检索,则为 true,否则为 false。默认为 false。
248ExpandedSubBlockEnd.gif        */

249InBlock.gif        public override bool EnablePasswordRetrieval
250ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
251InBlock.gif            get 
252ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
253InBlock.gif                return _EnablePasswordRetrieval;    
254ExpandedSubBlockEnd.gif            }

255ExpandedSubBlockEnd.gif        }

256InBlock.gif
257ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
258InBlock.gif        摘要:
259InBlock.gif        获取一个成员资格用户的集合,这些用户的电子邮件地址包含要匹配的指定电子邮件地址。
260InBlock.gif
261InBlock.gif        参数:
262InBlock.gif        totalRecords: 匹配用户的总数。
263InBlock.gif        pageIndex: 要返回的结果页的索引。pageIndex 是从零开始的。
264InBlock.gif        emailToMatch: 要搜索的电子邮件地址。
265InBlock.gif        pageSize: 要返回的结果页的大小。
266InBlock.gif
267InBlock.gif        返回值:
268InBlock.gif        包含一页 pageSizeSystem.Web.Security.MembershipUser 对象的 System.Web.Security.MembershipUserCollection 集合,这些对象从 pageIndex 指定的页开始。
269ExpandedSubBlockEnd.gif        */

270InBlock.gif        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
271ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
272InBlock.gif            string SqlString = "";
273InBlock.gif            MembershipUserCollection muc = new MembershipUserCollection();
274InBlock.gif            totalRecords = UserExistByMail(emailToMatch);
275InBlock.gif            if (totalRecords>0)
276ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
277InBlock.gif                SqlString = string.Format("select * from {0} where email='{1}'", _TableName,emailToMatch);
278InBlock.gif                muc = ToMembershipUserCollection(OT.RunSqlDataTable(SqlString));
279ExpandedSubBlockEnd.gif            }

280InBlock.gif            return muc;
281ExpandedSubBlockEnd.gif        }

282InBlock.gif
283ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
284InBlock.gif        摘要:
285InBlock.gif        获取一个成员资格用户的集合,这些用户的用户名包含要匹配的指定用户名。
286InBlock.gif
287InBlock.gif        参数:
288InBlock.gif        totalRecords: 匹配用户的总数。
289InBlock.gif        pageIndex: 要返回的结果页的索引。pageIndex 是从零开始的。
290InBlock.gif        usernameToMatch: 要搜索的用户名。
291InBlock.gif        pageSize: 要返回的结果页的大小。
292InBlock.gif
293InBlock.gif        返回值:
294InBlock.gif        包含一页 pageSizeSystem.Web.Security.MembershipUser 对象的 System.Web.Security.MembershipUserCollection 集合,这些对象从 pageIndex 指定的页开始。
295ExpandedSubBlockEnd.gif        */

296InBlock.gif         public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
297ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
298InBlock.gif            string SqlString = "";
299InBlock.gif            MembershipUserCollection muc = new MembershipUserCollection();
300InBlock.gif            totalRecords = UserExist(usernameToMatch);
301InBlock.gif            if (totalRecords > 0)
302ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
303InBlock.gif                SqlString = string.Format("select * from {0} where username='{1}'", _TableName,usernameToMatch);
304InBlock.gif                muc=ToMembershipUserCollection( OT.RunSqlDataTable(SqlString));
305ExpandedSubBlockEnd.gif            }

306InBlock.gif            return muc;
307ExpandedSubBlockEnd.gif        }

308InBlock.gif
309ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
310InBlock.gif        摘要:
311InBlock.gif        获取数据源中的所有用户的集合,并显示在数据页中。
312InBlock.gif
313InBlock.gif        参数:
314InBlock.gif        totalRecords: 匹配用户的总数。
315InBlock.gif        pageIndex: 要返回的结果页的索引。pageIndex 是从零开始的。
316InBlock.gif        pageSize: 要返回的结果页的大小。
317InBlock.gif
318InBlock.gif        返回值:
319InBlock.gif        包含一页 pageSizeSystem.Web.Security.MembershipUser 对象的 System.Web.Security.MembershipUserCollection 集合,这些对象从 pageIndex 指定的页开始。
320ExpandedSubBlockEnd.gif        */

321InBlock.gif        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
322ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
323InBlock.gif            string SqlString = "";
324InBlock.gif            MembershipUserCollection muc = new MembershipUserCollection();
325InBlock.gif            totalRecords = UsersTotalRecords();
326InBlock.gif            if (totalRecords > 0)
327ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
328InBlock.gif                SqlString =string.Format( "select * from {0}",_TableName);
329InBlock.gif                muc = ToMembershipUserCollection(OT.RunSqlDataTable(SqlString));
330ExpandedSubBlockEnd.gif            }

331InBlock.gif            return muc;
332ExpandedSubBlockEnd.gif        }

333InBlock.gif
334ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
335InBlock.gif        摘要:
336InBlock.gif        获取当前访问该应用程序的用户数。
337InBlock.gif
338InBlock.gif        返回值:
339InBlock.gif        当前访问该应用程序的用户数。
340ExpandedSubBlockEnd.gif        */

341InBlock.gif        public override int GetNumberOfUsersOnline()
342ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
343InBlock.gif            //TODO 获取在线人数
344InBlock.gif            throw new Exception("The method or operation is not implemented.");
345ExpandedSubBlockEnd.gif        }

346InBlock.gif
347ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
348InBlock.gif        摘要:
349InBlock.gif        从数据源获取指定用户名所对应的密码。
350InBlock.gif
351InBlock.gif        参数:
352InBlock.gif        username: 为其检索密码的用户。
353InBlock.gif        answer: 用户的密码提示问题答案。
354InBlock.gif
355InBlock.gif        返回值:
356InBlock.gif        指定用户名所对应的密码。
357ExpandedSubBlockEnd.gif        */

358InBlock.gif        public override string GetPassword(string username, string answer)
359ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
360InBlock.gif            string SqlString = "";
361InBlock.gif            string password="";
362InBlock.gif            if (UserExist(username) > 0)
363ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
364InBlock.gif                SqlString = string.Format("select password from {0} where username='{1}' and answer='{2}'", _TableName,username, answer);
365InBlock.gif                password = OT.RunSqlScalar(SqlString);
366ExpandedSubBlockEnd.gif            }

367InBlock.gif            return password;
368ExpandedSubBlockEnd.gif        }

369InBlock.gif
370ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
371InBlock.gif        摘要:
372InBlock.gif        从数据源获取用户的信息。提供一个更新用户最近一次活动的日期/时间戳的选项。
373InBlock.gif
374InBlock.gif        参数:
375InBlock.gif        username: 要获取其信息的用户名。
376InBlock.gif        userIsOnline: 如果为 true,则更新用户最近一次活动的日期/时间戳;如果为 false,则返回用户信息,但不更新用户最近一次活动的日期/时间戳。
377InBlock.gif
378InBlock.gif        返回值:
379InBlock.gif        用数据源中指定用户的信息填充的 System.Web.Security.MembershipUser 对象。
380ExpandedSubBlockEnd.gif        */

381InBlock.gif        public override MembershipUser GetUser(string username, bool userIsOnline)
382ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
383InBlock.gif            string SqlString = "";
384InBlock.gif            MembershipUser mu = null;
385InBlock.gif
386InBlock.gif            if (UserExist(username) > 0)
387ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
388InBlock.gif                SqlString = string.Format("select * from {0} where username='{1}'", _TableName,username);                
389InBlock.gif                mu=ToMembershipUser(OT.RunSqlDataTable(SqlString).Rows[0]);
390ExpandedSubBlockEnd.gif            }

391InBlock.gif
392InBlock.gif            return mu;
393ExpandedSubBlockEnd.gif        }

394InBlock.gif
395ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
396InBlock.gif        摘要:
397InBlock.gif        根据成员资格用户的唯一标识符从数据源获取该用户的信息。提供一个更新用户最近一次活动的日期/时间戳的选项。
398InBlock.gif
399InBlock.gif        参数:
400InBlock.gif        providerUserKey: 要获取其信息的成员资格用户的唯一标识符。
401InBlock.gif        userIsOnline: 如果为 true,则更新用户最近一次活动的日期/时间戳;如果为 false,则返回用户信息,但不更新用户最近一次活动的日期/时间戳。
402InBlock.gif
403InBlock.gif        返回值:
404InBlock.gif        用数据源中指定用户的信息填充的 System.Web.Security.MembershipUser 对象。
405ExpandedSubBlockEnd.gif        */

406InBlock.gif        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
407ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
408InBlock.gif            string SqlString = "";
409InBlock.gif            MembershipUser mu = new MembershipUser();
410InBlock.gif
411InBlock.gif            if (UserExist(username) > 0)
412ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
413InBlock.gif                SqlString = string.Format("select * from {0} where provideruserkey='{1}'", _TableName,providerUserKey.ToString());
414InBlock.gif                mu = ToMembershipUser(OT.RunSqlDataTable(SqlString).Rows[0]);
415ExpandedSubBlockEnd.gif            }

416InBlock.gif
417InBlock.gif            return mu;
418ExpandedSubBlockEnd.gif        }

419InBlock.gif
420InBlock.gif        public override string GetUserNameByEmail(string email)
421ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
422InBlock.gif            throw new Exception("The method or operation is not implemented.");
423ExpandedSubBlockEnd.gif        }

424InBlock.gif
425InBlock.gif        public override int MaxInvalidPasswordAttempts
426ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
427InBlock.gif            get
428ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
429InBlock.gif                return _MaxInvalidPasswordAttempts;
430ExpandedSubBlockEnd.gif            }

431ExpandedSubBlockEnd.gif        }

432InBlock.gif
433InBlock.gif        public override int MinRequiredNonAlphanumericCharacters
434ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
435InBlock.gif            get
436ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
437InBlock.gif                return _MinRequireNonAlphanumericCharacters;
438ExpandedSubBlockEnd.gif            }

439InBlock.gif
440ExpandedSubBlockEnd.gif        }

441InBlock.gif
442InBlock.gif        public override int MinRequiredPasswordLength
443ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
444InBlock.gif            get
445ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
446InBlock.gif                return _MinRequiredPasswordLength;
447ExpandedSubBlockEnd.gif            }

448ExpandedSubBlockEnd.gif        }

449InBlock.gif
450InBlock.gif        public override int PasswordAttemptWindow
451ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
452InBlock.gif            get
453ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
454InBlock.gif                return _PasswordAttemptWindow;
455ExpandedSubBlockEnd.gif            }

456ExpandedSubBlockEnd.gif        }

457InBlock.gif
458InBlock.gif        public override MembershipPasswordFormat PasswordFormat
459ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
460InBlock.gif            get
461ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
462InBlock.gif                return _PasswordFormat;
463ExpandedSubBlockEnd.gif            }

464ExpandedSubBlockEnd.gif        }

465InBlock.gif
466InBlock.gif        public override string PasswordStrengthRegularExpression
467ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
468InBlock.gif            get
469ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
470InBlock.gif                return PasswordStrengthRegularExpression;
471ExpandedSubBlockEnd.gif            }

472ExpandedSubBlockEnd.gif        }

473InBlock.gif
474InBlock.gif        public override bool RequiresQuestionAndAnswer
475ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
476InBlock.gif            get
477ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
478InBlock.gif                return RequiresQuestionAndAnswer;
479ExpandedSubBlockEnd.gif            }

480ExpandedSubBlockEnd.gif        }

481InBlock.gif
482InBlock.gif        public override bool RequiresUniqueEmail
483ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
484InBlock.gif            get
485ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
486InBlock.gif                return RequiresUniqueEmail;
487ExpandedSubBlockEnd.gif            }

488ExpandedSubBlockEnd.gif        }

489InBlock.gif
490InBlock.gif        public override string ResetPassword(string username, string answer)
491ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
492InBlock.gif            throw new Exception("The method or operation is not implemented.");
493ExpandedSubBlockEnd.gif        }

494InBlock.gif
495InBlock.gif        public override bool UnlockUser(string userName)
496ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
497InBlock.gif            throw new Exception("The method or operation is not implemented.");
498ExpandedSubBlockEnd.gif        }

499InBlock.gif
500InBlock.gif        public override void UpdateUser(MembershipUser user)
501ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
502InBlock.gif            throw new Exception("The method or operation is not implemented.");
503ExpandedSubBlockEnd.gif        }

504InBlock.gif
505InBlock.gif        public override bool ValidateUser(string username, string password)
506ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
507InBlock.gif            throw new Exception("The method or operation is not implemented.");
508ExpandedSubBlockEnd.gif        }

509InBlock.gif
510ContractedSubBlock.gifExpandedSubBlockStart.gif非MembershipProvider成员#region 非MembershipProvider成员
511InBlock.gif
512ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
513InBlock.gif        摘要:
514InBlock.gif        判断用户是否存在
515InBlock.gif
516InBlock.gif        参数:
517InBlock.gif        username:用户名 
518InBlock.gif
519InBlock.gif        返回值:
520InBlock.gif        找到用户返回true,没有找到用户false
521ExpandedSubBlockEnd.gif         */

522InBlock.gif        private int UserExist(string username)
523ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
524InBlock.gif            string SqlString = "";
525InBlock.gif            string r = "";
526InBlock.gif            int Count = 0;
527InBlock.gif
528InBlock.gif            SqlString = string.Format("select count(*) from {0} where username='{1}'", _TableName,username);
529InBlock.gif            r = OT.RunSqlScalar(SqlString);
530InBlock.gif            try
531ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
532InBlock.gif                Count = int.Parse(r);
533ExpandedSubBlockEnd.gif            }

534InBlock.gif            catch (Exception)
535ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
536InBlock.gif                Count = 0;
537ExpandedSubBlockEnd.gif            }

538InBlock.gif
539InBlock.gif            return Count;
540ExpandedSubBlockEnd.gif        }

541InBlock.gif
542ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
543InBlock.gif        摘要:
544InBlock.gif        判断用户是否存在
545InBlock.gif
546InBlock.gif        参数:
547InBlock.gif        username:用户名
548InBlock.gif        password:用户密码
549InBlock.gif
550InBlock.gif        返回值:
551InBlock.gif        找到用户的用户数。
552ExpandedSubBlockEnd.gif         */

553InBlock.gif        private int UserExist(string username, string password)
554ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
555InBlock.gif            string SqlString = "";
556InBlock.gif            string r = "";
557InBlock.gif            int Count = 0;
558InBlock.gif
559InBlock.gif            SqlString = string.Format("select count(*) from {0} where username='{1}' and password='{2}'",_TableName, username,password);
560InBlock.gif            r = OT.RunSqlScalar(SqlString);
561InBlock.gif            try
562ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
563InBlock.gif                Count = int.Parse(r);
564ExpandedSubBlockEnd.gif            }

565InBlock.gif            catch (Exception)
566ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
567InBlock.gif                Count = 0;
568ExpandedSubBlockEnd.gif            }

569InBlock.gif
570InBlock.gif            return Count;
571ExpandedSubBlockEnd.gif        }

572InBlock.gif
573ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
574InBlock.gif        摘要:
575InBlock.gif        判断用户是否存在
576InBlock.gif
577InBlock.gif        参数:
578InBlock.gif        email:用户名        
579InBlock.gif
580InBlock.gif        返回值:
581InBlock.gif        找到用户的用户数。
582ExpandedSubBlockEnd.gif         */

583InBlock.gif        private int UserExistByMail(string email)
584ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
585InBlock.gif            string SqlString = "";
586InBlock.gif            string r = "";
587InBlock.gif            int Count = 0;
588InBlock.gif
589InBlock.gif            SqlString = string.Format("select count(*) from {0} where email='{1}'", _TableName,email);
590InBlock.gif            r = OT.RunSqlScalar(SqlString);
591InBlock.gif            try
592ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
593InBlock.gif                Count = int.Parse(r);
594ExpandedSubBlockEnd.gif            }

595InBlock.gif            catch (Exception)
596ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
597InBlock.gif                Count = 0;
598ExpandedSubBlockEnd.gif            }

599InBlock.gif
600InBlock.gif            return Count;
601ExpandedSubBlockEnd.gif        }

602InBlock.gif
603ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
604InBlock.gif        摘要:
605InBlock.gif        转换用户数据表为MembershipUserCollection
606InBlock.gif
607InBlock.gif        参数:
608InBlock.gif        userstable:用户表
609InBlock.gif
610InBlock.gif        返回值:
611InBlock.gif        返回包含用户数据的MembershipUserCollection。
612ExpandedSubBlockEnd.gif         */

613InBlock.gif        private MembershipUserCollection ToMembershipUserCollection(DataTable userstable)
614ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
615InBlock.gif            MembershipUserCollection muc = new MembershipUserCollection();
616InBlock.gif
617InBlock.gif            foreach (DataRow dr in userstable.Rows)
618ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
619InBlock.gif                muc.Add(ToMembershipUser(dr));
620ExpandedSubBlockEnd.gif            }

621InBlock.gif
622InBlock.gif            return muc;
623ExpandedSubBlockEnd.gif        }

624InBlock.gif
625ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
626InBlock.gif        摘要:
627InBlock.gif        获得用户总数
628InBlock.gif
629InBlock.gif        返回值:
630InBlock.gif        返回用户数。
631ExpandedSubBlockEnd.gif         */

632InBlock.gif        private int UsersTotalRecords()
633ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
634InBlock.gif            string SqlString = "";
635InBlock.gif            string r = "";
636InBlock.gif            int Count = 0;
637InBlock.gif
638InBlock.gif            SqlString = string.Format("select count(*) from {0}",_TableName);
639InBlock.gif            r = OT.RunSqlScalar(SqlString);
640InBlock.gif            try
641ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
642InBlock.gif                Count = int.Parse(r);
643ExpandedSubBlockEnd.gif            }

644InBlock.gif            catch (Exception)
645ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
646InBlock.gif                Count = 0;
647ExpandedSubBlockEnd.gif            }

648InBlock.gif
649InBlock.gif            return Count;
650ExpandedSubBlockEnd.gif        }

651InBlock.gif
652ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
653InBlock.gif        摘要:
654InBlock.gif        转换用户数据表为MembershipUserCollection
655InBlock.gif
656InBlock.gif        参数:
657InBlock.gif        usersrecord:用户记录
658InBlock.gif
659InBlock.gif        返回值:
660InBlock.gif        返回包含用户数据的MembershipUser。
661ExpandedSubBlockEnd.gif         */

662InBlock.gif        private MembershipUser ToMembershipUser(DataRow usersrecord)
663ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
664InBlock.gif            MembershipUser mu = new MembershipUser();
665InBlock.gif            try
666ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
667InBlock.gif                mu.UserName = usersrecord["username"];
668InBlock.gif                mu.Email = usersrecord["email"];
669InBlock.gif                mu.PasswordQuestion = usersrecord["question"];
670ExpandedSubBlockEnd.gif            }

671InBlock.gif            catch (Exception)
672ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
673ExpandedSubBlockEnd.gif            }

674InBlock.gif            return mu;
675ExpandedSubBlockEnd.gif        }

676InBlock.gif
677ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
678InBlock.gif        摘要:
679InBlock.gif        获取配置文件
680InBlock.gif
681InBlock.gif        参数:
682InBlock.gif        ConfigValue:配置文件中的参数设置
683InBlock.gif        DefaultValue:缺省值
684InBlock.gif        
685InBlock.gif        返回值:
686InBlock.gif        如果ConfigValue值有效则返回ConfigValue,否则返回DefaultValue。
687ExpandedSubBlockEnd.gif         */

688InBlock.gif        private string GetConfigValue(string ConfigValue, string DefaultValue)
689ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
690InBlock.gif            if (string.IsNullOrEmpty(ConfigValue))
691InBlock.gif                return DefaultValue;
692InBlock.gif            return ConfigValue;
693ExpandedSubBlockEnd.gif        }

694ExpandedSubBlockEnd.gif#endregion

695InBlock.gif        
696ExpandedSubBlockEnd.gif    }

697ExpandedBlockEnd.gif}

698None.gif

ContractedBlock.gif ExpandedBlockStart.gif OracleTools
  1None.gifusing System;
  2None.gifusing System.Collections.Generic;
  3None.gifusing System.Text;
  4None.gifusing System.Data;
  5None.gifusing System.Data.OleDb;
  6None.gif
  7None.gifnamespace BoooLee
  8ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  9InBlock.gif    class OracleTools
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 11ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 12InBlock.gif        /// 私有变量
 13ExpandedSubBlockEnd.gif        /// </summary>

 14InBlock.gif        private string _ConnectionString = "";  //数据库连接串
 15InBlock.gif
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 17InBlock.gif        /// 日志记录器
 18ExpandedSubBlockEnd.gif        /// </summary>

 19InBlock.gif        private LogTools _LogTools = null;
 20InBlock.gif
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 22InBlock.gif        /// 是否允许记录日志
 23ExpandedSubBlockEnd.gif        /// </summary>

 24InBlock.gif        private bool _EnableEventLog = false;
 25InBlock.gif
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 27InBlock.gif        /// 是否允许记录日志
 28ExpandedSubBlockEnd.gif        /// </summary>

 29InBlock.gif        public bool EnableEventlog
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 31InBlock.gif            get
 32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 33InBlock.gif                return _EnableEventLog;
 34ExpandedSubBlockEnd.gif            }

 35InBlock.gif
 36InBlock.gif            set
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 38InBlock.gif                _EnableEventLog = value;
 39ExpandedSubBlockEnd.gif            }

 40ExpandedSubBlockEnd.gif        }

 41InBlock.gif
 42ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 43InBlock.gif        /// 日志记录器
 44ExpandedSubBlockEnd.gif        /// </summary>

 45InBlock.gif        public LogTools LogTool
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            get
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 49InBlock.gif                return _LogTools;
 50ExpandedSubBlockEnd.gif            }

 51InBlock.gif
 52InBlock.gif            set
 53ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 54InBlock.gif                _LogTools = value;
 55ExpandedSubBlockEnd.gif            }

 56ExpandedSubBlockEnd.gif        }

 57ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 58InBlock.gif        /// 数据库连接串
 59ExpandedSubBlockEnd.gif        /// </summary>

 60InBlock.gif        public string ConnectionString
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 62InBlock.gif            get
 63ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 64InBlock.gif                return _ConnectionString;
 65ExpandedSubBlockEnd.gif            }

 66InBlock.gif
 67InBlock.gif            set
 68ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 69InBlock.gif                _ConnectionString=value;
 70ExpandedSubBlockEnd.gif            }

 71ExpandedSubBlockEnd.gif        }

 72InBlock.gif
 73ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 74InBlock.gif        /// 运行无返回值的Sql语句。
 75InBlock.gif        /// </summary>
 76InBlock.gif        /// <param name="SqlString">指定要运行的Sql语句。</param>
 77InBlock.gif        /// <param name="Conn">指定数据库连接。</param>
 78ExpandedSubBlockEnd.gif        /// <returns>成功返回true,失败返回false。</returns>

 79InBlock.gif        public bool RunSqlNonQuery(string SqlString)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 81InBlock.gif            OleDbConnection Conn=null;
 82InBlock.gif
 83InBlock.gif            try
 84ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 85InBlock.gif                using (OleDbCommand Command = new OleDbCommand())
 86ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 87InBlock.gif                    Command.CommandText = SqlString;
 88InBlock.gif                    Conn = GetConnection();
 89InBlock.gif                    Conn.Open();
 90InBlock.gif                    Command.Connection = Conn;                    
 91InBlock.gif                    Command.ExecuteNonQuery();
 92ExpandedSubBlockEnd.gif                }

 93InBlock.gif                return true;
 94ExpandedSubBlockEnd.gif            }

 95InBlock.gif            catch (Exception ex)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 97InBlock.gif                if (_LogTools != null && _EnableEventLog)
 98ExpandedSubBlockStart.gifContractedSubBlock.gif                    _LogTools.WriteToEventLog(ex, string.Format("RunSqlNonQuery(\"dot.gif{0}\")", SqlString));
 99InBlock.gif                return false;
100ExpandedSubBlockEnd.gif            }

101InBlock.gif            finally 
102ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
103InBlock.gif                //关闭数据库连接
104InBlock.gif                if (Conn != null && Conn.State==ConnectionState.Open)
105InBlock.gif                    Conn.Close();
106ExpandedSubBlockEnd.gif            }

107ExpandedSubBlockEnd.gif        }

108InBlock.gif
109ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
110InBlock.gif        /// 运行返回一个值的Select语句。
111InBlock.gif        /// </summary>
112InBlock.gif        /// <param name="SqlString">指定要运行的Sql语句。</param>
113InBlock.gif        /// <param name="Conn">指定数据库连接。</param>
114ExpandedSubBlockEnd.gif        /// <returns>成功返回Select语句结果,失败返回空字符串。</returns>

115InBlock.gif        public string RunSqlScalar(string SqlString)
116ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
117InBlock.gif            OleDbConnection Conn = null;
118InBlock.gif
119InBlock.gif            try
120ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
121InBlock.gif                Conn = GetConnection();
122InBlock.gif                Conn.Open();
123InBlock.gif
124InBlock.gif                using (OleDbCommand Command = new OleDbCommand())
125ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
126InBlock.gif                    Command.CommandText = SqlString;
127InBlock.gif                    Command.Connection = Conn;
128InBlock.gif                    return Command.ExecuteScalar().ToString();
129ExpandedSubBlockEnd.gif                }

130ExpandedSubBlockEnd.gif            }

131InBlock.gif            catch (Exception ex)
132ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
133InBlock.gif                if (_LogTools != null && _EnableEventLog)
134ExpandedSubBlockStart.gifContractedSubBlock.gif                    _LogTools.WriteToEventLog(ex, string.Format("RunSqlScalar(\"dot.gif{0}\")",SqlString));
135InBlock.gif                return "";
136ExpandedSubBlockEnd.gif            }

137InBlock.gif            finally
138ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
139InBlock.gif                if (Conn != null && Conn.State == ConnectionState.Open)
140InBlock.gif                    Conn.Close();
141ExpandedSubBlockEnd.gif            }

142ExpandedSubBlockEnd.gif        }

143InBlock.gif
144ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
145InBlock.gif        /// 运行返回DataTable的Sql语句。
146InBlock.gif        /// </summary>
147InBlock.gif        /// <param name="SqlString">指定要运行的Sql语句。</param>
148InBlock.gif        /// <param name="Conn">指定数据库连接。</param>
149ExpandedSubBlockEnd.gif        /// <returns>成功返回Select语句结果DataTable。</returns>

150InBlock.gif        public DataTable RunSqlDataTable(string SqlString)
151ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
152InBlock.gif            OleDbConnection Conn = null;
153InBlock.gif
154InBlock.gif            try
155ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
156InBlock.gif                using (OleDbDataAdapter DataAdapter = new OleDbDataAdapter())
157ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
158InBlock.gif                    Conn = GetConnection();
159InBlock.gif                    Conn.Open();
160InBlock.gif                    OleDbCommand SC = new OleDbCommand();
161InBlock.gif                    SC.CommandText = SqlString;
162InBlock.gif                    SC.Connection = Conn;
163InBlock.gif                    DataAdapter.SelectCommand = SC;
164InBlock.gif                    DataTable dt = new DataTable();
165InBlock.gif                    DataAdapter.Fill(dt);
166InBlock.gif                    return dt;
167ExpandedSubBlockEnd.gif                }

168ExpandedSubBlockEnd.gif            }

169InBlock.gif            catch (Exception ex)
170ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
171InBlock.gif                if (_LogTools != null && _EnableEventLog)
172ExpandedSubBlockStart.gifContractedSubBlock.gif                    _LogTools.WriteToEventLog(ex, string.Format("RunSqlDataTable(\"dot.gif{0}\")", SqlString));
173InBlock.gif                return new DataTable();
174ExpandedSubBlockEnd.gif            }

175InBlock.gif            finally
176ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
177InBlock.gif                if (Conn != null && Conn.State == ConnectionState.Open)
178InBlock.gif                    Conn.Close();
179ExpandedSubBlockEnd.gif            }

180ExpandedSubBlockEnd.gif        }

181InBlock.gif
182ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
183InBlock.gif        /// 返回一个OleDbConnection对象。
184InBlock.gif        /// </summary>
185ExpandedSubBlockEnd.gif        /// <returns>成功返回的OleDbConnection对象,失败返回null。</returns>

186InBlock.gif        public OleDbConnection GetConnection()
187ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
188InBlock.gif            try
189ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
190InBlock.gif                OleDbConnection result = new OleDbConnection();
191InBlock.gif                result.ConnectionString = _ConnectionString;
192InBlock.gif                return result;
193ExpandedSubBlockEnd.gif            }

194InBlock.gif            catch (Exception ex)
195ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
196InBlock.gif                if (_LogTools != null && _EnableEventLog)
197InBlock.gif                    _LogTools.WriteToEventLog(ex, "GetConnection()");
198InBlock.gif                return null;
199ExpandedSubBlockEnd.gif            }

200ExpandedSubBlockEnd.gif        }

201ExpandedSubBlockEnd.gif    }

202ExpandedBlockEnd.gif}

203None.gif

ContractedBlock.gif ExpandedBlockStart.gif LogTools
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace BoooLee
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    class LogTools
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        private string _eventSource = "";
10InBlock.gif        private string _eventLog = "";
11InBlock.gif
12InBlock.gif
13ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
14InBlock.gif        /// 事件发生源
15ExpandedSubBlockEnd.gif        /// </summary>

16InBlock.gif        public string EventSource
17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
18InBlock.gif            get
19ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
20InBlock.gif                return _eventSource;
21ExpandedSubBlockEnd.gif            }

22InBlock.gif
23InBlock.gif            set
24ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
25InBlock.gif                _eventSource = value;
26ExpandedSubBlockEnd.gif            }

27ExpandedSubBlockEnd.gif        }

28InBlock.gif
29ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
30InBlock.gif        /// 日志名
31ExpandedSubBlockEnd.gif        /// </summary>

32InBlock.gif        public string EventLog
33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
34InBlock.gif            get
35ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
36InBlock.gif                return _eventLog;
37ExpandedSubBlockEnd.gif            }

38InBlock.gif
39InBlock.gif            set
40ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
41InBlock.gif                _eventLog = value;
42ExpandedSubBlockEnd.gif            }

43ExpandedSubBlockEnd.gif        }

44InBlock.gif
45InBlock.gif        public void WriteToEventLog(Exception e, string action)
46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
47InBlock.gif            EventLog log = new EventLog();
48InBlock.gif            log.Source = _eventSource;
49InBlock.gif            log.Log = _eventLog;
50InBlock.gif
51InBlock.gif            string message = "数据源发生异常.\n\n";
52InBlock.gif            message += "操作: " + action + "\n\n";
53InBlock.gif            message += "异常: " + e.ToString();
54InBlock.gif
55InBlock.gif            log.WriteEntry(message);
56ExpandedSubBlockEnd.gif        }

57ExpandedSubBlockEnd.gif    }

58ExpandedBlockEnd.gif}

59None.gif

随着编写的深入,从起初的只想学习一下MemshipProvider知识,变成了现在的要写一个实用的OracleMemshipProvider,这一切离不开关注本blog的朋友们,希望有兴趣的朋友参与进来,编写出基于其他数据库平台的MemshipProvider。比如:MySQLMemshipProvider等。

转载于:https://www.cnblogs.com/booolee/archive/2007/01/29/633447.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值