網站首頁 編程語言 正文
一、建立一個空白的解決方案,名稱為“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
相關推薦
- 2023-01-01 淺析C++中static的一些用法_C 語言
- 2022-06-01 Python寫一個字符串數字后綴部分的遞增函數_python
- 2023-03-22 R語言基礎數據類型的具體使用_R語言
- 2023-11-26 在 el-table 中嵌入 el-checkbox el-input el-upload 多組件,
- 2022-07-06 C語言for循環嵌套for循環在實踐題目中應用詳解_C 語言
- 2022-08-27 DOS編寫腳本常用命令整理小結_DOS/BAT
- 2022-06-09 ASP.NET?Core使用EF創建模型(索引、備用鍵、繼承、支持字段)_實用技巧
- 2023-05-05 Golang實現簡易的命令行功能_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支