使用Spring.Net框架實(shí)現(xiàn)多數(shù)據(jù)庫
一、建立一個(gè)空白的解決方案,名稱為“SpringDotNot”
二、新建一個(gè)類庫項(xiàng)目:IBLL
在IBLL類庫里面有一個(gè)名稱為IDatabaseService的接口,接口里面有兩個(gè)方法:GetDataTableBySQL()和GetDbTyoe()。
代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace IBLL
{
/// <summary>
/// 數(shù)據(jù)庫服務(wù)接口
/// </summary>
public interface IDatabaseService
{
/// <summary>
/// 根據(jù)SQL語句查詢數(shù)據(jù)
/// </summary>
/// <returns></returns>
DataTable GetDataTableBySQL();
/// <summary>
/// 獲取數(shù)據(jù)庫類型
/// </summary>
/// <returns></returns>
string GetDbTyoe();
}
}三、新建一個(gè)類庫項(xiàng)目:BLLMsSql
BLLMsSql表示使用SqlServer數(shù)據(jù)庫實(shí)現(xiàn)IBLL里面的接口,BLLMsSql要添加IBLL.dll的引用,代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBLL;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace BLLMsSql
{
/// <summary>
/// SqlServer服務(wù)類,實(shí)現(xiàn)IDatabaseService接口
/// </summary>
public class SqlServerService :IDatabaseService
{
public DataTable GetDataTableBySQL()
{
string strConn = ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString;
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(strConn))
{
try
{
string str = "select * from PtInfectionCard";
SqlCommand cmd = new SqlCommand(str, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
}
return dt;
}
/// <summary>
/// 返回SqlServer數(shù)據(jù)庫
/// </summary>
/// <returns></returns>
public string GetDbTyoe()
{
return "我是SQLServer數(shù)據(jù)庫";
}
}
}四、新建一個(gè)類庫項(xiàng)目:BLLOracle
BLLOracle表示使用Oracle數(shù)據(jù)庫實(shí)現(xiàn)IBLL里面的接口,BLLOracle要添加IBLL.dll的引用,代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBLL;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
namespace BLLOracle
{
/// <summary>
/// Oracle數(shù)據(jù)服務(wù)類,實(shí)現(xiàn)IDatabaseService接口
/// </summary>
public class OracleService :IDatabaseService
{
public DataTable GetDataTableBySQL()
{
string strConn = ConfigurationManager.ConnectionStrings["ORACLE"].ConnectionString;
DataTable dt = new DataTable();
using (OracleConnection conn = new OracleConnection(strConn))
{
try
{
string str = "select * from emp";
OracleCommand cmd = new OracleCommand(str, conn);
OracleDataAdapter adapter = new OracleDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
}
return dt;
}
/// <summary>
/// 返回Oracle數(shù)據(jù)庫
/// </summary>
/// <returns></returns>
public string GetDbTyoe()
{
return "我是Oracle數(shù)據(jù)庫";
}
}
}五、客戶端調(diào)用
添加一個(gè)winform應(yīng)用程序,界面上有一個(gè)DataGridView和一個(gè)Button按鈕,點(diǎn)擊Button按鈕的時(shí)候,從數(shù)據(jù)庫里面取數(shù)據(jù)并通過DataGridView展示查詢出的數(shù)據(jù),界面設(shè)計(jì)如下:

Spring.Net的配置信息都寫在項(xiàng)目的配置文件(即App.config)中,配置文件如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!--注冊(cè)spring的切面-->
<sectionGroup name="spring">
<!--注冊(cè)spring的上下文切面-->
<section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"/>
<!--注冊(cè)spring的對(duì)象切面-->
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core"/>
</sectionGroup>
</configSections>
<!--Spring的依賴注入配置-->
<spring>
<context>
<!--使用配置文件里面spring節(jié)點(diǎn)下面objects節(jié)點(diǎn)里面的資源-->
<resource uri="config://spring/objects"/>
</context>
<!--objects節(jié)點(diǎn)內(nèi)配置需要注入到spring容器內(nèi)的類-->
<objects xmlns="http://www.springframework.net">
<!--type組成: 逗號(hào)前面是命名空間.類名 逗號(hào)后面是程序集名稱-->
<object id="bll" type="BLLOracle.OracleService,BLLOracle"/>
</objects>
</spring>
<connectionStrings>
<!--Oracle數(shù)據(jù)庫連接字符串-->
<add name="ORACLE" connectionString="Data Source=127.0.0.1/orcl;Persist Security Info=True;User ID=scott;Password=tiger;Unicode=True;"/>
<!--SqlServer數(shù)據(jù)庫連接字符串-->
<add name="SqlServer" connectionString="Data Source=.;Initial Catalog=******;Persist Security Info=True;User ID=******;Password=*********"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>后臺(tái)代碼如下:
using Spring.Context;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IBLL;
namespace WinClient
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
/// <summary>
/// 加載數(shù)據(jù)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_LoadData_Click(object sender, EventArgs e)
{
// 從配置文件讀取配置
IApplicationContext ctx = Spring.Context.Support.ContextRegistry.GetContext();
// 獲取具體的實(shí)現(xiàn)類
IDatabaseService dbService = ctx.GetObject("bll") as IDatabaseService;
// 從數(shù)據(jù)庫查詢數(shù)據(jù)
DataTable dt = dbService.GetDataTableBySQL();
// 將查詢出的數(shù)據(jù)綁定到DataGridView中
this.dgv_Demo.DataSource = dt;
}
}
}配置文件中設(shè)置的是使用OracleService實(shí)現(xiàn)類,所以程序運(yùn)行結(jié)果:

