代码改变世界

Patterns

2012-11-22 11:00  stubman  阅读(430)  评论(0编辑  收藏  举报

 

  1 #region Copyright
  2 /*
  3  File Name(文件名):                      DBFactory.cs
  4  Storage Path(存储路径):                 Core\Ado.Net\
  5  Author(作者):                           Stubman 
  6  File Action describe(文件功能描述):     ADO.NET数据库抽象工厂。
  7  File Memo(备注):
  8  */
  9 #endregion
 10 
 11 using System;
 12 using System.Data;
 13 using System.Reflection;
 14 
 15 namespace Stubman.DBHelper.Ado.Net
 16 {
 17     /// <summary>
 18     /// 数据库抽象工厂摘要说明。
 19     /// </summary>
 20     [Serializable]
 21     public abstract class DBFactory
 22     {
 23         #region 私有字段
 24 
 25         /// <summary>
 26         /// 连接数据库类型
 27         /// </summary>
 28         private DatabaseType _dbType;        //数据库系统类型
 29 
 30         #endregion
 31 
 32         #region 受保护的构造函数
 33         /// <summary>
 34         /// 初始化类型的新实例。
 35         /// </summary>
 36         /// <param name="dbType">数据库系统类型。</param>
 37         protected DBFactory(DatabaseType dbType)
 38         {
 39             this._dbType = dbType;
 40         }
 41         #endregion
 42 
 43         #region 创建具体DataSource的实例
 44 
 45         /// <summary>
 46         /// 创建指定的子类的实例。
 47         /// </summary>
 48         /// <param name="assemblyQualifiedTypeName">子类的完全限定名。</param>
 49         /// <remarks>
 50         /// 应用程序ナイキ ズーム KD 5不直接创建子类的实例,必须调用CreateInstance方法。
 51         /// </remarks>
 52         public static object CreateInstance(string assemblyQualifiedTypeName)
 53         {
 54             Type t = Type.GetType(assemblyQualifiedTypeName, true);
 55             return Activator.CreateInstance(t, true);
 56         }
 57 
 58         #endregion
 59 
 60         /// <summary>
 61         /// 获取此数据源的数据库系统类型。
 62         /// </summary>
 63         public virtual DatabaseType DatabaseType
 64         {
 65             get
 66             {
 67                 return this._dbType;
 68             }
 69         }
 70 
 71         /// <summary>
 72         /// 数据库连接
 73         /// </summary>
 74         /// <param name="connString">数据库连接字符串</param>
 75         /// <remarks>
 76         /// <para>
 77         /// 不同的数据库系统,连接串格式不同:
 78         /// </para>
 79         /// <para>SQL Server数据库:</para>
 80         /// <para>
 81         /// <c>"Data Source=serverName; Initial Catalog=databaseName; User ID=MyUserID; Password=MyPassword;"</c>
 82         /// </para>
 83         /// <para>Oracle数据库:</para>
 84         /// <para>
 85         /// <c>"Data Source=serverName; User ID=MyUserID; Password=MyPassword;"</c>
 86         /// </para>
 87         /// <para>Access数据库:</para>
 88         /// <para>
 89         /// <c>"Data Source=C:\DatabasePath\MmDatabase.mdb; User Id=admin; Password=;"</c>
 90         /// </para>
 91         /// <para>DB2数据库:</para>
 92         /// <para>
 93         /// <c>"Network Transport Library=TCPIP; Network Address=IPAddress; 
 94         ///             Initial Catalog=MyCatalog; Package Collection=MyPackageCollection; Default Schema=MySchema; 
 95         ///             User ID=MyUsername; Password=MyPassword;
 96         /// </c>
 97         /// </para>
 98         /// </remarks>
 99         /// <returns>指定数据源的连接对象</returns>
100         public abstract IDbConnection GetDBConnection(string connString);
101         /// <summary>
102         /// 创建与指定数据源连接相关联的SQL命令对象。
103         /// </summary>
104         /// <param name="dbConn"></param>
105         /// <returns></returns>
106         public abstract IDbCommand GetDBCommand(IDbConnection dbConn);
107         /// <summary>
108         /// 数据库适配器
109         /// </summary>
110         /// <param name="dbCommand"></param>
111         /// <returns></returns>
112         public abstract IDbDataAdapter GetDataAdapter(IDbCommand dbCommand);
113     }
114 }

 

 

 

#region Copyright
/*
 File Name(文件名):                      SqlServerDBFactory.cs
 Storage Path(存储路径):                 Core\Ado.Net\
 Author(作者):                           Stubman 
 File Action describe(文件功能描述):     SqlServer数据库工厂类, 用于定义SqlServer数据库的连接、操作和关闭等。
 File Memo(备注):
 
*/
#endregion

using System;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;

namespace Stubman.DBHelper.Ado.Net
{
    /// <summary>
    
/// SqlServerDBFactory产生SqlServer相关的数据库操作对象。
    
/// </summary>
    [Serializable]
    public sealed class SqlServerDBFactory : DBFactory
    {
        #region 受保护的构造函数
        /// <summary>
        
/// 私有的无参数构造函数。
        
/// </summary>
        private SqlServerDBFactory()
            : base(DatabaseType.DB_SqlServer)
        {
        }
        #endregion

        /// <summary>
        
/// 数据库连接
        
/// </summary>
        
/// <param name="connString">数据库连接字符串</param>
        
/// <returns>指定数据源的连接对象</returns>
        public override IDbConnection GetDBConnection(string connString)
        {
            SqlConnection dbConn = new SqlConnection(connString);
            return (IDbConnection)dbConn;
        }

        /// <summary>
        
/// 创建与指定数据源连接相关联的SQL命令对象。
        
/// </summary>
        
/// <param name="dbConn">数据库连接</param>
        
/// <returns></returns>
        public override IDbCommand GetDBCommand(IDbConnection dbConn)
        {
            try
            {
                if (dbConn.State == ConnectionState.Closed)
                {
                    dbConn.Open();
                }

                return dbConn.CreateCommand();
            }
            catch (SqlException ex)
            {
                throw new ApplicationException(ex.Message, ex);
            }
        }

        /// <summary>
        
/// 数据库适配器
        
/// </summary>
        
/// <param name="dbCommand"></param>
        
/// <returns></returns>
        public override IDbDataAdapter GetDataAdapter(IDbCommand dbCommand)
        {
            return new SqlDataAdapter((SqlCommand)dbCommand);
        }

    }

} 

 

 #region Copyright

/*
 File Name(文件名):                      OracleDBFactory.cs
 Storage Path(存储路径):                 Core\Ado.Net\
 Author(作者):                           Stubman 
 File Action describe(文件功能描述):     Oralce数据库工厂类, 用于定义Oracle数据库的连接、操作和关闭等。
 File Memo(备注):
 
*/
#endregion

using System;
using System.Data;
using System.Data.OracleClient;
using System.Reflection;

namespace Stubman.DBHelper.Ado.Net
{
    /// <summary>
    
/// OracleDBFactory产生Oracle相关的数据库操作对象。
    
/// </summary>
    [Serializable]
    public sealed class OracleDBFactory : DBFactory
    {
        #region 受保护的构造函数

        /// <summary>
        
/// 私有的无参数构造函数。
        
/// </summary>
        private OracleDBFactory()
            : base(DatabaseType.DB_Oracle)
        {
        }

        #endregion

        /// <summary>
        
/// 数据库连接
        
/// </summary>
        
/// <param name="connString">数据库连接字符串</param>
        
/// <returns>指定数据源的连接对象</returns>
        public override IDbConnection GetDBConnection(string connString)
        {
            OracleConnection dbConn = new OracleConnection(connString);
            return (IDbConnection)dbConn;
        }

        /// <summary>
        
/// 创建与指定数据源连接相关联的SQL命令对象。
        
/// </summary>
        
/// <param name="dbConn">数据库连接</param>
        
/// <returns></returns>
        public override IDbCommand GetDBCommand(IDbConnection dbConn)
        {
            try
            {
                if (dbConn.State == ConnectionState.Closed)
                {
                    dbConn.Open();
                }

                return dbConn.CreateCommand();
            }
            catch (OracleException ex)
            {
                throw new ApplicationException(ex.Message, ex);
            }
        }

        /// <summary>
        
/// 数据库适配器
        
/// </summary>
        
/// <param name="dbCommand"></param>
        
/// <returns></returns>
        public override IDbDataAdapter GetDataAdapter(IDbCommand dbCommand)
        {
            return new OracleDataAdapter((OracleCommand)dbCommand);
        }

    }
}

 

 

 /// <summary>

/// 运用template模式实现管理员用户与学生用户的不同登陆动作
/// </summary>
public abstract class Login : Page
{
    protected readonly M_userBo userBo = new M_userBo();
    protected string usrId = string.Empty;
    protected string usrPwd = string.Empty;
   
    /// <summary>
    
/// 绑定用户登陆信息
    
/// </summary>
    
/// <param name="usrid"></param>
    
/// <param name="usrpwd"></param>
    public void BindUserInfo(string usrid, string usrpwd)
    {
        this.usrId = usrid;
        this.usrPwd = usrpwd;
    }
    /// <summary>
    
/// 登陆系统
    
/// </summary>
    
/// <param name="usrid"></param>
    
/// <param name="usrpwd"></param>
    public void LoginIn(string usrid, string usrpwd)
    {
        BindUserInfo(usrid, usrpwd);
        bool loginAdmit = LoginAdmitAndBindSession(usrId, usrPwd);
        Redirect(loginAdmit);
    }
    /// <summary>
    
/// 获得登陆许可,以及绑定相应的Session
    
/// </summary>
    
/// <param name="usrID"></param>
    
/// <param name="usrpwd"></param>
    
/// <returns></returns>
    public abstract bool LoginAdmitAndBindSession(string usrID, string usrpwd);
    /// <summary>
    
/// 跳转到相应的URL
    
/// </summary>
    
/// <param name="loginAdmit"></param>
    public abstract void Redirect(bool loginAdmit);
}
public class AdminLogin : Login
{

    public override bool LoginAdmitAndBindSession(string usrID, string usrpwd)
    {
        PubFunc gongGong = new PubFunc();

        string usrAndPwd = usrId + usrPwd;

        #region 用户名、密码输入合法性判断
        if ((usrAndPwd.IndexOf("'") > 0) || (usrAndPwd.IndexOf("or") > 0))
        {
            gongGong.showMessage("用户名或密码输入不合法.");
            return false;
        }

        if (("" == usrId) || ("" == usrPwd))
        {
            gongGong.showMessage("用户名或密码不能为空.");
            return false;
        }
        #endregion
        
        #region 判断是否获得登陆许可
        ArrayList roleids = new ArrayList();
        M_user usr = new M_user();
        usr.Usr = usrId.ToLower();
        if ("0000" != usrPwd)
        {
            string msg1 = "";
            usr.Userpwd = JiaoYanMa.StrEncrypt(usrPwd, usr.Usr, ref msg1);
        }
        else
        {
            usr.Userpwd = usrPwd;// MD5.GetMd5Code(usrPwd);//加密要用哪个 DES、md5
        }
        Msg msg = new Msg();
        usr = this.userBo.LoadUsr(usr, ref msg);
        if (msg.showMsg != null && msg.showMsg != "")
        {
            gongGong.showMessage(msg.showMsg);
            return false;
        }
        if (usr != null)
        {

            #region 判断角色
            DataTable dtRole = this.userBo.GetUsrRole(usr.Usr, ref msg);

            if (dtRole.Rows.Count < 1)
            {
                gongGong.showMessage("您还未被授任何角色,请与管理员联系!");
                return false;
            }
            else
            {

                foreach (DataRow row in dtRole.Rows)
                {
                    roleids.Add(row["role"].ToString());
                }
               
            }
            #endregion
           
        }
        else
        {
            gongGong.showMessage("用户名或密码输入错误,请重新输入。");

            return false;
        }
        #endregion


        #region 绑定Session值
        Session["Login_Park"] = usr.Park;
        Session["Login_Roleids"] = roleids;
        Session["Login_User"] = usr.Usr;
        Session["Login_UserName"] = usr.Username;
        Session["Login_Skin"] = "blue";
        Session["LoginType"] = "Admin";//管理员身份登陆
        #endregion

        return true;
    }
    public override void Redirect(bool loginAdmit)
    {
        if (loginAdmit)
        {

            System.Web.HttpContext.Current.Response.Redirect("Main.aspx");
        }
    }
}
public class StudentLogin : Login
{
    public override bool LoginAdmitAndBindSession(string usrID, string usrpwd)
    {
        Session["LoginType"] = "Student";//学生身份登陆
        return true;
    }
    public override void Redirect(bool loginAdmit)
    {
        if (loginAdmit)
        {

            System.Web.HttpContext.Current.Response.Redirect("../Main/Letter/Default.aspx");
        }
    }
}

 

 

 

 

   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 Stubman.Common;
 11 namespace Stubman.Web
 12 {
 13     /// <summary>
 14     /// StaticOptions 的摘要说明
 15     /// </summary>
 16     public class StaticSelectOptions : System.Web.UI.Page
 17     {
 18      
 19         /// <summary>
 20         /// * ********************************************************
 21         /// * 绑定单选下拉框公共方法
 22         /// * 调用示例: 
 23         /// * StaticSelectOptions Option = new StaticSelectOptions();
 24         /// * Option.OptionsBind(selobj, "信件类型");
 25         /// **********************************************************
 26         /// </summary>
 27         public StaticSelectOptions()
 28         {
 29 
 30 
 31         }
 32 
 33 
 34         /// <summary>
 35         /// 根据表名,项名,是否有默认值,是否有空白项,绑定下拉框(HtmlSelect)
 36         /// </summary>
 37         /// <param name="SelObj"></param>
 38         /// <param name="ListName"></param>
 39         /// <param name="ItemName"></param>
 40         public void OptionsBind(HtmlSelect SelObj, string ParamName)
 41         {
 42             SelObj.Items.Clear();
 43             
 44             DataRow[] Rows = LoadParam.GetLoadParam().ParamDT.Select("paramname='" + ParamName + "'""id");
 45             string DEF = string.Empty;//默认值
 46             if (0 == Rows.Length)
 47             {
 48                 return;
 49             }
 50             foreach (DataRow dr in Rows)
 51             {
 52                 if (!dr["DEF"].ToString().Equals("2"))
 53                 {
 54                     SelObj.Items.Add(new ListItem(dr["paramvalue"].ToString(), dr["id"].ToString()));
 55                 }
 56                 if (dr["DEF"].ToString().Equals("1"))
 57                 {
 58                     DEF = dr["id"].ToString();
 59                 }
 60             }
 61             try
 62             {
 63                 if (!string.IsNullOrEmpty(DEF))
 64                 {
 65                     SelObj.Value = DEF;
 66                 }
 67 
 68                 if (Rows[0]["blank"].ToString().Equals("1"))//是否插入空白项
 69                 {
 70                     SelObj.Items.Insert(0new ListItem(""""));
 71                 }
 72             }
 73             catch (Exception)
 74             { }
 75 
 76 
 77         }
 78         /// <summary>
 79         /// 根据表名,项名,是否有默认值,是否有空白项,绑定下拉框(DropDownList)
 80         /// </summary>
 81         /// <param name="SelObj"></param>
 82         /// <param name="ListName"></param>
 83         /// <param name="ItemName"></param>
 84         public void OptionsBind(DropDownList SelObj, string ParamName)
 85         {
 86             SelObj.Items.Clear();
 87             DataRow[] Rows = LoadParam.GetLoadParam().ParamDT.Select("paramname='" + ParamName + "'""id");
 88             string DEF = string.Empty;//默认值
 89             if (0 == Rows.Length)
 90             {
 91                 return;
 92             }
 93             foreach (DataRow dr in Rows)
 94             {
 95                 if (!dr["DEF"].ToString().Equals("2"))
 96                 {
 97                     SelObj.Items.Add(new ListItem(dr["paramvalue"].ToString(), dr["id"].ToString()));
 98                 }
 99                 if (dr["DEF"].ToString().Equals("1"))
100                 {
101                     DEF = dr["id"].ToString();
102                 }
103             }
104             try
105             {
106                 if (!string.IsNullOrEmpty(DEF))
107                 {
108                     SelObj.SelectedValue = DEF;
109                 }
110 
111                 if (Rows[0]["blank"].ToString().Equals("1"))//是否插入空白项
112                 {
113                     SelObj.Items.Insert(0new ListItem(""""));
114                 }
115             }
116             catch (Exception)
117             { }
118         }
119         /// <summary>
120         /// 根据参数名获得该项目参数的数据集
121         /// </summary>
122         /// <param name="ParamName"></param>
123         /// <returns></returns>
124         public DataTable GetOptionsDataByParamName(string ParamName)
125         {
126             if (LoadParam.GetLoadParam().ParamDT != null)
127             {
128                 DataTable dt = LoadParam.GetLoadParam().ParamDT.Clone();
129                 DataRow[] Rows = LoadParam.GetLoadParam().ParamDT.Select("ParamName='" + ParamName + "'""id");
130                 foreach (DataRow dr in Rows)
131                 {
132                     dt.ImportRow(dr);
133                 }
134                 return dt;
135             }
136             else
137             {
138                 return null;
139             }
140         }
141         /// <summary>
142         /// 由id获取paramvalue
143         /// </summary>
144         /// <param name="id"></param>
145         /// <param name="ListName"></param>
146         /// <param name="ItemName"></param>
147         /// <returns></returns>
148         public string GetOptionNameByID(object id, string ParamName)
149         {
150             if (!CanShuPanDuan.ShiFouKong(id))
151             {
152                 DataTable dt = GetOptionsDataByParamName(ParamName);
153                 DataRow[] Rows = dt.Select("id='" + id.ToString() + "'");
154                 if (Rows.Length > 0)
155                 {
156                     return Rows[0]["paramvalue"].ToString();
157                 }
158                 else
159                 {
160                     return "";
161                 }
162             }
163             else
164             {
165                 return "";
166             }
167 
168         }
169         /// <summary>
170         /// 由id获取paramvalue
171         /// </summary>
172         /// <param name="id"></param>
173         /// <param name="ListName"></param>
174         /// <param name="ItemName"></param>
175         /// <returns></returns>
176         public string GetOptionNameByID(object id)
177         {
178             if (!CanShuPanDuan.ShiFouKong(id))
179             {
180 
181                 DataRow[] Rows = LoadParam.GetLoadParam().ParamDT.Select("id='" + id.ToString() + "'");
182                 if (Rows.Length > 0)
183                 {
184                     return Rows[0]["paramvalue"].ToString();
185                 }
186                 else
187                 {
188                     return "";
189                 }
190             }
191             else
192             {
193                 return "";
194             }
195 
196         }
197        
198     }
199 }

 

  1 using System;

 2 using System.Collections.Generic;
 3 using System.Web;
 4 using System.Data;
 5 using System.Threading;
 6 using MailManage.Business;
 7 namespace Stubman.Web
 8 {
 9     /// <summary>
10     ///LoadParam 的摘要说明
11     /// </summary>
12     public class LoadParam
13     {
14         private static LoadParam loadParam;
15         private DataTable _paramDT = new DataTable();
16         public LoadParam()
17         {
18             _paramDT = new M_paramBo().GetDataTable(); 
19         }
20         public static LoadParam GetLoadParam()
21         {
22             if (loadParam == null)
23             {
24                 //通过双检锁模式支持多线程应用
25                 Mutex mutex = new Mutex();
26                 mutex.WaitOne();
27                 if (loadParam == null)
28                 {
29                     loadParam = new LoadParam();
30                 }
31                 mutex.Close();
32             }
33             return loadParam;
34         }
35 
36         public DataTable ParamDT
37         {
38             get
39             { 
40                 return _paramDT;
41             }
42         }
43        
44     }
45 
46 }