欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Spring.Net框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)

 更新時(shí)間:2022年03月03日 10:27:26   作者:.NET開(kāi)發(fā)菜鳥(niǎo)  
這篇文章介紹了Spring.Net框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、建立一個(gè)空白的解決方案,名稱(chēng)為“SpringDotNot”

二、新建一個(gè)類(lèi)庫(kù)項(xiàng)目:IBLL

在IBLL類(lèi)庫(kù)里面有一個(gè)名稱(chēng)為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ù)庫(kù)服務(wù)接口
    /// </summary>
    public interface IDatabaseService
    {
        /// <summary>
        /// 根據(jù)SQL語(yǔ)句查詢(xún)數(shù)據(jù)
        /// </summary>
        /// <returns></returns>
        DataTable GetDataTableBySQL();

        /// <summary>
        /// 獲取數(shù)據(jù)庫(kù)類(lèi)型
        /// </summary>
        /// <returns></returns>
        string GetDbTyoe();
    }
}

三、新建一個(gè)類(lèi)庫(kù)項(xiàng)目:BLLMsSql

BLLMsSql表示使用SqlServer數(shù)據(jù)庫(kù)實(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ù)類(lèi),實(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ù)庫(kù)
        /// </summary>
        /// <returns></returns>
        public string GetDbTyoe()
        {
            return "我是SQLServer數(shù)據(jù)庫(kù)";
        }
    }
}

四、新建一個(gè)類(lèi)庫(kù)項(xiàng)目:BLLOracle

BLLOracle表示使用Oracle數(shù)據(jù)庫(kù)實(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ù)類(lèi),實(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ù)庫(kù)
        /// </summary>
        /// <returns></returns>
        public string GetDbTyoe()
        {
            return "我是Oracle數(shù)據(jù)庫(kù)";
        }
    }
}

五、客戶(hù)端調(diào)用

添加一個(gè)winform應(yīng)用程序,界面上有一個(gè)DataGridView和一個(gè)Button按鈕,點(diǎn)擊Button按鈕的時(shí)候,從數(shù)據(jù)庫(kù)里面取數(shù)據(jù)并通過(guò)DataGridView展示查詢(xún)出的數(shù)據(jù),界面設(shè)計(jì)如下:

Spring.Net的配置信息都寫(xiě)在項(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的依賴(lài)注入配置-->
  <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)的類(lèi)-->
    <objects xmlns="http://www.springframework.net">
        <!--type組成: 逗號(hào)前面是命名空間.類(lèi)名 逗號(hào)后面是程序集名稱(chēng)-->
       <object id="bll" type="BLLOracle.OracleService,BLLOracle"/>
    </objects>
  </spring>
  <connectionStrings>
    <!--Oracle數(shù)據(jù)庫(kù)連接字符串-->
    <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ù)庫(kù)連接字符串-->
    <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)類(lèi)
            IDatabaseService dbService = ctx.GetObject("bll") as IDatabaseService;
            // 從數(shù)據(jù)庫(kù)查詢(xún)數(shù)據(jù)
            DataTable dt = dbService.GetDataTableBySQL();
            // 將查詢(xún)出的數(shù)據(jù)綁定到DataGridView中
            this.dgv_Demo.DataSource = dt;
        }
    }
}

配置文件中設(shè)置的是使用OracleService實(shí)現(xiàn)類(lèi),所以程序運(yùn)行結(jié)果:

如果要使用SqlServer數(shù)據(jù)庫(kù),只需要修改配置文件中object節(jié)點(diǎn)中type的屬性值即可:

<object id="bll" type="BLLMsSql.SqlServerService,BLLMsSql"/>

改成使用SqlServer數(shù)據(jù)庫(kù)以后的運(yùn)行結(jié)果:

到此這篇關(guān)于Spring.Net框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論