数据访问层设计(2)

    技术2022-05-11  89

    2类型设计 

    1)ConnectionType 数据库连接

    using System;

    namespace HKH.DataBase.Type{ /// <summary> /// 数据库连接字符串类 /// </summary> /// <remarks> /// Create By Liwt on 2006 - 06 - 23 /// 保存数据库连接的各种参数 /// </remarks> public class ConnectionType {  #region 私有变量

      private DataBaseType m_DBType;

      private string m_Provider;

      private string m_ServerName;

      private string m_DataBaseName;

      private string m_UserID;

      private string m_Password;

      private bool m_IntegratedSecurity;

      private int m_ConnectTimeout;

      private string m_ApplicationName;    private string m_AttachDBFileName;    private string m_strConnectionString;

      private string m_ExtendedProperties;

      private string m_Mode;

      #endregion

      #region 属性

      /// <summary>  /// 获取或设置数据库类型  /// </summary>  public DataBaseType DBType  {   get   {    return m_DBType;   }   set   {    m_DBType = value;   }  }

      /// <summary>  /// 获取或设置驱动提供者(OleDb)  /// </summary>  public string Provider  {   get   {    return m_Provider;   }   set   {    m_Provider = value;   }  }

      /// <summary>  /// 获取或设置扩展属性(OleDb)  /// </summary>  public string ExtendedProperties  {   get   {    return m_ExtendedProperties;   }   set   {    m_ExtendedProperties = value;   }  }

      /// <summary>  /// 获取或设置访问模式(OleDb)  /// </summary>  public string Mode  {   get   {    return m_Mode;   }   set   {    m_Mode = value;   }  }

      /// <summary>  /// 获取或设置服务器名称  /// </summary>  public string ServerName  {   get   {    return m_ServerName;   }   set   {    m_ServerName = value;   }  }

      /// <summary>  /// 获取或设置连接字符串  /// </summary>  public string ConnectionString  {   get   {    if ( null != m_strConnectionString && "" != m_strConnectionString )     return m_strConnectionString;    else     return this.toConnectionString();   }   set   {    this.m_strConnectionString = value;   }  }

      /// <summary>  /// 获取或设置数据库名称  /// </summary>  public string DataBaseName  {   get   {    return m_DataBaseName;   }   set   {    m_DataBaseName = value;   }  }

      /// <summary>  /// 获取或设置数据库用户ID  /// </summary>  public string UserID  {   get   {    return m_UserID;   }   set   {    m_UserID = value;   }  }

      /// <summary>  /// 获取或设置数据库用户密码  /// </summary>  public string Password  {   get   {    return m_Password;   }   set   {    m_Password = value;   }  }

      /// <summary>  /// 获取或设置安全验证模式  /// </summary>  public bool IntegratedSecurity  {   get   {    return m_IntegratedSecurity;   }   set   {    m_IntegratedSecurity = value;   }  }

      /// <summary>  /// 获取或设置连接超时  /// </summary>  public int ConnectTimeout  {   get   {    return m_ConnectTimeout;   }   set   {    m_ConnectTimeout = value;   }  }

      /// <summary>  /// 获取或设置应用程序名称  /// </summary>  public string ApplicationName  {   get   {    return m_ApplicationName;   }   set   {    m_ApplicationName = value;   }  }

      /// <summary>  /// 获取或设置附加数据文件名  /// </summary>  public string AttachDBFileName  {   get   {    return m_AttachDBFileName;   }   set   {    m_AttachDBFileName = value;   }  }

      #endregion

      #region 私有方法  /// <summary>  /// 生成连接字符串  /// </summary>  /// <returns>连接字符串</returns>  private string toConnectionString()  {   string strConnString;   if (!( 0 == m_strConnectionString.Length ))   {    return m_strConnectionString;   }   strConnString = "";

       //SqlServer   if ( this.m_DBType == DataBaseType.SqlServer )   {    if( true != m_IntegratedSecurity )    {     strConnString += "Integrated Security= false";    }    else    {     strConnString += "Integrated Security= true";    }    if( m_ConnectTimeout < 0 )    {     strConnString += ";Connect Timeout=" + "15";    }    else    {     strConnString += ";Connect Timeout=" + m_ConnectTimeout.ToString();    }    if( 0 != m_ServerName.Length )    {     strConnString += ";data source=" + m_ServerName;    }    if( 0 != m_DataBaseName.Length )    {     strConnString += ";initial catalog=" + m_DataBaseName;    }    if( 0 != m_UserID.Length )    {     strConnString += ";user id=" + m_UserID;    }    if( 0 != m_Password.Length )    {     strConnString += ";password=" + m_Password;    }    if( 0 != m_ApplicationName.Length )    {     strConnString += "" + m_ApplicationName;    }    if( 0 != m_AttachDBFileName.Length )    {     strConnString += "" + m_AttachDBFileName;    }   }

       //OleDb   if (this.m_DBType == DataBaseType.OleDb)   {    if( 0 != m_Provider.Length )    {     strConnString += "Provider=" + m_Provider;    }    if( 0 != m_DataBaseName.Length )    {     strConnString += ";Data Source=" + m_DataBaseName;    }    if( 0 != m_UserID.Length )    {     strConnString += ";User ID=" + m_UserID;    }    if( 0 != m_Password.Length )    {     strConnString += ";Password=" + m_Password;    }    if( 0 != m_Mode.Length )    {     strConnString += ";Mode=" + m_Mode;    }    if( 0 != m_ExtendedProperties.Length )    {     strConnString += ";Extended Properties=" + m_ExtendedProperties;    }   }

       //Odbc   if ( DataBaseType.Odbc == m_DBType)   {   }

       return strConnString;  }

      #endregion

      #region 构造函数

      /// <summary>  /// 构造函数  /// </summary>  public ConnectionType()  {   m_DBType = DataBaseType.SqlServer;   m_Provider = "";   m_ServerName = "LocalHost";   m_DataBaseName = "Master";   m_UserID = "sa";   m_Password = "";   m_IntegratedSecurity = true;   m_ConnectTimeout = 15;   m_ApplicationName = "";   m_AttachDBFileName = "";   m_strConnectionString = "";   m_ExtendedProperties = "";   m_Mode = "";  }

      #endregion }}

    2)DataBaseType 数据库类型

    using System;

    namespace HKH.DataBase.Type{ /// <summary> /// 数据库类型 /// </summary> public enum DataBaseType {  SqlServer,   //MS SQL-SERVER  Odbc,    //Oracle  OleDb    //ACCESS }}

    3)ProcParameterType 存储过程参数类型

    using System;using System.Data;

    namespace HKH.DataBase.Type{ /// <summary> /// 存储过程参数类型 /// </summary> /// <remarks> /// Create By Liwt on 2006 - 06 - 23 /// 因目前使用的主流数据库为SQL-SERVER,以下类型原型均为SqlDbType /// 以后使用其它数据库时,可结合做微调 /// </remarks> public enum ProcParameterType {  BigInt ,            //int64   8字节  Binary ,   //Byte[]  1 到 8,000  Bit ,    //Boolean  1字节  Char ,    //String  1 到 8,000  DateTime ,   //DateTime  1753 年 1 月 1 日到 9999 年 12 月 31 日  Decimal ,   //Decimal  -10 (38) -1 和 10 (38) -1  Float ,    //Double  -1.79E +308 到 1.79E +308   Image ,    //Byte[]  0 到 231 -1  Int ,    //int32   4字节  Money ,    //Decimal  -2 (63)(即 -922,337,203,685,477.5808)到 2 (63) -1  NChar ,    //String  1 到 4,000 (Unicode)  NText ,    //String  1 到 2 (30) - 1 (Unicode)  NVarChar ,   //String  1 到 4,000 (Unicode)  Real ,    //Single  -3.40E +38 到 3.40E +38   SmallDateTime ,  //DateTime  1900 年 1 月 1 日到 2079 年 6 月 6 日  SmallInt ,   //int16   2字节  SmallMoney ,  //Decimal  -214,748.3648 到 +214,748.3647   Text ,    //String  1 到 2 (31) -1  Timestamp ,   //Byte[]  8字节  TinyInt ,   //Byte   1字节  UniqueIdentifier , //GUID  VarBinary ,   //Byte[]  1 到 8,000  VarChar ,   //String  1 到 8,000  Variant    //Object }

     /// <summary> /// 构造存储过程参数的自定义结构 /// </summary> /// <remarks> /// Create by Liwt on 2006- 06 - 23 /// </remarks> public struct ProcParameter {  #region 变量

      private string parameterName ;  private object parameterValue ;  private ProcParameterType parameterType ;  private int parameterSize ;  private ParameterDirection parameterDirec ;

      #endregion

      #region 属性

      /// <summary>  /// 参数名称  /// </summary>  public string ParameterName  {   get   {    return this.parameterName;   }   set   {    this.parameterName = value;   }  }

      /// <summary>  /// 参数值  /// </summary>  public object ParameterValue  {   get   {    return this.parameterValue;   }   set   {    this.parameterValue = value;   }  }

      /// <summary>  /// 参数类型  /// </summary>  public ProcParameterType ParameterType  {   get   {    return this.parameterType;   }   set   {    this.parameterType = value;   }  }

      /// <summary>  /// 参数长度  /// </summary>  public int ParameterSize  {   get   {    return this.parameterSize;   }   set   {    this.parameterSize = value;   }  }

      /// <summary>  /// 参数传输方向  /// </summary>  public ParameterDirection ParameterDirec  {   get   {    return this.parameterDirec;   }   set   {    this.parameterDirec = value;   }  }

      #endregion

      #region 构造

      public ProcParameter(string PName , object PValue , ProcParameterType PType , int PSize , ParameterDirection PDirec)  {   this.parameterName = PName;   this.parameterValue = PValue;   this.parameterType = PType;   this.parameterSize = PSize;   this.parameterDirec = PDirec;  }

      public ProcParameter(string PName , object PValue , ProcParameterType PType , int PSize )  {   this.parameterName = PName;   this.parameterValue = PValue;   this.parameterType = PType;   this.parameterSize = PSize;   this.parameterDirec = ParameterDirection.Input;  }

      public ProcParameter(string PName , object PValue )  {   this.parameterName = PName;   this.parameterValue = PValue;   this.parameterType = ProcParameterType.Variant;   this.parameterSize = 0;   this.parameterDirec = ParameterDirection.Input;  }

      #endregion }}


    最新回复(0)