141019●用C#操作SQL資料庫

來源:互聯網
上載者:User

標籤:io   os   ar   使用   sp   資料   on   cti   代碼   

資料庫操作:ADO.NET

一、命名空間:

         using System.Data;

         using System.Data.SqlClient;     //針對SqlServer進行最佳化了的資料訪問類的空間

         System.Data.OleDB;System.Data.ODBC;    //命名空間通用資料訪問類的空間。

 

ODBC——Open DataBase Connection開放式資料互聯

 

二、步驟:

         1、建立連結化物件——建一個通向資料庫的通道。

         2、開啟通道

         3、操作資料庫

         4、關閉通道

 

三、類及使用

         連結字串

                  方法一:server=.;database=mydb;uid=sa;pwd=123

                   方法二:功能表列中“視圖”→“伺服器總管”→右鍵→“添加串連”→選擇“Sql Server”→VS視窗右邊的“屬性”→“連結字串”

 

(一)、連結化物件:

         SqlConnection:連結資料庫的類

1、建構函式:

         SqlConnection()

         SqlConnection(string connectionString)

2、屬性:

         ConnectionString:字串類型,連接字串

         State:串連狀態

3、方法:

         Open()

         Close()

         CreateCommand();    //產生一個通過本連結化物件來訪問資料庫的SqlCommand執行個體。

4、舉例

         string connectionString = "server=.;database=mydb;uid=sa;pwd=123";

         SqlConnection conn = new SqlConnection(connectionString);

         conn.Open();

         //操作

         conn.Close();

(二)、命令對象

         SqlCommand:資料庫操作的命令對象

1、建構函式:

         SqlCommand();  //推薦

         SqlCommand(string sql)

         SqlCommand(string sql,SqlConnection conn)

2、屬性:

         CommandText:字串類型,要執行的SQL語句、預存程序

         CommandType:CommandType枚舉類型。CommandType.Text--要執行的是SQL語句(預設);CommandType.StoredProcedure——要執行的是預存程序。

      如何調用預存程序?

        1.把CommandText賦為預存程序的名。

        2.把CommandType賦為CommandType.StoredProcedure

        3.使用cmd.Parameters.AddWithValue()為預存程序參數賦值。

         Connection:SqlConnection類型,通過哪個連結通道訪問資料庫

         Parameters:對CommandText中的SQLServer局部變數進行賦值或取值

3.方法:

         ExecuteNonQuery();    //執行增刪改的方法,返回影響的行數。

         ExecuteReader();    //用來執行查詢的方法,返回SqlDataReader對象,用來執行查詢。

         ExecuteScalar();    //返回首行首列,一般用來執行統計查詢。

                   int count=(int)ExecuteScalar();

4、舉例:

         string connectionString = "server=.;database=mydb;uid=sa;pwd=123";

         SqlConnection conn = new SqlConnection(connectionString);

 

         SqlCommand cmd = new SqlCommand();

         cmd.Connection = conn;

         cmd.CommandText = "Sql語句";

 

         conn.Open();

         cmd.ExecuteNonQuery();

         conn.Close();

 

(三)、讀取器對象。在記憶體中只佔一條資料的空間。擷取資料庫中資料。SqlDataReader

1、建構函式

         無法new出來。原因是,它的建構函式是非public的。

         它只有一種執行個體化的方式:SqlDataReader dr = cmd.ExecuteReader();

2、屬性

         .HasRows    //返回bool型資料,判斷讀取器中是否能讀取資料。

3、方法

         .Read();    //把資料讀取到SqlDataReader對象中來。如果讀取成功,返回true;否則返回false。

         dr.Close();    //關閉讀取器。關閉連結時,讀取器隨之關閉。

         dr[“列名”];    //讀取記憶體中當前SqlDataReader對象中的某列資料,讀出來的資料是Object類型。

         dr[索引號];    //讀取記憶體中當前SqlDataReader對象中的某列資料,讀出來的資料是Object類型。

         dr.GetString(索引號),dr.Getint(索引號)……

4、舉例:

    string connectionString = @"server=.\sqlexpress;database=mydb;uid=sa;pwd=sa";

    SqlConnection conn = new SqlConnection(connectionString);

 

    SqlCommand cmd = new SqlCommand();

    cmd.Connection = conn;

    cmd.CommandText = "select * from info";

           

    conn.Open();

    SqlDataReader dr = cmd.ExecuteReader();

    while (dr.Read())

    {

        Console.WriteLine(dr["Code"].ToString()+dr["Name"].ToString());

    }

    conn.Close();

 

 

服務——伺服器管理員

 

 

 

 

 

 

裝箱(Boxing)

         把資料從實值型別變成參考型別;把資料從棧空間轉型到堆空間。

拆箱(Unboxing)

         把資料從參考型別變成實值型別;把資料從堆空間轉型到棧空間。

 

         int n=19;

         object obj=n;    //裝箱。

         n=20;

         int m=(int)obj;    //拆箱。

 

 

壞處:

         1、裝箱,占空間,占時間,運行慢。

         2、拆箱,佔用時間,可能出現類型異常。

 

異常處理

         異常,與正常情況有差異,不一定是錯誤。

 

         try

         {

                   可能會出現異常的代碼;

         }

         [catch[(異常類型 異常對象)]

         {

                   一旦出現異常,將會進入此處,

                   對錯誤資訊進行處理;

         }]

         [finally    //不論程式是否運行正常,最後一定執行,常用來關閉連結通道。

         {

         }]

 

141019●用C#操作SQL資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.