1. 程式人生 > 程式設計 >C#連線SQL Server資料庫的例項講解

C#連線SQL Server資料庫的例項講解

C#連線資料庫:Connection物件

1.Connection物件概述

Connection物件是一個連線物件,主要功能是建立與物理資料庫的連線。其主要包括4種訪問資料庫的物件類,也可稱為資料提供程式,分別介紹如下。

  • SQL Server資料提供程式,位於System.Data.SqlClient名稱空間。
  • ODBC資料提供程式,位於System.Data.Odbc名稱空間。
  • OLEDB資料提供程式,位於System.Data.OleDb名稱空間。
  • Oracle資料提供程式,位於System.Data.OracleClient名稱空間。

說明:根據使用資料庫的不同,引入不同的名稱空間,然後通過名稱空間中的Connection物件連線類連線資料庫。例如,連線SQL Server資料庫,首先要通過using System.Data.SqlClient命令引用SQL Server資料提供程式,然後才能呼叫空間下的SqlConnection類連線資料庫。

2.連線資料庫

以SQL Server資料庫為例,如果要連線SQL Server資料庫,必須使用System.Data.SqlClient名稱空間下的SqlConnection類。所以首先要通過using System.Data.SqlClient命令引用名稱空間,連線資料庫之後,通過呼叫SqlConnection物件的Open方法開啟資料庫。通過SqlConnection物件的State屬性判斷資料庫的連線狀態。

介面:

程式碼:

private void btn1_Click(object sender,EventArgs e)
    {
      if (txt1.Text == "")
      {
        MessageBox.Show("請輸入要連線的資料庫名稱!");
      }
      else
      {
        try
        {
          string connString = "server=.;database=" + txt1.Text.Trim() + ";uid=test;pwd=test;connect timeout=5"; //**
          SqlConnection sqlConnection = new SqlConnection(connString); //**
          sqlConnection.Open(); //**

          if (sqlConnection.State == ConnectionState.Open)
          {
            lab2.Text = "資料庫【" + txt1.Text.Trim() + "】已經連線並開啟!";
          }
        }
        catch
        {
          MessageBox.Show("資料庫連線失敗!");
        }
      }
    }

3.關閉連線

當對資料庫操作完畢後,要關閉與資料庫的連線,釋放佔用的資源。可以通過呼叫SqlConnection物件的Close方法或Dispose方法關閉與資料庫的連線。這兩種方法的主要區別是:Close方法用於關閉一個連線,而Dispose方法不僅關閉一個連線,而且還清理連線所佔用的資源。當使用Close方法關閉連線後,可以再呼叫Open方法開啟連線,不會產生任何錯誤。而如果使用Dispose方法關閉連線,就不可以直接用Open方法開啟連線,必須再次重新初始化連線再開啟。

介面:

程式碼:

SqlConnection sqlConnection; //***
    /// <summary>
    /// 連線資料庫
    /// </summary>
    private void btn1_Click_1(object sender,EventArgs e)
    {
      if (txt1.Text == "")
      {
        MessageBox.Show("請輸入資料庫名稱:");
      }
      else
      {
        try
        {
          string connString = "server=.;database=" + txt1.Text.Trim() + ";uid=test;pwd=test;connect timeout=5"; //***
          sqlConnection = new SqlConnection(connString); //***
          sqlConnection.Open(); //***

          if (sqlConnection.State == ConnectionState.Open)
          {
            btn1.Text = "連線成功";
          }
        }
        catch(Exception ex) 
        {
          MessageBox.Show(ex.Message);
          txt1.Text = "";
        }
      }
    }

    /// <summary>
    /// 使用Close方法關閉連線並重新呼叫Open方法連線資料庫
    /// </summary>
    private void btn2_Click(object sender,EventArgs e)
    {
      try
      {
        string str = "";

        sqlConnection.Close(); //***
        if (sqlConnection.State == ConnectionState.Closed)
        {
          str = "資料庫已經成功關閉\n";
        }

        sqlConnection.Open(); //***
        if (sqlConnection.State == ConnectionState.Open)
        {
          str += "資料庫已經成功開啟\n";
        }

        rtbox1.Text = str;
      }
      catch (Exception ex)
      {
        rtbox1.Text = ex.Message;
      }
    }

    /// <summary>
    /// 使用Dispose方法關閉連線並重新呼叫Open方法連線資料庫
    /// </summary>
    private void btn3_Click(object sender,EventArgs e)
    {
      try
      {
        sqlConnection.Dispose(); //***
        sqlConnection.Open(); //***
      }
      catch (Exception ex)
      {
        rtbox1.Text = ex.Message;
      }
    }

以上就是本次介紹的全部知識點內容,感謝大家的學習和對我們的支援。