本項目除用到"實時數據庫"外,?還需要用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;
????????}
????}
}
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操作
????????
//
其他功能,故意省略
????}
}
舉例如下:
默認情況下是訪問Oracle數據庫,數據庫連接字符串已經在Config文件中定義,所以不需要再設置其ConnectionString和DataSourceType屬性,此時返回一個DataSet的代碼如下:
DBAccess?db?
=
?
new
?DBAccess();
//
同時執行兩條查詢語句
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;
(完)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

