怎么在C#中使用Connection類操作數(shù)據(jù)庫?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

10年積累的網(wǎng)站設(shè)計、成都網(wǎng)站制作經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有盧龍免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
四種類型的連接方式
SQLConnection
ADOConnection
OractleConnection
ODBCConnection
以SQLConnection方式實現(xiàn)數(shù)據(jù)庫的連接:
SQL Server數(shù)據(jù)庫
windows 身份信息驗證
步驟:
引用命名空間 using System.Data.SqlClient;
將連接方法聲明值字符串中
創(chuàng)建Connection對象
調(diào)用方法
為了節(jié)省系統(tǒng)資源提高系統(tǒng)性能,最好使用完數(shù)據(jù)庫就關(guān)閉連接,在C#語言中由于GC(垃圾回收機(jī)制)的存在,會在以后的某個時刻釋放資源,它是非決定性的,并不能確定這個過程在什么時候發(fā)生,當(dāng)忘記關(guān)閉數(shù)據(jù)庫是可以using語句確保對象退出時立即被釋放,從而達(dá)到關(guān)閉數(shù)據(jù)庫的作用,還有一種通過try..catch..final..語句控制連接數(shù)據(jù)庫的關(guān)閉來提高性能
代碼實現(xiàn)如下:
using System;
using System.Data.SqlClient; //引入命名空間
namespace Csharpone
{
class Program
{
static void Main(string[] args)
{
//windows 身份信息驗證 下面的csharp01為新建的數(shù)據(jù)庫名稱
string constr = "Server.;integrated security=SSPI;Initial Catalog=csharp01";
SqlConnection MySQLCon = new SqlConnection(constr); //實例化
mysqlCon.Open(); //打開數(shù)據(jù)庫
Console.WriteLine("數(shù)據(jù)庫打開"); //正常打印說明沒問題,否則會拋出異常
//SQ驗證方式 name是你設(shè)置的數(shù)據(jù)庫的用戶名,pwd是密碼 csharp02是數(shù)據(jù)庫名稱
string constr1 = "Server.;user=name; pwd=mima; database=csharp02";
SqlConnection mysqlCon1 = new SqlConnection(constr1); //實例化
mysqlCon1.Open(); //打開數(shù)據(jù)庫
Console.WriteLine("SQL方式 數(shù)據(jù)庫打開");
/*通過using語句實現(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();
}
}
}關(guān)于怎么在C#中使用Connection類操作數(shù)據(jù)庫問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
本文題目:怎么在C#中使用Connection類操作數(shù)據(jù)庫
當(dāng)前網(wǎng)址:http://www.chinadenli.net/article16/geisdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、響應(yīng)式網(wǎng)站、定制網(wǎng)站、做網(wǎng)站、云服務(wù)器、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)