日期:2010-02-06  浏览次数:20466 次

复制代码 代码如下:

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
{
//数据库连接字符串(web.config来配置)
//<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.connectionString = ConfigurationSettings.AppSettings["ConnectionString"] ;
this.dbType = ConfigurationSettings.AppSettings["DataType"] ;
}

/**//// <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 GetSqlPara(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 );
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.Dat