如果要使用SqlServer數(shù)據(jù)庫,只需要修改配置文件中object節(jié)點(diǎn)中type的屬性值即可:
<object id="bll" type="BLLMsSql.SqlServerService,BLLMsSql"/>
改成使用SqlServer數(shù)據(jù)庫以后的運(yùn)行結(jié)果:

到此這篇關(guān)于Spring.Net框架實(shí)現(xiàn)多數(shù)據(jù)庫的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET使用ajax實(shí)現(xiàn)分頁局部刷新頁面功能
使用ajax方法實(shí)現(xiàn)分頁也很簡單,主要是兩個(gè),ContentTemplate和Trigger。先把listView扔ContentTemplate里面。然后在Trigger里面加入asp:AsyncPostBackTrigger,將ID指向之前的分頁控件DataPager控件。具體實(shí)現(xiàn)代碼大家可以參考下本文2017-03-03
更方便快捷的外部操作數(shù)據(jù)庫的方法(另類玩法)
數(shù)據(jù)庫操作方法很多,各種各樣但是外部操作數(shù)據(jù)庫的方法就會(huì)顯得格外陌生了,感興趣的朋友可以詳細(xì)了解下本文,或許對(duì)你學(xué)習(xí)ado.net有所幫助2013-02-02
asp.net?Core中同名服務(wù)注冊(cè)的實(shí)現(xiàn)代碼
Asp.Net?Core中自帶了容器,同時(shí)也可以使用AutoFac替換掉默認(rèn)的容器,以下為兩種方式同名服務(wù)的注冊(cè)實(shí)現(xiàn),對(duì)asp.net?Core服務(wù)注冊(cè)的實(shí)現(xiàn)代碼感興趣的朋友一起看看吧2022-03-03
asp.net 產(chǎn)生隨機(jī)顏色實(shí)現(xiàn)代碼
asp.net 隨機(jī)顏色產(chǎn)生實(shí)現(xiàn)代碼,需要的朋友拿過去測(cè)試一下。2009-11-11
創(chuàng)建一個(gè)ASP.NET MVC5項(xiàng)目的實(shí)現(xiàn)方法(圖文)
這篇文章主要介紹了創(chuàng)建一個(gè)ASP.NET MVC 5項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
ASP.NET中實(shí)現(xiàn)獲取調(diào)用方法名
這篇文章主要介紹了ASP.NET中實(shí)現(xiàn)獲取調(diào)用方法名的技巧,較為詳細(xì)的講述了相關(guān)的命名空間的調(diào)用及語句執(zhí)行順序,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
在運(yùn)行時(shí)編輯代碼的 .NET 熱重載的操作方法
今天通過本文給大家分享 Visual Studio 2019 中 16.11(預(yù)覽版1)中的 .NET 熱重載(通過 .NET 6(預(yù)覽版4)中的 dotnet watch 命令行工具),介紹什么是 .NET 熱重載,如何使用這個(gè)特征,感興趣的朋友跟隨小編一起學(xué)習(xí)下吧2021-07-07

