日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

使用Spring.Net框架實現多數據庫_實用技巧

作者:.NET開發菜鳥 ? 更新時間: 2022-05-04 編程語言

一、建立一個空白的解決方案,名稱為“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數據庫以后的運行結果:

原文鏈接:https://www.cnblogs.com/dotnet261010/p/7374219.html