欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#中通過使用Connection類來實(shí)現(xiàn)打開/關(guān)閉數(shù)據(jù)庫的代碼實(shí)例

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

為了訪問數(shù)據(jù)庫,就要提供數(shù)據(jù)庫連接類,在C#中,是通過Connection類來實(shí)現(xiàn)的

四種類型的連接方式

  1. SQLConnection
  2. ADOConnection
  3. OractleConnection
  4. ODBCConnection

以SQLConnection方式實(shí)現(xiàn)數(shù)據(jù)庫的連接:

  • SQL Server數(shù)據(jù)庫
  • windows 身份信息驗(yàn)證

步驟:

  1. 引用命名空間 using System.Data.SqlClient;
  2. 將連接方法聲明值字符串中
  3. 創(chuàng)建Connection對象
  4. 調(diào)用方法

為了節(jié)省系統(tǒng)資源提高系統(tǒng)性能,最好使用完數(shù)據(jù)庫就關(guān)閉連接,在C#語言中由于GC(垃圾回收機(jī)制)的存在,會在以后的某個(gè)時(shí)刻釋放資源,它是非決定性的,并不能確定這個(gè)過程在什么時(shí)候發(fā)生,當(dāng)忘記關(guān)閉數(shù)據(jù)庫是可以using語句確保對象退出時(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ù)庫打開");  //正常打印說明沒問題,否則會拋出異常
      //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)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對我們的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

上一篇:C#中重載重寫和覆蓋的定義與區(qū)別

欄    目:C#教程

下一篇:C#泛型概念的簡介與泛型的使用

本文標(biāo)題:C#中通過使用Connection類來實(shí)現(xiàn)打開/關(guān)閉數(shù)據(jù)庫的代碼實(shí)例

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5031.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有