本文转自:
http://blog.csdn.net/mydriverc/archive/2007/06/14/1652128.aspx
本项目除用到"实时数据库"外, 还需要用Oracle数据库存储大量的配置信息和生成的数据,而且对 Oracle的读取相当的频繁,在项目开始之处,数据访问就是一个很令人烦恼的问题,仅仅数据访问类就修改了好多版本,直到目前正在使用的这个版本.同时为了应付开发过程中不时需要读取SqlServer和Access数据库,所以就写成三种数据源的通用访问类,虽然有点四不象,不过挺省事的,嘻嘻!
此模块分为两个CS文件:
DataFactory.cs
using
System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.OracleClient;
using System.Collections;
namespace REAP.Utility
{
public enum DataBaseType
{
Access,
SQLServer,
Oracle
}
/// <summary>
/// DataFactory 的摘要说明。
/// </summary>
class DataFactory
{
public DataFactory()
{ }
public static IDbConnection CreateConnection( string ConnectionString, DataBaseType dbtype)
{
IDbConnection cnn;
switch (dbtype)
{
case DataBaseType.Access:
cnn = new OleDbConnection(ConnectionString);
break ;
case DataBaseType.SQLServer:
cnn = new SqlConnection(ConnectionString);
break ;
case DataBaseType.Oracle:
cnn = new OracleConnection(ConnectionString);
break ;
default :
cnn = new SqlConnection(ConnectionString);
break ;
}
return cnn;
}
public static IDbCommand CreateCommand(DataBaseType dbtype, IDbConnection cnn)
{
IDbCommand cmd;
switch (dbtype)
{
case DataBaseType.Access:
cmd = new OleDbCommand( "" , (OleDbConnection)cnn);
break ;
case DataBaseType.SQLServer:
cmd = new SqlCommand( "" , (SqlConnection)cnn);
break ;
case DataBaseType.Oracle:
cmd = new OracleCommand( "" , (OracleConnection)cnn);
break ;
default :
cmd = new SqlCommand( "" , (SqlConnection)cnn);
break ;
}
return cmd;
}
public static IDbCommand CreateCommand( string CommandText, DataBaseType dbtype, IDbConnection cnn)
{
IDbCommand cmd;
switch (dbtype)
{
case DataBaseType.Access:
cmd = new OleDbCommand(CommandText, (OleDbConnection)cnn);
break ;
case DataBaseType.SQLServer:
cmd = new SqlCommand(CommandText, (SqlConnection)cnn);
break ;
case DataBaseType.Oracle:
cmd = new OracleCommand(CommandText, (OracleConnection)cnn);
break ;
default :
cmd = new SqlCommand(CommandText, (SqlConnection)cnn);
break ;
}
return cmd;
}
public static DbDataAdapter CreateAdapter(IDbCommand cmd, DataBaseType dbtype)
{
DbDataAdapter da;
switch (dbtype)
{
case DataBaseType.Access:
da = new OleDbDataAdapter((OleDbCommand)cmd);
break ;
case DataBaseType.SQLServer:
da = new SqlDataAdapter((SqlCommand)cmd);
break ;
case DataBaseType.Oracle:
da = new OracleDataAdapter((OracleCommand)cmd);
break ;
default :
da = new SqlDataAdapter((SqlCommand)cmd);
break ;
}
return da;
}
public static IDataParameter CreateParameter(DataBaseType dbtype)
{
IDataParameter param = null ;
switch (dbtype)
{
case DataBaseType.Access:
param = new OleDbParameter();
break ;
case DataBaseType.SQLServer:
param = new SqlParameter();
break ;
case DataBaseType.Oracle:
param = new OracleParameter();
break ;
default :
param = new SqlParameter();
break ;
}
return param;
}
}
}
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.OracleClient;
using System.Collections;
namespace REAP.Utility
{
public enum DataBaseType
{
Access,
SQLServer,
Oracle
}
/// <summary>
/// DataFactory 的摘要说明。
/// </summary>
class DataFactory
{
public DataFactory()
{ }
public static IDbConnection CreateConnection( string ConnectionString, DataBaseType dbtype)
{
IDbConnection cnn;
switch (dbtype)
{
case DataBaseType.Access:
cnn = new OleDbConnection(ConnectionString);
break ;
case DataBaseType.SQLServer:
cnn = new SqlConnection(ConnectionString);
break ;
case DataBaseType.Oracle:
cnn = new OracleConnection(ConnectionString);
break ;
default :
cnn = new SqlConnection(ConnectionString);
break ;
}
return cnn;
}
public static IDbCommand CreateCommand(DataBaseType dbtype, IDbConnection cnn)
{
IDbCommand cmd;
switch (dbtype)
{
case DataBaseType.Access:
cmd = new OleDbCommand( "" , (OleDbConnection)cnn);
break ;
case DataBaseType.SQLServer:
cmd = new SqlCommand( "" , (SqlConnection)cnn);
break ;
case DataBaseType.Oracle:
cmd = new OracleCommand( "" , (OracleConnection)cnn);
break ;
default :
cmd = new SqlCommand( "" , (SqlConnection)cnn);
break ;
}
return cmd;
}
public static IDbCommand CreateCommand( string CommandText, DataBaseType dbtype, IDbConnection cnn)
{
IDbCommand cmd;
switch (dbtype)
{
case DataBaseType.Access:
cmd = new OleDbCommand(CommandText, (OleDbConnection)cnn);
break ;
case DataBaseType.SQLServer:
cmd = new SqlCommand(CommandText, (SqlConnection)cnn);
break ;
case DataBaseType.Oracle:
cmd = new OracleCommand(CommandText, (OracleConnection)cnn);
break ;
default :
cmd = new SqlCommand(CommandText, (SqlConnection)cnn);
break ;
}
return cmd;
}
public static DbDataAdapter CreateAdapter(IDbCommand cmd, DataBaseType dbtype)
{
DbDataAdapter da;
switch (dbtype)
{
case DataBaseType.Access:
da = new OleDbDataAdapter((OleDbCommand)cmd);
break ;
case DataBaseType.SQLServer:
da = new SqlDataAdapter((SqlCommand)cmd);
break ;
case DataBaseType.Oracle:
da = new OracleDataAdapter((OracleCommand)cmd);
break ;
default :
da = new SqlDataAdapter((SqlCommand)cmd);
break ;
}
return da;
}
public static IDataParameter CreateParameter(DataBaseType dbtype)
{
IDataParameter param = null ;
switch (dbtype)
{
case DataBaseType.Access:
param = new OleDbParameter();
break ;
case DataBaseType.SQLServer:
param = new SqlParameter();
break ;
case DataBaseType.Oracle:
param = new OracleParameter();
break ;
default :
param = new SqlParameter();
break ;
}
return param;
}
}
}
DBAccess.cs
using
System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.OracleClient;
using System.Configuration;
namespace REAP.Utility
{
/// <summary>
/// 由于可能会在多种数据源,如ORACLE,SQLSERVER,ACCESS等之间进行切换,
/// 所以将数据源连接字符串和数据源类型定义为类属性,在默认情况下有配置文件定义;
/// 当需要在两种不同的数据源之间进行切换时,可以重新为属性赋值。
/// </summary>
public class DBAccess
{
属性设置
DataSet生成操作
SQL执行操作
DataReader操作
// 其他功能,故意省略
}
}
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.OracleClient;
using System.Configuration;
namespace REAP.Utility
{
/// <summary>
/// 由于可能会在多种数据源,如ORACLE,SQLSERVER,ACCESS等之间进行切换,
/// 所以将数据源连接字符串和数据源类型定义为类属性,在默认情况下有配置文件定义;
/// 当需要在两种不同的数据源之间进行切换时,可以重新为属性赋值。
/// </summary>
public class DBAccess
{
属性设置
DataSet生成操作
SQL执行操作
DataReader操作
// 其他功能,故意省略
}
}
举例如下:
默认情况下是访问Oracle数据库,数据库连接字符串已经在Config文件中定义,所以不需要再设置其ConnectionString和DataSourceType属性,此时返回一个DataSet的代码如下:
DBAccess db
=
new
DBAccess();
// 同时执行两条查询语句
string strSql = " SELECT * FROM TABLE1;SELECT * FROM TABLE2 " ;
DataSet ds = db.GetDataSet(strSql);
// 同时执行两条查询语句
string strSql = " SELECT * FROM TABLE1;SELECT * FROM TABLE2 " ;
DataSet ds = db.GetDataSet(strSql);
但是如果在程序中需要临时访问SqlServer数据库,则需要设置属性,此时代码如下:
DBAccess db
=
new
DBAccess();
db.ConnectionString = " server=localhost;UID=sa;PWD=123456;DATABASE=Money;connect timeout=120 " ;
db.DataSourceType = DataBaseType.SQLServer;
db.ConnectionString = " server=localhost;UID=sa;PWD=123456;DATABASE=Money;connect timeout=120 " ;
db.DataSourceType = DataBaseType.SQLServer;
(完)