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;
}
}
}
}
{
/// <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();
}
}
{
/// <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();
}
}
}
{
/// <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();
}
}
}