• 设计模式(抽象工厂模式)


    开发中为了解决连接不同数据源使用了抽象工厂生成器来构造数据访问层,
    namespace Ehl.Atms.UI.IID
    {
        
    /// <summary>
        
    /// 数据库工厂生成器
        
    /// </summary>

        internal class DataFactory
        
    {
            
    public DataFactory()
            
    {
                
    //
                
    // TODO: 在此处添加构造函数逻辑
                
    //
            }

            
    /// <summary>
            
    /// 生成器
            
    /// </summary>
            
    /// <param name="connString">连接字符串</param>
            
    /// <param name="dbType">数据库类型</param>
            
    /// <returns></returns>

            public DataAbstract GetInstance(string connString,string dbType)
            
    {
                
    if( dbType.ToLower() == "oracle" )
                
    {
                    
    return new DataOracle( connString );
                }

                
    else
                
    {
                    
    return null;
                }

            }

        }

    }
    namespace Ehl.Atms.UI.IID
    {
        
    /// <summary>
        
    /// 数据库连接工厂抽象类
        
    /// </summary>
        internal abstract class DataAbstract
        {
            
    /// <summary>
            
    /// 查询数据
            
    /// </summary>
            
    /// <param name="strSql"></param>
            
    /// <returns></returns>
            public abstract DataView Sql_Select( string strSql );
            
    /// <summary>
            
    /// 执行Sql语句
            
    /// </summary>
            
    /// <param name="strSql"></param>
            public abstract void Sql_Execute( string strSql );
            
    /// <summary>
            
    /// 判断是否重复
            
    /// </summary>
            
    /// <param name="strSql"></param>
            
    /// <returns></returns>
            public abstract bool Sql_isExist( string strSql );
            
    /// <summary>
            
    /// 关闭连接
            
    /// </summary>
            public abstract void Close();
        }
    }
    namespace Ehl.Atms.UI.IID
    {
        
    /// <summary>
        
    /// 数据库工厂模式OracleO连接类
        
    /// </summary>
        internal class DataOracle:DataAbstract
        {
            
    private OracleConnection oracleConn ;
            
    private OracleCommand oracleComm ;
            
    private OracleDataAdapter oracleDa;
            
    public DataOracle( string ConnectString )
            {
                oracleConn 
    = new OracleConnection( ConnectString );
                oracleConn.Open();
            }
            
    /// <summary>
            
    /// 实现查询
            
    /// </summary>
            
    /// <param name="strSql"></param>
            
    /// <returns></returns>
            public override DataView Sql_Select( string strSql )
            {
                
    try
                {
                    DataSet ds 
    = new DataSet() ;
                    
    if( strSql != "" ) 
                    {
                        
    if( oracleConn.State == ConnectionState.Broken ) 
                        {
                            oracleConn.Close() ;
                            oracleConn.Open() ;
                        }
                        oracleComm 
    = oracleConn.CreateCommand();
                        oracleComm.CommandType 
    = CommandType.Text;
                        oracleComm.CommandText 
    = strSql;
                        oracleDa 
    = new OracleDataAdapter( oracleComm );
                        oracleDa.Fill(ds);
                        oracleDa.Dispose();
                        oracleComm.Dispose();
                    }
                    
    return ds.Tables[0].DefaultView;
                }
                
    catch( Exception ex ) 
                {
                    Log.WriteLog( 
    string.Format( "类名称:{0} 方法名称:{1} 消息: {2}" , "DataOracle" , "Sql_Select" , ex.ToString() ) , true ) ;
                    
    return  new DataView() ;
                }

            }
            
    /// <summary>
            
    /// 执行Sql
            
    /// </summary>
            
    /// <param name="strSql"></param>
            public override void Sql_Execute(string strSql)
            {
                
    try
                {
                    ExecuteNonQuery( strSql );
                }
                
    catch( Exception ex )
                {
                    Log.WriteLog( 
    string.Format( "类名称:{0} 方法名称:{1} 消息: {2}" , "DataOracle" , "Sql_Execute" , ex.ToString() ) , true ) ;
                }
            }

            
    public override bool Sql_isExist(string strSql)
            {
                
    try
                {    
                    
    bool flag = false;
                    
    if( strSql.Trim() != "")
                    {
                        
    if( oracleConn.State == ConnectionState.Broken ) 
                        {
                            oracleConn.Close() ;
                            oracleConn.Open() ;
                        }
                        oracleComm 
    = oracleConn.CreateCommand( ) ;
                        oracleComm.CommandType 
    = CommandType.Text ;
                        oracleComm.CommandText 
    = strSql ;
                        
    if(oracleComm.ExecuteScalar() != null )
                        {
                            flag 
    = true;
                        }
                        oracleComm.Dispose();
                    }
                    
    return flag;
                }
                
    catch( Exception ex )
                {
                    Log.WriteLog( 
    string.Format( "类名称:{0} 方法名称:{1} 消息: {2}" , "DataOracle" , "Sql_isExist" , ex.ToString() ) , true ) ;
                    
    return true;
                }
            }



            
    /// <summary>
            
    /// 执行批处理的sql语句
            
    /// </summary>
            private int ExecuteNonQuery( string strSql ) 
            {
                
    try
                {
                    
    int iRecordCount = 0 ;
                    strSql 
    = strSql.Trim() ;
                    
    if( strSql != "" ) 
                    {
                        strSql 
    = ( strSql.Substring( strSql.Length - 1 , 1 ) == ";" ) ?  strSql : strSql + ";" ;
                        
    if( oracleConn.State == ConnectionState.Broken ) 
                        {
                            oracleConn.Close() ;
                            oracleConn.Open() ;
                        }
                        oracleComm 
    = oracleConn.CreateCommand( ) ;
                        oracleComm.CommandType 
    = CommandType.Text ;
                        oracleComm.CommandText 
    = string.Format( "begin {0} end ;" , strSql ) ;
                        iRecordCount 
    = oracleComm.ExecuteNonQuery() ;
                        oracleComm.Dispose();
                    }
                    
    return iRecordCount ;
                }
                
    catch( Exception ex ) 
                {
                    Log.WriteLog( 
    string.Format( "类名称:{0} 方法名称:{1} 消息: {2}" , "DataOracle" , "ExecuteNonQuery" , ex.ToString() ) , true ) ;
                    
    return  0 ;
                }
            }
            
    /// <summary>
            
    /// 关闭连接
            
    /// </summary>
            public override void Close()
            {
                oracleConn.Dispose();
                oracleConn.Close();
            }

        }
    }
  • 相关阅读:
    PyQt(Python+Qt)学习随笔:QTreeWidgetItem项子项展开相关方法
    PyQt(Python+Qt)学习随笔:QTreeWidgetItem项下子项的指示符展示原则childIndicatorPolicy
    PyQt(Python+Qt)学习随笔:QTreeWidgetItem项是否禁用disable、隐藏isHidden和允许选中isSelected
    查看 Apache并发请求数及其TCP连接状态
    Web服务器性能/压力测试工具http_load、webbench、ab、Siege使用教程
    Apache下禁止使用IP直接访问本站的配置方法
    apache两种工作模式详解
    合理设置apache的连接数及进程工作方式
    apache 工作模式
    Windows下Apache的优化
  • 原文地址:https://www.cnblogs.com/jinweida/p/1212278.html
Copyright © 2020-2023  润新知