使用Spring.Net框架實現(xiàn)多數(shù)據(jù)庫
一、建立一個空白的解決方案,名稱為“SpringDotNot”
二、新建一個類庫項目:IBLL
在IBLL類庫里面有一個名稱為IDatabaseService的接口,接口里面有兩個方法: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ù)庫服務接口
/// </summary>
public interface IDatabaseService
{
/// <summary>
/// 根據(jù)SQL語句查詢數(shù)據(jù)
/// </summary>
/// <returns></returns>
DataTable GetDataTableBySQL();
/// <summary>
/// 獲取數(shù)據(jù)庫類型
/// </summary>
/// <returns></returns>
string GetDbTyoe();
}
}三、新建一個類庫項目:BLLMsSql
BLLMsSql表示使用SqlServer數(shù)據(jù)庫實現(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服務類,實現(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ù)庫";
}
}
}四、新建一個類庫項目:BLLOracle
BLLOracle表示使用Oracle數(shù)據(jù)庫實現(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ù)服務類,實現(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)用
添加一個winform應用程序,界面上有一個DataGridView和一個Button按鈕,點擊Button按鈕的時候,從數(shù)據(jù)庫里面取數(shù)據(jù)并通過DataGridView展示查詢出的數(shù)據(jù),界面設計如下:

Spring.Net的配置信息都寫在項目的配置文件(即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é)點下面objects節(jié)點里面的資源-->
<resource uri="config://spring/objects"/>
</context>
<!--objects節(jié)點內(nèi)配置需要注入到spring容器內(nèi)的類-->
<objects xmlns="http://www.springframework.net">
<!--type組成: 逗號前面是命名空間.類名 逗號后面是程序集名稱-->
<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>后臺代碼如下:
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();
// 獲取具體的實現(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;
}
}
}配置文件中設置的是使用OracleService實現(xiàn)類,所以程序運行結(jié)果:

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

到此這篇關于Spring.Net框架實現(xiàn)多數(shù)據(jù)庫的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
ASP.NET使用ajax實現(xiàn)分頁局部刷新頁面功能
使用ajax方法實現(xiàn)分頁也很簡單,主要是兩個,ContentTemplate和Trigger。先把listView扔ContentTemplate里面。然后在Trigger里面加入asp:AsyncPostBackTrigger,將ID指向之前的分頁控件DataPager控件。具體實現(xiàn)代碼大家可以參考下本文2017-03-03
更方便快捷的外部操作數(shù)據(jù)庫的方法(另類玩法)
數(shù)據(jù)庫操作方法很多,各種各樣但是外部操作數(shù)據(jù)庫的方法就會顯得格外陌生了,感興趣的朋友可以詳細了解下本文,或許對你學習ado.net有所幫助2013-02-02
asp.net?Core中同名服務注冊的實現(xiàn)代碼
Asp.Net?Core中自帶了容器,同時也可以使用AutoFac替換掉默認的容器,以下為兩種方式同名服務的注冊實現(xiàn),對asp.net?Core服務注冊的實現(xiàn)代碼感興趣的朋友一起看看吧2022-03-03
asp.net 產(chǎn)生隨機顏色實現(xiàn)代碼
asp.net 隨機顏色產(chǎn)生實現(xiàn)代碼,需要的朋友拿過去測試一下。2009-11-11
創(chuàng)建一個ASP.NET MVC5項目的實現(xiàn)方法(圖文)
這篇文章主要介紹了創(chuàng)建一個ASP.NET MVC 5項目,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09

