亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#利用反射實(shí)現(xiàn)多數(shù)據(jù)庫訪問

 更新時(shí)間:2022年03月07日 11:12:34   作者:.NET開發(fā)菜鳥  
本文詳細(xì)講解了C#利用反射實(shí)現(xiàn)多數(shù)據(jù)庫訪問的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

上一篇文章中講解了什么是反射,以及利用反射可以獲取程序集里面的哪些內(nèi)容。在平時(shí)的項(xiàng)目中,可能會遇到項(xiàng)目需要使用多種數(shù)據(jù)庫,這篇文章中將會講解如何利用反射實(shí)現(xiàn)訪問多種數(shù)據(jù)庫。

項(xiàng)目整體結(jié)構(gòu)如下圖所示:

1、Database.Instance是一個(gè)類庫文件,IDBHelper是一個(gè)接口,封裝的訪問數(shù)據(jù)庫數(shù)據(jù)的CURD方法,OracleDBHelper和SQLServerDBHelper類實(shí)現(xiàn)IDBHelper接口,分別用來訪問Oracle數(shù)據(jù)庫和SQL Server數(shù)據(jù)庫,接口和類的定義如下:

IDBHelper接口定義

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Database.Instance.Interface
{
    public interface IDBHelper
    {
        /// <summary>
        /// 創(chuàng)建數(shù)據(jù)
        /// </summary>
        void Create();

        /// <summary>
        /// 更新數(shù)據(jù)
        /// </summary>
        void Update();

        /// <summary>
        /// 讀取數(shù)據(jù)
        /// </summary>
        void Retrieve();

        /// <summary>
        /// 刪除數(shù)據(jù)
        /// </summary>
        void Delete();
    }
}

OracleDBHelper類定義如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Database.Instance.Interface;

namespace Database.Instance.Oracle
{
   public class OracleDBHelper :IDBHelper
    {
        public void Create()
        {
            Console.WriteLine("這是Oracle數(shù)據(jù)庫執(zhí)行創(chuàng)建操作");
        }

        public void Update()
        {
            Console.WriteLine("這是Oracle數(shù)據(jù)庫執(zhí)行更新操作");
        }

        public void Retrieve()
        {
            Console.WriteLine("這是Oracle數(shù)據(jù)庫執(zhí)行讀取操作");
        }

        public void Delete()
        {
            Console.WriteLine("這是Oracle數(shù)據(jù)庫執(zhí)行刪除操作");
        }
    }
}

SQLServerDBHelper類定義如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Database.Instance.Interface;

namespace Database.Instance.SQL_Server
{
    public class SQLServerDBHelper:IDBHelper
    {
        public void Create()
        {
            Console.WriteLine("這是SQL Server數(shù)據(jù)庫執(zhí)行創(chuàng)建操作");
        }

        public void Update()
        {
            Console.WriteLine("這是SQL Server數(shù)據(jù)庫執(zhí)行更新操作");
        }

        public void Retrieve()
        {
            Console.WriteLine("這是SQL Server數(shù)據(jù)庫執(zhí)行讀取操作");
        }

        public void Delete()
        {
            Console.WriteLine("這是SQL Server數(shù)據(jù)庫執(zhí)行刪除操作");
        }
    }
}

2、MyReflection是一個(gè)控制臺程序,用來測試

一、使用原始方法實(shí)現(xiàn)

使用原始的方法實(shí)現(xiàn)代碼如下:

using Database.Instance.Interface;
using Database.Instance.Oracle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Configuration;

namespace MyReflection
{
    class Program
    {
        static void Main(string[] args)
        {
            // 實(shí)例化(調(diào)用Oracle數(shù)據(jù)庫)
            IDBHelper dbHelper = new OracleDBHelper();
            // 調(diào)用方法
            dbHelper.Create();
            dbHelper.Update();
            dbHelper.Retrieve();
            dbHelper.Delete();

            Console.ReadKey();
        }
    }
}

程序運(yùn)行結(jié)果:

存在的問題:如果換一種數(shù)據(jù)庫,那么就需要修改實(shí)例化的代碼,例如更換SQL Server數(shù)據(jù)庫,那么代碼修改如下:

IDBHelper dbHelper = new SQLServerDBHelper();

這樣很不方便,每次更換數(shù)據(jù)庫的時(shí)候,都需要修改實(shí)例化的代碼,有沒有什么方便的方法可以做到不需要修改代碼就可以實(shí)現(xiàn)更換數(shù)據(jù)庫呢?辦法就是使用反射加配置文件實(shí)現(xiàn)。

二、使用反射加配置文件實(shí)現(xiàn)

配置文件結(jié)構(gòu)如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!--key表示定義的接口 value格式 要加載的程序集名稱,要實(shí)例化的類 value值中間以','分割-->
    <add key="Database.Instance.Interface.IDBHelper" value="Database.Instance,Database.Instance.Oracle.OracleDBHelper"/>
  </appSettings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
    </startup>
</configuration>

Program類定義如下:

using Database.Instance.Interface;
using Database.Instance.Oracle;
using Database.Instance.SQL_Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Configuration;

namespace MyReflection
{
    class Program
    {
        static void Main(string[] args)
        {
            // 根據(jù)key值讀取對應(yīng)的value值
            string[] config = ConfigurationManager.AppSettings["Database.Instance.Interface.IDBHelper"].Split(',');
            // 加載程序集 config[0]=Database.Instance
            Assembly assembly = Assembly.Load(config[0]);

            // 根據(jù)類的完全限定名找出類型 config[1]= Database.Instance.Oracle.OracleDBHelper
            Type type = assembly.GetType(config[1]);
            // 根據(jù)類型創(chuàng)建對象
            object obj = Activator.CreateInstance(type);
            //實(shí)例化
            IDBHelper dbHelper = obj as IDBHelper;
            dbHelper.Create();
            dbHelper.Update();
            dbHelper.Retrieve();
            dbHelper.Delete();
            Console.ReadKey();
        }
    }
}

 運(yùn)行結(jié)果如下:

如果更新數(shù)據(jù)庫,只需要更新配置文件中value的值即可,例如要更換SQL Server數(shù)據(jù)庫,配置文件修改如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!--key表示定義的接口 value格式 要加載的程序集名稱,要實(shí)例化的類 value值中間以','分割-->
    <add key="Database.Instance.Interface.IDBHelper" value="Database.Instance,Database.Instance.SQL_Server.SQLServerDBHelper"/>
  </appSettings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
    </startup>
</configuration>

 Program類不需要修改,運(yùn)行結(jié)果如下:

到此這篇關(guān)于C#利用反射實(shí)現(xiàn)多數(shù)據(jù)庫訪問的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論