ADO.NET中有關SqlConnection與Sqlcommand 的介紹與應用

ADO.NET中有關SqlConnection與Sqlcommand 的介紹與應用sql

思惟導圖:數據庫

 

ADO.NET提供5個主要的類的對象來實現數據的鏈接訪問和離線訪問。這5個類分別是ConnectionCommandDataReaderDataAdapterDataSet服務器

SqlConnection 的介紹與應用網絡

一、介紹與做用函數

SqlConnectionADO.NET中的鏈接類。ui

 使用sqlconnection 類能夠鏈接到SQL  Server數據庫。this

二、主要屬性以及方法spa

   主要屬性:ConnectionString(鏈接字符串)orm

   其它屬性簡介:對象

屬性

說明

ClientConnectionId

最近鏈接嘗試鏈接的 ID,不管該嘗試是成功仍是失敗。

ConnectionTimeout

獲取在嘗試創建鏈接時終止嘗試並生成錯誤以前所等待的時間。 (重寫 DbConnectionConnectionTimeout。)

Container

獲取 IContainer,它包含 Component。 (繼承自 Component。)

Credential

獲取或設置此鏈接的 SqlCredential 對象。

Database

獲取當前數據庫或鏈接打開後要使用的數據庫的名稱。 (重寫 DbConnectionDatabase。)

DataSource

獲取要鏈接的 SQL Server 實例的名稱。 (重寫 DbConnectionDataSource。)

FireInfoMessageEventOnUserErrors

獲取或設置 FireInfoMessageEventOnUserErrors 屬性。

PacketSize

用來與 SQL Server 的實例進行通訊的網絡數據包的大小,以字節爲單位。

ServerVersion

獲取包含客戶端鏈接的 SQL Server 實例的版本的字符串。 (重寫 DbConnectionServerVersion。)

Site

獲取或設置 Component ISite。 (繼承自 Component。)

State

指示最近在鏈接上執行網絡操做時, SqlConnection 的狀態。 (重寫 DbConnectionState。)

StatisticsEnabled

若是設置爲 true,則對當前鏈接啓用統計信息收集。

WorkstationId

獲取標識數據庫客戶端的一個字符串。

方法:Open();(打開數據庫)  Close();(關閉數據庫)

構造函數:

名稱

說明

SqlConnection

初始化 SqlConnection 類的新實例。

SqlConnection(String)

若是給定包含鏈接字符串的字符串,則初始化 SqlConnection 類的新實例。

SqlConnection(String, SqlCredential)

初始化給定鏈接字符串的 SqlConnection 類的新實例,而不使用包含用戶識別號和密碼的 Integrated Security = true SqlCredential 對象。

 

三、建立SqlConnection對象:

  可使用定義好的鏈接字符串建立SqlConnection對象。代碼以下

  SqlConnection  connection=new  SqlConnection(connectionString);

要訪問數據源,必須先創建鏈接。這個鏈接對象裏描述了數據庫服務器類型、數據庫的名字、用戶名、密碼等參數。

 SqlConnection  conn=new  SqlConnection();

conn.ConnectionString=」Data Source=(Local);

Initial  Catalog=pubs;  uid=sa;  pwd=sa;」

調用SqlConnection 對象的Open()Close() 方法來打開和關閉數據庫的代碼以下:

打開數據庫:connection.Open();

關閉數據庫:connection.Close();

 

SqlCommand的介紹與應用

  SqlCommand屬於命令類

一、做用:

 SqlCommand 對象用於執行具體的SQL語句,如增長、刪除、修改、查找。

二、主要屬性以及方法

SqlCommand對象的經常使用屬性

屬  性

說  明

Connection屬性

指定Command對象所使用的Connection對象。

CommandType屬性

指定Command對象的類型,有3種選擇:

1 Text:表示Command對象用於執行SQL語句。

2 StoredProcedure:表示Command對象用於執行存儲過程。

3 TableDirect:表示Command對象用於直接處理某個表。

CommandType屬性的默認值爲Text

CommandText屬性

根據CommandType屬性的取值來決定CommandText屬性的取值,分爲3種狀況:

若是CommandType屬性取值爲Text,則CommandText屬性指出SQL語句的內容

若是CommandType屬性取值爲StoredProcedure,則CommandText屬性指出存儲過程的名稱

若是CommandType屬性取值爲TableDirect,則CommandText屬性指出表的名稱

CommandText屬性的默認值爲SQL語句

CommandTimeout屬性

指定Command對象用於執行命令的最長延遲時間,以秒爲單位,若是在指定時間內仍不能開始執行命令,則返回失敗信息。

默認值爲30秒。

Parameters屬性

指定一個參數集合。

SqlCommand對象的重要方法:

方法

說明

ExecuteScalar

執行查詢,並返回查詢結果中的第一行第一列的值,類型是object

ExecuteNonQuery

執行SQL語句並返回受影響的行數

ExecuteReader

執行查詢命令,返回SqlDataReader對象

 

三、SqlCommand對象的使用步驟以下

(1)建立SqlConnection對象

 SqlConnection  connection=new  SqlConnection(connectionString);

(2)定義SQL語句

 把所要執行的SQL語句賦給字符串。

(3)建立SqlCommand對象

  SqlCommand command = new SqlCommand();

(4)調用SqlCommand對象的某個方法,執行SQL語句。

 注意在調用SqlCommand 對象的某個方法以前,必定要打開數據庫鏈接,不然程序會出錯

舉例以下:

public partial class Form1 : Form

    {

        //數據庫鏈接字符串       

        private static string connString = "Data Source=localhost;Initial Catalog=QQ;Integrated Security=true";

        //數據庫鏈接對象

        public static SqlConnection connection = new SqlConnection(connString);

        public Form1()

        {

            InitializeComponent();

        }

        //執行SQL語句

        private void btnSql_Click(object sender, EventArgs e)

        {

            try

            {

                //查詢用的 SQL 語句

                string selectSql = "select count(*) from Users";

                //建立Command對象

                SqlCommand command = new SqlCommand();

                //指定Command對象所使用的Connection對象

                command.Connection = connection;

                //指定Command對象用於執行SQL語句

                command.CommandType = CommandType.Text;

                //指定要執行的SQL語句

                command.CommandText = selectSql;        

                //打開數據庫鏈接

                connection.Open();

                //執行查詢操做,返回單個值

                this.txtCount.Text = command.ExecuteScalar().ToString();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            finally

            {

                //關閉數據庫鏈接

                connection.Close();

            }

        }

        //執行存儲過程

        private void btnStoreProc_Click(object sender, EventArgs e)

        {

            try

            {

                //建立Command對象

                SqlCommand command = new SqlCommand();

                //指定Command對象所使用的Connection對象

                command.Connection = connection;

                //指定Command對象用於執行存儲過程

                command.CommandType = CommandType.StoredProcedure;

                //指定要執行的存儲過程的名稱

                command.CommandText = "procSelect1";               

                //打開數據庫鏈接

                connection.Open();

                //執行查詢操做,返回單個值

                this.txtCount.Text = command.ExecuteScalar().ToString();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            finally

            {

                //關閉數據庫鏈接

                connection.Close();

            }

        }

}

相關文章
相關標籤/搜索