走進ADO.NET的世界
ADO.NET通過封裝一些對象來實現(xiàn)C#與數(shù)據(jù)庫的連接,其實就是一個橋梁。
下面先通過SQL Server來看ADO.NET是怎么樣具體連接數(shù)據(jù)庫的。
首先看一下在Visual Studio中創(chuàng)建數(shù)據(jù)庫(Visual Studio內(nèi)置一個數(shù)據(jù)庫服務器)
在視圖上打開服務器資源管理器,在Visual Studio左邊會看到
右鍵數(shù)據(jù)庫連接,創(chuàng)建數(shù)據(jù)庫
其中的“.”代表本地服務器,新創(chuàng)建的數(shù)據(jù)名字PersonDB,創(chuàng)建一個人Persons表,三個字段ID整形自動增長為主鍵,一個PersonName,nvarchar(15),存儲人的姓名,PersonAge整形存儲人的年紀。好了表創(chuàng)建好了,接下來就看看怎樣把它同程序進行連接,來實現(xiàn)數(shù)據(jù)庫的增刪改查。
創(chuàng)建一個windowForm程序,界面如下:
我們先來實現(xiàn)添加操作,首先引用命名空間:using System.Data.SqlClient;
首先先來連接數(shù)據(jù)庫,通過SqlConnection創(chuàng)建連接對象。
- SqlConnection con = new SqlConnection();//創(chuàng)建連接對象
要想連接數(shù)據(jù)庫,首先要知道你是要連接哪一個服務器,數(shù)據(jù)庫名稱等等。
- con.ConnectionString = "server=.;database=persondb;uid=sa;pwd=sa";
- //分別表示服務器名稱、數(shù)據(jù)庫名稱、登錄用戶名及密碼
接下來創(chuàng)建sql命令對象:
- SqlCommand cmd = con.CreateCommand();//sql命令對象,表示要對sql數(shù)據(jù)庫執(zhí)行一個sql語句
- cmd.CommandText = "insert into persons(personname,personage) values(@name,@age)";
- //sql語句@name,@age表示兩個參數(shù)
- cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = textBox1.Text;
- //給參數(shù)賦值,并指定類型
- cmd.Parameters.Add("@age", SqlDbType.Int).Value = textBox2.Text;
- con.Open();//打開連接
- cmd.ExecuteNonQuery();
- //執(zhí)行不是查詢的sql語句
- MessageBox.Show("插入成功!");
打開數(shù)據(jù)庫連接一定要記得關(guān)閉連接:
- catch (Exception ex)//由于sql語句對半全角很敏感,捕捉異常
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- con.Close();//不管打開成功還是失敗,都能關(guān)閉連接
- }
好了,到這一步插入操作做完了,接下來看一下能不能往表中插入數(shù)據(jù)。
點擊添加,我們?nèi)ersons表中看一下有沒有成功插入數(shù)據(jù)
我們看到“張三”被成功添加進去了,我們再來多添加幾條數(shù)據(jù)。
接下來看一下查詢操作怎么做,其實和插入操作沒多大區(qū)別,只是多了一個SqlDataReader讀者對象:下面來看一下源代碼:
- try
- {
- con.ConnectionString = "server=.;database=persondb;uid=sa;pwd=sa";
- //分別表示服務器名稱、數(shù)據(jù)庫名稱、登錄用戶名及密碼
- SqlCommand cmd = con.CreateCommand();
- cmd.CommandText = "select id,personname,personage from persons";
- //查詢所有
- con.Open();
- SqlDataReader dr = cmd.ExecuteReader();//提供一種從 SQL Server 數(shù)據(jù)庫讀取行的只進流的方式
- listBox1.Items.Clear();//清除listbox數(shù)據(jù)
- comboBox1.Items.Clear();//清除combox數(shù)據(jù)
- while (dr.Read())//一行一行讀取數(shù)據(jù),返回值是bool類型
- {
- listBox1.Items.Add("姓名:"+dr.GetValue(1).ToString() + " 年齡:"+dr.GetValue(2).ToString());
- comboBox1.Items.Add(dr.GetValue(0));//把ID放到combox中,方便刪除及修改
- }
- dr.Close();//關(guān)閉讀取流操作
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- con.Close();
- }
來看一下查詢結(jié)果:
接下來在來實現(xiàn)刪除操作,和插入幾乎一樣,先從數(shù)據(jù)庫中查詢出所有數(shù)據(jù),獲得它們的ID(都放到combox里面了)
下面看一下具體實現(xiàn):
- try
- {
- con.ConnectionString = "server=.;database=persondb;uid=sa;pwd=sa";
- SqlCommand cmd = con.CreateCommand();
- con.Open();
- cmd.CommandText = "delete persons where id=@id";//刪除語句,已ID為條件刪除
- cmd.Parameters.Add("@id", SqlDbType.Int).Value =comboBox1.Text;
- cmd.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- con.Close();
- }
先查詢,
選擇刪除ID,
點擊刪除,再來查詢一下
我們看到朱重八被刪除了。
最后來做修改,這個比較麻煩,首先要修獲得要修改的數(shù)據(jù),先獲得所有數(shù)據(jù),在通過ID來實現(xiàn)查詢當個數(shù)據(jù),然后在確定修改
看修改查詢代碼:
- try
- {
- con.ConnectionString = "server=.;database=persondb;uid=sa;pwd=sa";
- SqlCommand cmd = con.CreateCommand();
- cmd.CommandText = "select personname,personage from persons where id=@id";
- cmd.Parameters.Add("@id",SqlDbType.Int).Value = comboBox1.Text;
- con.Open();
- SqlDataReader dr = cmd.ExecuteReader();
- dr.Read();//因為只讀一條數(shù)據(jù),就不用while循環(huán)
- textBox1.Text = dr.GetValue(0).ToString();
- textBox2.Text = dr.GetValue(1).ToString();
- dr.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- con.Close();
- }
再來看一下確定修改代碼:
- try
- {
- con.ConnectionString = "server=.;database=persondb;uid=sa;pwd=sa";
- SqlCommand cmd = con.CreateCommand();
- con.Open();
- cmd.CommandText = "update persons set personname=@name,personage=@age where id=@id";
- cmd.Parameters.Add("@id", SqlDbType.Int).Value = comboBox1.Text;
- cmd.Parameters.Add("@name",SqlDbType.NVarChar).Value=textBox1.Text;
- cmd.Parameters.Add("@age", SqlDbType.NVarChar).Value = textBox2.Text;
- cmd.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- con.Close();
- }
好了,我們先查詢?nèi)啃畔?,在通過ID獲得修改信息
把名字改成,七匹馬,確定修改,再來查詢一下是否修改成功
現(xiàn)在可以看到修改成功了。
到這里,基礎(chǔ)性的東西都講完了,還有SqlDataAdapter,DataTable類,下次在講。
鏈接:http://www.cnblogs.com/wjfluisfigo/archive/2010/05/23/1742034.html


2011-05-20 11:31:07
2009-11-12 13:53:27




