using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.OracleClient;
using System.Configuration;
using System.Reflection;
namespace SystemFramework.DAL
{
/
**//// <summary>
/// All rights reserved
/// 数据访问基础类
/// 用户可以修改满足自己项目的需要。
/// </summary>
public class DataBaseLayer
{
//数据库连接字符串(fig来配置)
//<add key="ConnectionString" value="server=127.0.0.1;database=DATABASE;uid=sa;pwd=" />       
private string connectionString;
public string ConntionString
{
get
{
return connectionString ;
}
set
{
connectionString = value;
}
}
public DataBaseLayer(string strConnect,string dataType)
{           
this.ConntionString = strConnect;
this.DbType = dataType;
}
public DataBaseLayer()
{
this.dbType = ConfigurationSettings.AppSettings["DataType"] ;
//tionString = "data source=192.168.1.43;user id=sa;pwd=sa;database=temphrdb";
//this.dbType = "SqlServer";
}
/
**//// <summary>
/// 数据库类型
/// </summary>
private string dbType;
public string DbType
{
get
{
if ( dbType == string.Empty || dbType == null )
{
return "Access";
}
else
{
return dbType;
}
}
set 
{
if ( value != string.Empty  &&  value != null )
{
dbType = value;
}
if (dbType ==string.Empty || dbType == null)
{
dbType = ConfigurationSettings.AppSettings["DataType"];
}
if ( dbType == string.Empty || dbType == null )
{
dbType = "Access";
}
}     
}
转换参数#region 转换参数
private System.Data.IDbDataParameter iDbPara(string ParaName,string DataType)
{
switch(this.DbType)
{
case "SqlServer":
return GetSqlPara(ParaName,DataType);
case "Oracle":
return GetOleDbPara(ParaName,DataType);
case "Access":
return GetOleDbPara(ParaName,DataType);
default :
return G
etSqlPara(ParaName,DataType);
}
}
private System.Data.SqlClient.SqlParameter  GetSqlPara( string ParaName , string DataType)
{
switch(DataType)
{
case "Decimal":
return  new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.Decimal );
case "Varchar":
return new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.VarChar );
case "DateTime":
return new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.DateTime );
case "Iamge":
return new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.Image );
case "Int":
return new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.Int );
oracle decimal类型case "Text":
return new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.NText );
default :
return new System.Data.SqlClient.SqlParameter ( ParaName, System.Data.SqlDbType.VarChar );
}
}
private System.Data.OracleClient.OracleParameter  GetOraclePara( string ParaName , string DataType)
{
switch(DataType)
{
case "Decimal":
return  new System.Data.OracleClient.OracleParameter( ParaName, System.Data.OracleClient.OracleType.Double);
case "Varchar":
return  new System.Data.OracleClient.OracleParameter ( ParaName, System.Data.OracleClient.OracleType.VarChar );
case "DateTime":
return  new System.Data.OracleClient.OracleParameter ( ParaName, System.Data.OracleClient.OracleType.DateTime );
case "Iamge":
return  new System.Data.OracleClient.OracleParameter ( ParaName, System.Data.OracleClient.OracleType.BFile );
case "Int":
return  new System.Data.OracleClient.OracleParameter ( ParaName, System.Data.OracleClient.OracleType.Int32 );
case "Text":
return  new System.Data.OracleClient.OracleParameter ( ParaName, System.Data.OracleClient.OracleType.LongVarChar );
default:
return  new System.Data.OracleClient.OracleParameter ( ParaName, System.Data.OracleClient.OracleType.VarChar );
}
}
private System.Data.OleDb.OleDbParameter  GetOleDbPara( string ParaName , string DataType)
{
switch(DataType)
{
case "Decimal":
return  new System.Data.OleDb.OleDbParameter( ParaName, System.Data.DbType.Decimal);
case "Varchar":
return  new System.Data.OleDb.OleDbParameter ( ParaNam
e, System.Data.DbType.String );
case "DateTime":
return  new System.Data.OleDb.OleDbParameter ( ParaName, System.Data.DbType.DateTime );
case "Iamge":
return  new System.Data.OleDb.OleDbParameter( ParaName, System.Data.DbType.Binary );
case "Int":
return  new System.Data.OleDb.OleDbParameter ( ParaName, System.Data.DbType.Int32 );
case "Text":
return  new System.Data.OleDb.OleDbParameter ( ParaName, System.Data.DbType.String );                   
default:
return  new System.Data.OleDb.OleDbParameter ( ParaName, System.Data.DbType.String );
}
}
#endregion
创建 Connection 和 Command#region 创建 Connection 和 Command
private IDbConnection GetConnection()
{
switch(this.DbType)
{
case "SqlServer":
return new System.Data.SqlClient.SqlConnection(this.ConntionString);
case "Oracle":
return new System.Data.OracleClient.OracleConnection(this.ConntionString);
case "Access":
return new System.Data.OleDb.OleDbConnection(this.ConntionString);
default:
return new System.Data.SqlClient.SqlConnection(this.ConntionString);
}
}
private IDbCommand GetCommand(string Sql,IDbConnection iConn)
{
switch(this.DbType)
{
case "SqlServer":
return new System.Data.SqlClient.SqlCommand(Sql,(SqlConnection)iConn);
case "Oracle":
return new System.Data.OracleClient.OracleCommand(Sql,(OracleConnection)iConn);
case "Access":
return new System.Data.OleDb.OleDbCommand(Sql,(OleDbConnection)iConn);
default:
return new System.Data.SqlClient.SqlCommand(Sql,(SqlConnection)iConn);
}   
}
private IDbCommand GetCommand()
{
switch(this.DbType)
{
case "SqlServer":
return new System.Data.SqlClient.SqlCommand();
case "Oracle":
return new System.Data.OracleClient.OracleCommand();
case "Access":
return new System.Data.OleDb.OleDbCommand();
default:
return new System.Data.SqlClient.SqlCommand();
}   
}
private IDataAdapter GetAdapater(string Sql,IDbConnection iConn)
{
switch(this.DbType)
{
case "SqlServer":
return new System.Data.SqlClient.SqlDataAdapter(Sql,(SqlConnection)iConn);
case "Oracle":
return new System.Data.OracleClient.OracleDataAdapter(Sql,(OracleConnection)iConn);
case "Access":
return new System.Data.OleDb.OleDbDataAdapter(Sql,(OleDbConnection)iConn);
default:
return new System.Data.SqlClient.SqlDataAdapter(Sql,(SqlConnection)iConn);;
}
}
private IDataAdapter GetAdapater()
{
switch(this.DbType)
{
case "SqlServer":
return new System.Data.SqlClient.SqlDataAdapter();
case "Oracle":
return new System.Data.OracleClient.OracleDataAdapter();
case "Access":
return new System.Data.OleDb.OleDbDataAdapter();
default:
return new System.Data.SqlClient.SqlDataAdapter();
}
}
private IDataAdapter GetAdapater(IDbCommand iCmd)
{
switch(this.DbType)
{
case "SqlServer":
return new System.Data.SqlClient.SqlDataAdapter((SqlCommand)iCmd);
case "Oracle":
return new System.Data.OracleClient.OracleDataAdapter((OracleCommand)iCmd);
case "Access":
return new System.Data.OleDb.OleDbDataAdapter((OleDbCommand)iCmd);
default:
return new System.Data.SqlClient.SqlDataAdapter((SqlCommand)iCmd);
}
}
#endregion
执行简单SQL语句#region  执行简单SQL语句
/**//// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SqlString)
{
using (System.Data.IDbConnection  iConn = this.GetConnection())
{               
using (System.Data.IDbCommand iCmd  = GetCommand(SqlString,iConn))
{
iConn.Open();
try
{       
int rows=iCmd.ExecuteNonQuery();
return rows;
}
catch(System.Exception E)
{                   
throw new Exception(E.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}               
}
}
/**//// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>       
public  void ExecuteSqlTran(ArrayList SQLStringList)
{
using (System.Data.IDbConnection  iConn = this.GetConnection())
{
iConn.Open();
using(System.Data.IDbCommand iCmd  =  GetCommand())
{
iCmd.Connection=iConn;               
using(System.Data.IDbTransaction iDbTran = iConn.BeginTransaction())
{
iCmd.Transaction=iDbTran;   
try
{         
for(int n=0;n<SQLStringList.Count;n++)
{
string strsql = SQLStringList[n].ToString();
if ( strsql.Trim().Length>1)
{
iCmd.CommandText = strsql;
iCmd.ExecuteNonQuery();
}
}                                       
iDbTran.Commit();                   
}
catch(System.Exception E)
{       
iDbTran.Rollback();
throw new Exception(E.Message);
}
finally
{
if(iConn.State != ConnectionState.Closed)
{
iConn.Close();
}
}
}
}
}
}
/**//// <summary>
/// 执行带一个存储过程参数的的SQL语句。
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SqlString,string content)
{               
using (System.Data.IDbConnection  iConn = this.GetConnection())
{
using(System.Data.IDbCommand iCmd  = GetCommand(SqlString,iConn))
{
System.Data.IDataParameter  myParameter = this.iDbPara( "@content", "Text");
myParameter.Value = content ;
iCmd.Parameters.Add(myParameter);
iConn.Open();
try
{
int rows = iCmd.ExecuteNonQuery();
return rows;
}
catch( System.Exception e )
{   

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。