C#中通過使用Connection類來實(shí)現(xiàn)打開/關(guān)閉數(shù)據(jù)庫的代碼實(shí)例
為了訪問數(shù)據(jù)庫,就要提供數(shù)據(jù)庫連接類,在C#中,是通過Connection類來實(shí)現(xiàn)的
四種類型的連接方式
- SQLConnection
- ADOConnection
- OractleConnection
- ODBCConnection
以SQLConnection方式實(shí)現(xiàn)數(shù)據(jù)庫的連接:
- SQL Server數(shù)據(jù)庫
- windows 身份信息驗(yàn)證
步驟:
- 引用命名空間 using System.Data.SqlClient;
- 將連接方法聲明值字符串中
- 創(chuàng)建Connection對(duì)象
- 調(diào)用方法
為了節(jié)省系統(tǒng)資源提高系統(tǒng)性能,最好使用完數(shù)據(jù)庫就關(guān)閉連接,在C#語言中由于GC(垃圾回收機(jī)制)的存在,會(huì)在以后的某個(gè)時(shí)刻釋放資源,它是非決定性的,并不能確定這個(gè)過程在什么時(shí)候發(fā)生,當(dāng)忘記關(guān)閉數(shù)據(jù)庫是可以u(píng)sing語句確保對(duì)象退出時(shí)立即被釋放,從而達(dá)到關(guān)閉數(shù)據(jù)庫的作用,還有一種通過try..catch..final..
語句控制連接數(shù)據(jù)庫的關(guān)閉來提高性能
代碼實(shí)現(xiàn)如下:
using System; using System.Data.SqlClient; //引入命名空間 namespace Csharpone { class Program { static void Main(string[] args) { //windows 身份信息驗(yàn)證 下面的csharp01為新建的數(shù)據(jù)庫名稱 string constr = "Server.;integrated security=SSPI;Initial Catalog=csharp01"; SqlConnection mysqlCon = new SqlConnection(constr); //實(shí)例化 mysqlCon.Open(); //打開數(shù)據(jù)庫 Console.WriteLine("數(shù)據(jù)庫打開"); //正常打印說明沒問題,否則會(huì)拋出異常 //SQ驗(yàn)證方式 name是你設(shè)置的數(shù)據(jù)庫的用戶名,pwd是密碼 csharp02是數(shù)據(jù)庫名稱 string constr1 = "Server.;user=name; pwd=mima; database=csharp02"; SqlConnection mysqlCon1 = new SqlConnection(constr1); //實(shí)例化 mysqlCon1.Open(); //打開數(shù)據(jù)庫 Console.WriteLine("SQL方式 數(shù)據(jù)庫打開"); /*通過using語句實(shí)現(xiàn)數(shù)據(jù)庫的關(guān)閉 using (mysqlCon1) { mysqlCon1.Open(); Console.WriteLine("數(shù)據(jù)成功打開"); //執(zhí)行完立即關(guān)閉 } //通過try..catch..finally.. try { mysqlCon.Open(); Console.WriteLine("數(shù)據(jù)庫關(guān)閉"); } catch { } finally { mysqlCon.Close(); Console.WriteLine("關(guān)閉數(shù)據(jù)庫"); }*/ //以上兩種方式結(jié)合使用,確保數(shù)據(jù)庫占用資源得到釋放 try { using (mysqlCon) { mysqlCon.Open(); Console.WriteLine("打開數(shù)據(jù)庫"); } } catch { } finally { mysqlCon.Close(); Console.WriteLine("關(guān)閉數(shù)據(jù)庫"); } Console.Read(); } } }
MySQL數(shù)據(jù)庫代碼如下:
using System; using MySql.Data.MySqlClient; //導(dǎo)入引用,并且添加命名空間 namespace CSharp連接Mysql { class Program { static void Main(string[] args) { string connectStr = "server=localhost;port=3306;database=czhenya01;user=root;password=123456;"; //并沒有建立數(shù)據(jù)庫連接 MySqlConnection conn = new MySqlConnection(connectStr); try { conn.Open(); //建立連接,打開數(shù)據(jù)庫 Console.WriteLine("打開數(shù)據(jù)庫成功"); }catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { conn.Close(); //關(guān)閉連接 } Console.ReadKey(); } } }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- C# 數(shù)據(jù)庫鏈接字符串加密解密工具代碼詳解
- C# 操作 access 數(shù)據(jù)庫的實(shí)例代碼
- C#實(shí)現(xiàn)復(fù)制數(shù)據(jù)庫 C#將A數(shù)據(jù)庫數(shù)據(jù)轉(zhuǎn)到B數(shù)據(jù)庫
- C# 操作PostgreSQL 數(shù)據(jù)庫的示例代碼
- C#連接Oracle數(shù)據(jù)庫使用Oracle.ManagedDataAccess.dll
- C#實(shí)現(xiàn)連接SQL Server2012數(shù)據(jù)庫并執(zhí)行SQL語句的方法
- C#連接到sql server2008數(shù)據(jù)庫的實(shí)例代碼
- C#連接加密的Sqlite數(shù)據(jù)庫的方法
- C#實(shí)現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫中的方法
- C#使用ODBC與OLEDB連接數(shù)據(jù)庫的方法示例
- C#實(shí)現(xiàn)的ACCESS數(shù)據(jù)庫操作類完整實(shí)例
- c#實(shí)現(xiàn)幾種數(shù)據(jù)庫的大數(shù)據(jù)批量插入
- 詳解C#把DataTable中數(shù)據(jù)一次插入數(shù)據(jù)庫的方法
相關(guān)文章
PowerShell 定時(shí)執(zhí)行.Net(C#)程序的方法
利用PowerShell可以調(diào)用動(dòng)態(tài)頁面,然后再用 .bat 執(zhí)行 PowerShell 腳本,最后把 .bat 添加到服務(wù)器的任務(wù)計(jì)劃里面。OK,所有操作都做好了,.Net 定時(shí)執(zhí)行了,是不是呢,有木有呢。2013-04-04C# 中 Array和 ArrayList詳解及區(qū)別
這篇文章主要介紹了C# 中 Array和 ArrayList詳解及區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-01-01C# 繪制統(tǒng)計(jì)圖大全(柱狀圖, 折線圖, 扇形圖)
本篇文章介紹了C# 繪制統(tǒng)計(jì)圖大全,其中包括狀圖, 折線圖, 扇形圖,有需要的同學(xué)可以了解一下。2016-11-11C# 實(shí)現(xiàn)Table的Merge,Copy和Clone
這篇文章主要介紹了C# 實(shí)現(xiàn)Table的Merge,Copy和Clone,幫助大家更好的利用c#處理文件,感興趣的朋友可以了解下2020-12-12C#用websocket實(shí)現(xiàn)簡易聊天功能(客戶端)
這篇文章主要為大家詳細(xì)介紹了C#用websocket實(shí)現(xiàn)簡易聊天功能,客戶端方向,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02