使用Spring.Net框架实现多数据库

这篇文章介绍了Spring.Net框架实现多数据库的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、建立一个空白的解决方案,名称为“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 { ///  /// 数据库服务接口 ///  public interface IDatabaseService { ///  /// 根据SQL语句查询数据 ///  ///  DataTable GetDataTableBySQL(); ///  /// 获取数据库类型 ///  ///  string GetDbTyoe(); } }

三、新建一个类库项目:BLLMsSql

BLLMsSql表示使用SqlServer数据库实现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 { ///  /// SqlServer服务类,实现IDatabaseService接口 ///  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; } ///  /// 返回SqlServer数据库 ///  ///  public string GetDbTyoe() { return "我是SQLServer数据库"; } } }

四、新建一个类库项目:BLLOracle

BLLOracle表示使用Oracle数据库实现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 { ///  /// Oracle数据服务类,实现IDatabaseService接口 ///  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; } ///  /// 返回Oracle数据库 ///  ///  public string GetDbTyoe() { return "我是Oracle数据库"; } } }

五、客户端调用

添加一个winform应用程序,界面上有一个DataGridView和一个Button按钮,点击Button按钮的时候,从数据库里面取数据并通过DataGridView展示查询出的数据,界面设计如下:

Spring.Net的配置信息都写在项目的配置文件(即App.config)中,配置文件如下:

    

后台代码如下:

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(); } ///  /// 加载数据 ///  ///  ///  private void btn_LoadData_Click(object sender, EventArgs e) { // 从配置文件读取配置 IApplicationContext ctx = Spring.Context.Support.ContextRegistry.GetContext(); // 获取具体的实现类 IDatabaseService dbService = ctx.GetObject("bll") as IDatabaseService; // 从数据库查询数据 DataTable dt = dbService.GetDataTableBySQL(); // 将查询出的数据绑定到DataGridView中 this.dgv_Demo.DataSource = dt; } } }

配置文件中设置的是使用OracleService实现类,所以程序运行结果:

如果要使用SqlServer数据库,只需要修改配置文件中object节点中type的属性值即可:

改成使用SqlServer数据库以后的运行结果:

到此这篇关于Spring.Net框架实现多数据库的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持0133技术站。

以上就是使用Spring.Net框架实现多数据库的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » ASP编程