使用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>
<!--注冊spring的切面-->
<sectionGroup name="spring">
<!--注冊spring的上下文切面-->
<section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"/>
<!--注冊spring的對象切面-->
<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ù)庫的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
運(yùn)行page頁面時(shí)的事件執(zhí)行順序及頁面的回發(fā)與否深度了解
page頁面時(shí)的事件執(zhí)行順序的了解對于一些.net開發(fā)者起到者尤關(guān)重要的作用;頁面的回發(fā)與否會(huì)涉及到某些事件執(zhí)行與不執(zhí)行,在本文中會(huì)詳細(xì)介紹,感興趣的朋友可以了解下2013-01-01
asp.net使用jquery實(shí)現(xiàn)搜索框默認(rèn)提示功能
這篇文章主要介紹了asp.net使用jquery實(shí)現(xiàn)搜索框默認(rèn)提示功能,大家參考使用吧2014-01-01
asp.net中的check與uncheck關(guān)鍵字用法解析
這篇文章主要介紹了asp.net中的check與uncheck關(guān)鍵字用法,以實(shí)例形式較為詳細(xì)的分析了check與uncheck關(guān)鍵字的各種常見用法與使用時(shí)的注意事項(xiàng),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
ASP.NET Core擴(kuò)展庫之日志功能的使用詳解
這篇文章主要介紹了ASP.NET Core擴(kuò)展庫之日志功能的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用.NET技術(shù),感興趣的朋友可以了解下2021-03-03
.net簡單使用Log4net的方法(多個(gè)日志配置文件)
log4net是.net中常用的一個(gè)日志記錄工具,下面這篇文章主要給大家介紹了關(guān)于.net簡單使用Log4net的方法(多個(gè)日志配置文件),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2018-11-11
asp.net中Timer無刷新定時(shí)器的實(shí)現(xiàn)方法
這篇文章主要介紹了asp.net中Timer無刷新定時(shí)器的實(shí)現(xiàn)方法,是一個(gè)非常具有實(shí)用價(jià)值的技巧,需要用到Ajax技術(shù),需要的朋友可以參考下2014-08-08
c# 讀取文件內(nèi)容存放到int數(shù)組 array.txt
c# 讀取文本的內(nèi)容,并且將內(nèi)容保存到int數(shù)組中,大家可以學(xué)習(xí)到c#一些數(shù)組跟讀取內(nèi)容的函數(shù)。2009-04-04

