C#實現(xiàn)IDbConnection/IDbCommand等相關(guān)通用數(shù)據(jù)接口
關(guān)于數(shù)據(jù)接口
在.net 應(yīng)用中,與數(shù)據(jù)庫進行連接、訪問和執(zhí)行經(jīng)常會用到數(shù)據(jù)接口的相關(guān)對象,如下:
1、 Connection
表示一個到數(shù)據(jù)庫的打開的連接,是連接數(shù)據(jù)必不可少的對象。
2、 Command
命令對象,表示要對數(shù)據(jù)源連接執(zhí)行的 SQL 語句或存儲過程,以獲取返回結(jié)果或執(zhí)行返回值。
3、 DataParameter
用于表示Command命令對象需要的參數(shù)設(shè)置,雖然這是一個可選項,但在實際的應(yīng)用中幾乎都會使用到
ADO.NET 中的數(shù)據(jù)提供者對象提供了IDbConnection、IDbCommand、IDbDataParameter等通用數(shù)據(jù)接口,本文將利用這些對象實現(xiàn)一個通用方法以訪問和操作數(shù)據(jù)庫內(nèi)容。
對象執(zhí)行流程
首先需要創(chuàng)建連接對象,成功后下達符合對應(yīng)數(shù)據(jù)庫規(guī)范的命令指令,該指令可能包括需要的參數(shù)對象(需要定義名稱和賦值等操作),大體流程如下圖:

范例運行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
.net版本: .netFramework4.7.1 或以上
開發(fā)工具:VS2019 C#
數(shù)據(jù)庫:在這里我們以支持 Oracle 9i、MS SQL Server 2016、國產(chǎn)達夢數(shù)據(jù) 8 為例
設(shè)計與實現(xiàn)
引用
在實現(xiàn)方法前請引用如下代碼:
using System.Data; using System.Data.SqlClient; using System.Data.OracleClient; using Dm; using System.Collections;
GetConnection方法
GetConnection 方法返回 System.Data.IDbConnection 對象,其參數(shù)說明見下表:
| 序號 | 參數(shù)名 | 類型 | 說明 |
|---|---|---|---|
| 1 | DbServerType | string | 目前支持 "oracle"、 "dm8",其它字符串均視為 MS SQL Server |
| 2 | ConnectionString | string | 對應(yīng)數(shù)據(jù)庫的連接字符串 |
實現(xiàn)代碼如下:
public System.Data.IDbConnection GetConnection(string DbServerType,string ConnectionString)
{
IDbConnection con = null;
switch (DbServerType.ToLower())
{
case "oracle":
con = new OracleConnection(ConnectionString);break;
case "dm8":
con = new DmConnection(ConnectionString); break;
default:
con = new SqlConnection(ConnectionString); break;
}
return con;
}GetCommand方法
GetCommand 方法返回 System.Data.IDbCommand 對象,其參數(shù)說明見下表:
| 序號 | 參數(shù)名 | 類型 | 說明 |
|---|---|---|---|
| 1 | dbServerType | string | 目前支持 "oracle"、 "dm8",其它字符串均視為 MS SQL Server |
| 2 | cmdText | string | 要執(zhí)行的SQL語句命令行 |
| 3 | paras | ArrayList | 要賦值的參數(shù)對象,逐個添加到ArrayList里,請注意參數(shù)為實體數(shù)據(jù)參數(shù)對象,如 MS SQL Server ,請傳遞如下代碼: ArrayList.Add(new SqlParameter("參數(shù)名",參數(shù)值)); |
| 4 | con | IDbConnection | 要傳遞的Connection對象,可能過前面所述的GetConnection方法獲取 |
實現(xiàn)代碼如下:
public IDbCommand GetCommand(string dbservertype,string cmdText,ArrayList paras,IDbConnection con)
{
IDbCommand cmd = null;
switch (dbservertype.ToLower())
{
case "oracle": cmd = new OracleCommand(cmdText,(OracleConnection)con);
break;
case "dm8":
cmd = new DmCommand(cmdText, (DmConnection)con);
break;
default: cmd = new SqlCommand(cmdText,(SqlConnection)con); break;
}
if(paras!=null)
{
for(int i=0;i<paras.Count;i++)
{
cmd.Parameters.Add(GetParameter(dbservertype,paras[i]));
}
}
return cmd;
}請注意,代碼中的 GetParameter 方法我們將后續(xù)做介紹。
GetParameter方法
GetParameter 方法返回 System.Data.IDbDataParameter 對象,其參數(shù)說明見下表:
| 序號 | 參數(shù)名 | 類型 | 說明 |
|---|---|---|---|
| 1 | dbServerType | string | 目前支持 "oracle"、 "dm8",其它字符串均視為 MS SQL Server |
| 2 | para | object | 傳遞的單一參數(shù)對象 |
實現(xiàn)代碼如下:
public System.Data.IDbDataParameter GetParameter(string dbservertype,object para)
{
IDbDataParameter pa = null;
switch (dbservertype.ToLower())
{
case "oracle": pa =(OracleParameter)para;
break;
case "dm8":
para = (DmParameter)pa;
break;
default: para =(SqlParameter)pa;
break;
}
return pa;
}小結(jié)
到此這篇關(guān)于C#實現(xiàn)IDbConnection/IDbCommand等相關(guān)通用數(shù)據(jù)接口的文章就介紹到這了,更多相關(guān)C#實現(xiàn)IDbConnection/IDbCommand接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# Newtonsoft.Json 解析多嵌套json 進行反序列化的實例
這篇文章主要介紹了C# Newtonsoft.Json 解析多嵌套json 進行反序列化的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

