第7章獲取數(shù)據(jù)_第1頁(yè)
第7章獲取數(shù)據(jù)_第2頁(yè)
第7章獲取數(shù)據(jù)_第3頁(yè)
第7章獲取數(shù)據(jù)_第4頁(yè)
第7章獲取數(shù)據(jù)_第5頁(yè)
已閱讀5頁(yè),還剩24頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、第7章 獲取數(shù)據(jù)7.1 ADO.NET簡(jiǎn)介ADO.NET 是與C#和 Microsoft.NET framework一起使用的類集的名稱,用于以關(guān)系型的、面向表的格式訪問(wèn)數(shù)據(jù)。這包括關(guān)系數(shù)據(jù)庫(kù),比如Microsoft Access 和SQL Server,以及其他數(shù)據(jù)庫(kù),甚至還包括非關(guān)系數(shù)據(jù)源。ADO.NET被集成到.NET framework,用于.NET語(yǔ)言。ADO.NET包含System.Data名稱空間中的一組對(duì)象,System.Data名稱空間可以通過(guò).NET的數(shù)據(jù)提供者(provider)與數(shù)據(jù)庫(kù)通信。ADO.NET的名稱來(lái)源于ADO(ActiveX Data Objects),這

2、是用途廣泛的類組,用于在以往的Microsoft技術(shù)中訪問(wèn)數(shù)據(jù)。之所以使用ADO.NET名稱,是因?yàn)镸icrosoft希望表明,這是在.NET編程環(huán)境中優(yōu)先使用的數(shù)據(jù)訪問(wèn)接口。ADO.NET的主要目標(biāo)是提供對(duì)關(guān)系數(shù)據(jù)的簡(jiǎn)單訪問(wèn)功能。ADO.NET提供兩個(gè)核心組件:DataSet和.NET數(shù)據(jù)提供者程序。ADO.NETDataSet組件為ADO.NET提供斷開(kāi)式結(jié)構(gòu)服務(wù),它的設(shè)計(jì)是為了實(shí)現(xiàn)獨(dú)立于任何數(shù)據(jù)源的數(shù)據(jù)訪問(wèn)。因此,它可以用于多種不同的數(shù)據(jù)源。.NET數(shù)據(jù)提供者程序用于連接到數(shù)據(jù)源,檢索、修改數(shù)據(jù)源中的數(shù)據(jù)。開(kāi)發(fā)人員可以將檢索到的結(jié)果放入ADO.NETDataSet。在實(shí)際應(yīng)用中,.NET

3、數(shù)據(jù)提供者程序在數(shù)據(jù)源和ADO.NETDataSet之間起著橋梁的作用。ADO.NET提供了兩種內(nèi)置的ADO.NET數(shù)據(jù)提供者,一種是OLE DB.NET數(shù)據(jù)提供程序,用于使用OLE DB公開(kāi)的數(shù)據(jù)源(如Microsoft Access),另一種是SQL Server.NET提供者程序,用于使用Microsoft SQL Server7.0或更高版本。另外,Microsoft最近預(yù)演了用于ADO.NET的ODBC.NET提供者,它允許.NET訪問(wèn)更多的舊的數(shù)據(jù)格式和第三方數(shù)據(jù)庫(kù)。7.2使用ADO.NET訪問(wèn)數(shù)據(jù)庫(kù)7.2.1 ADO.NET類和對(duì)象概述可以把類分為.NET提供者對(duì)象和用戶對(duì)象。提

4、供者對(duì)象專用于每一種類型的數(shù)據(jù)源;專用于提供者的對(duì)象完成在數(shù)據(jù)源中實(shí)際的讀取和寫(xiě)入工作。用戶對(duì)象是將數(shù)據(jù)讀入到內(nèi)存中后用來(lái)訪問(wèn)和操縱數(shù)據(jù)的對(duì)象。用戶對(duì)象以非連接方式使用;提供者對(duì)象要求活動(dòng)的連接,可以使用它們首先讀取數(shù)據(jù),然后根據(jù)需要,通過(guò)用戶對(duì)象使用內(nèi)存中的數(shù)據(jù),通過(guò)使用者對(duì)象更新數(shù)據(jù)源中的數(shù)據(jù),并將變動(dòng)寫(xiě)回到數(shù)據(jù)源中。提供者對(duì)象即在每一個(gè).NET數(shù)據(jù)提供者中定義的對(duì)象,其名稱前帶有專用于提供者的名稱。1連接對(duì)象Connection連接對(duì)象是您要使用的第一個(gè)對(duì)象,被要求用于任何其他ADO.NET對(duì)象之前。它提供了到數(shù)據(jù)源的基本連接。專用于提供者的名稱包括用于SQL Server7.0的Sql

5、Connection和用于 OLE DB 的 OleDbConnection。2命令對(duì)象Command可以使用此對(duì)象發(fā)出命令,比如對(duì)數(shù)據(jù)源的查詢,專用于提供者的名稱包括用于SQL Server 的SqlCommand 和用于 OLE DB 的 OleDbCommand。3 CommandBuilder對(duì)象此對(duì)象用于構(gòu)建SQL命令,可以在基于單一表查詢的對(duì)象中進(jìn)行數(shù)據(jù)修改。專用于提供者的名稱包括用于SQL Server7.0的SqlCommandBuilder和用于OLE DB 的OleDbCommandBuilder。4 DataReader對(duì)象可以從數(shù)據(jù)源中讀取僅能前向和只讀的數(shù)據(jù)流。此對(duì)象

6、具有最好的功能,可以簡(jiǎn)單地讀取數(shù)據(jù);專用于提供者的名稱包括用于SQL Server7.0的SqlDataReader和用于OLE DB的OledbDataReader。5 DataAdapter對(duì)象提供連接DataSet對(duì)象和數(shù)據(jù)源的橋梁,DataAdapter使用Command對(duì)象在數(shù)據(jù)源中執(zhí)行SQL命令,對(duì)數(shù)據(jù)源進(jìn)行各種操作,包括更新變動(dòng)的數(shù)據(jù),填充數(shù)據(jù)集以及其他操作。專用于提供者的名稱包括用于SQL Server7.0的SqlDataAdapter和用于OLE DB的OledbAdapter。7.2.3 用戶對(duì)象DataSet此對(duì)象表示一組相關(guān)表,在應(yīng)用程序中這些表作為一個(gè)單元被引用。有

7、了此對(duì)象,就可以快速?gòu)拿恳粋€(gè)表中獲取所需要的數(shù)據(jù),當(dāng)與服務(wù)器斷開(kāi)時(shí)檢查并修改數(shù)據(jù),然后在一次操作中使用這些修改的數(shù)據(jù)更新服務(wù)器。DataSet具有屬性,使得您可以訪問(wèn)低級(jí)對(duì)象,這些對(duì)象代表單獨(dú)的表、行、列和關(guān)系。這些對(duì)象是:1. DataTable對(duì)象此對(duì)象代表在DataSet中的表。可以在DataSet中存儲(chǔ)多個(gè)DataTable對(duì)象。2. DataRow對(duì)象此對(duì)象代表來(lái)自表的關(guān)聯(lián)數(shù)據(jù)的行??梢栽贒ataTable中存儲(chǔ)多個(gè)DataRow對(duì)象。3.DataColumn 對(duì)象此對(duì)象代表表中的列??梢栽贒ataRow中存儲(chǔ)多個(gè)DataColumn對(duì)象。4. DataRelation對(duì)象此對(duì)象代表

8、通過(guò)共享列而發(fā)生關(guān)系的兩個(gè)表之間的關(guān)系。 使用System.Data名稱空間在C#代碼中使用ADO.NET的第一步是引用System.Data名稱空間,其中含有所有的ADO.NET類。1. SQL Server.NET數(shù)據(jù)提供者如果您使用的是SQL Server(版本7或更高級(jí)的版本)或MSDE,則通過(guò)使用SQL Server專用的.NET數(shù)據(jù)提供者就可以獲得最好的性能和對(duì)基礎(chǔ)性的功能的最直接的訪問(wèn),按如下方式使用using 指令可以引用SQL Server 專用的.NET數(shù)據(jù)提供者:using System.Data.SqlClient;2. OLE DB.NET提供者對(duì)于不是SQL Ser

9、ver7.0或以上版本的大多數(shù)數(shù)據(jù)源(Microsoft Access, Oralce 以及其他數(shù)據(jù)源)可以使用OLE DB.NET數(shù)據(jù)提供者,按如下方式使用using指令引用它:using System.Data.OleDb;*7.3 在VS中訪問(wèn)數(shù)據(jù)例題1:建立SQL Server數(shù)據(jù)庫(kù)student,顯示其中一個(gè)表xs的記錄內(nèi)容,并可查詢、修改、刪除其記錄。(參見(jiàn)D:GZC#ado數(shù)據(jù)庫(kù)sql 程序)一 建立SQL Server數(shù)據(jù)庫(kù)啟動(dòng)Microsoft SQL Serverà企業(yè)管理器àMicrosoft SQL ServersàSQL Servers組

10、àlocal(Windows NT)à右鍵單擊數(shù)據(jù)庫(kù)à單擊新建數(shù)據(jù)庫(kù)à輸入新建數(shù)據(jù)庫(kù)名(student)à右鍵單擊 studentà單擊新建表,輸入表中各字段定義,確定主鍵à關(guān)閉該窗口并以xs為名存儲(chǔ)à右鍵單擊xs表à單擊打開(kāi)表à單擊返回所有行à輸入表中各記錄值à關(guān)閉該窗口。二將C#應(yīng)用程序與數(shù)據(jù)庫(kù)連接1.建立數(shù)據(jù)適配器到數(shù)據(jù)庫(kù)的連接在工具箱中單擊“數(shù)據(jù)”標(biāo)簽à雙擊“SqlDataAdapter”,打開(kāi)“數(shù)據(jù)適配器配置向?qū)А?#224;單擊“下一步”à選“

11、新建數(shù)據(jù)庫(kù)連接”,打開(kāi)“數(shù)據(jù)鏈接屬性”對(duì)話框à輸入服務(wù)器名(選擇默認(rèn)的即可),選擇“使用windows集成安全”,在服務(wù)器上選擇數(shù)據(jù)庫(kù)“student”,點(diǎn)擊“測(cè)試連接”按鈕,顯示“連接成功”à單擊“確定”à單擊“下一步”à選擇“使用SQL語(yǔ)句”,單擊“下一步”à單擊“查詢生成器”à添加表xsà選“*”,單擊“確定”à單擊“下一步”à單擊“完成”此時(shí),表單下添加了兩個(gè)對(duì)象,即sqlDataAdapter1和sqlConnection1。數(shù)據(jù)適配器包含我們希望從數(shù)據(jù)庫(kù)中返回的數(shù)據(jù)的基本信息,是.NET數(shù)據(jù)提

12、供者的一個(gè)組件,用于將來(lái)自數(shù)據(jù)庫(kù)的數(shù)據(jù)填充到DataSet中,并將在DataSet中的變化返回到數(shù)據(jù)庫(kù)中。sqlConnection對(duì)象包含了ADO.NET 用來(lái)連接到數(shù)據(jù)庫(kù)的所有信息。2.添加DataSet右鍵單擊剛才創(chuàng)建的sqlDataAdapter1à選擇“生成數(shù)據(jù)集”à選擇“新建”DataSet1,選擇“添加到數(shù)據(jù)集中的表xs”à單擊“確定”此時(shí)Form下添加了一個(gè)新的對(duì)象DataSet11,同時(shí),解決方案中添加了一個(gè)新文件DataSet1.xsd。此文件是DataSet的模式。XSD(XML模式定義)模式是一個(gè)文檔,通過(guò)它,我們可以驗(yàn)證XML文檔的結(jié)構(gòu),

13、XML是用于表示數(shù)據(jù)的文本格式。因?yàn)镈ataSet在后臺(tái)使用XML組織數(shù)據(jù),此文件由VS自動(dòng)生成,它規(guī)定了數(shù)據(jù)集、每一個(gè)表及表之間的關(guān)系的結(jié)構(gòu)。3.文本框與數(shù)據(jù)庫(kù)數(shù)據(jù)的綁定在表單上創(chuàng)建文本框txtxh,在其DataBindings屬性細(xì)目上,單擊其text部分,出現(xiàn)datasete11-xs-xh xm,單擊“xh”,則txtxh與xs表中的xh字段綁定。同樣方法,可將另一個(gè)文本框txtxm與xm字段綁定。這僅僅意味著在文本框中可以看見(jiàn)數(shù)據(jù)集中的數(shù)據(jù),用戶在文本框中所作的任何變化都可以反映到DataSete中。但這不會(huì)影響數(shù)據(jù)源(即數(shù)據(jù)庫(kù))中的數(shù)據(jù),除非用更新命令修改表中數(shù)據(jù)。4.上述操作僅定

14、義了來(lái)自表中的數(shù)據(jù)的結(jié)構(gòu),還沒(méi)有定義實(shí)際的數(shù)據(jù),下面的命令是通知數(shù)據(jù)適配器填充DataSet,即使應(yīng)用程序獲取數(shù)據(jù):可單擊表單(Form)背景,進(jìn)入代碼編輯器,(或制作一查詢按鈕,在其事件處理程序中,)寫(xiě)如下代碼: this.sqlDataAdapter1.Fill(this.dataSet11,0,0,”xs”);Fill()方法使用來(lái)自數(shù)據(jù)源的數(shù)據(jù)填充DataSet中的DataTable。此方法帶有4個(gè)參數(shù)。第一個(gè)參數(shù)規(guī)定希望填充的DataSet,在此就是dataSet11 。第二個(gè)參數(shù)規(guī)定,應(yīng)該從第幾個(gè)記錄開(kāi)始。第三個(gè)參數(shù)規(guī)定返回幾個(gè)記錄。如果設(shè)為0,則返回所有記錄。第4個(gè)參數(shù)規(guī)定生成數(shù)

15、據(jù)的表的名稱,在此就是xs?,F(xiàn)在,保存并運(yùn)行該程序,可在文本框中看到xs表中的首行記錄值。5.添加導(dǎo)航按鈕,以便看到表中全部記錄。見(jiàn)源程序代碼。6.使用DataGrid瀏覽表中所有數(shù)據(jù)在其DataSouce屬性中設(shè)置dataset11.xs主要代碼如下:staticvoidMain() Application.Run(new Form1();privatevoid Form1_Load(object sender, System.EventArgs e)/this.sqlDataAdapter1.Fill(this.dataSet11,0,0,"xs");privatevo

16、id btnback_Click(object sender, System.EventArgs e)this.BindingContextthis.dataSet11,"xs".Position-;privatevoid btnnext_Click(object sender, System.EventArgs e)this.BindingContextthis.dataSet11,"xs".Position+;privatevoid btnupdate_Click(object sender, System.EventArgs e)this.sqlD

17、ataAdapter1.Update(this.dataSet11.xs);privatevoid btnqer_Click(object sender, System.EventArgs e)this.dataSet11.Clear();this.sqlDataAdapter1.Fill(this.dataSet11,0,0,"xs");privatevoid btnreset_Click(object sender, System.EventArgs e)this.dataSet11.Clear();*例題2:建立Access數(shù)據(jù)庫(kù),顯示其中一個(gè)表的記錄內(nèi)容,并可查詢、

18、修改、刪除其記錄。(參見(jiàn)D:GZC#備課Database1程序)staticvoidMain() Application.Run(new DateBase2();privatevoid Form1_Load(object sender, System.EventArgs e)/this.oleDbDataAdapter1.Fill(this.dataSet11,0,0,"t1");privatevoid buttonback_Click(object sender, System.EventArgs e)this.BindingContextthis.dataSet11,&

19、quot;t1".Position-;privatevoid buttonnext_Click(object sender, System.EventArgs e)this.BindingContextthis.dataSet11,"t1".Position+;privatevoid buttonupdate_Click(object sender, System.EventArgs e)this.oleDbDataAdapter1.Update(this.dataSet11.t1);privatevoid buttonq_Click(object sender,

20、 System.EventArgs e)this.dataSet11.Clear();this.oleDbDataAdapter1.Fill(this.dataSet11,0,0,"t1");privatevoid buttonr_Click(object sender, System.EventArgs e)this.dataSet11.Clear();7.4使用SQL Server.NET數(shù)據(jù)提供者應(yīng)用舉例例題1:用ADO.NET訪問(wèn)SQL數(shù)據(jù)庫(kù)在數(shù)據(jù)庫(kù)魏菊麗20086666中建立student表,其中含sno(主鍵),sname,ssex,sage,sdept等字段。

21、在Form1中制作一個(gè) comboBox控件,該控件含有CS,IS,MA三個(gè)系的選項(xiàng),讓用戶通過(guò)該控件選擇查詢學(xué)生所在系,在dataGridView控件中顯示查詢結(jié)果。代碼詳見(jiàn)d:gzc#ado數(shù)據(jù)庫(kù)ado_combox2005 程序1.“數(shù)據(jù)庫(kù)ado_combox2005”程序界面:2.其中,控件comboBox1中項(xiàng)目(Items)的設(shè)置:3.“數(shù)據(jù)庫(kù)ado_combox”程序中“查詢按鈕”運(yùn)行結(jié)果界面:4.“數(shù)據(jù)庫(kù)ado_combox2005”程序代碼:/用戶添加,可以引用SQL Server專用的.NET數(shù)據(jù)提供者using System.Data.SqlClient;/以下butto

22、n1_Click是“數(shù)據(jù)庫(kù)ado_combox”程序“查詢”按鈕代碼privatevoid button1_Click(object sender, System.EventArgs e)/生成連接對(duì)象實(shí)例thisConnection連接SQL Server數(shù)據(jù)庫(kù)魏菊麗20086666SqlConnection thisConnection=new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=魏菊麗20086666");/*創(chuàng)建并返回一個(gè)與thisConnecti

23、on相關(guān)聯(lián)的SqlCommand 對(duì)象實(shí)例thisCommand,同時(shí)獲取或設(shè)置要對(duì)數(shù)據(jù)源執(zhí)行的SQL語(yǔ)句*/SqlCommand thisCommand=new SqlCommand("select * from student where sdept='"+comboBox1.Text+"'",thisConnection); /創(chuàng)建一個(gè)SqlDataAdapter對(duì)象實(shí)例thisAdapterSqlDataAdapter thisAdapter=new SqlDataAdapter();/獲取一個(gè)SQL語(yǔ)句,用于在數(shù)據(jù)源中選擇記錄t

24、hisAdapter.SelectCommand=thisCommand;DataSet thisDataSet=new DataSet();/生成數(shù)據(jù)集實(shí)例thisDataSetthisConnection.Open();/打開(kāi)本次設(shè)置的數(shù)據(jù)庫(kù)連接/將以上在數(shù)據(jù)源中選擇的記錄的所有行填充到數(shù)據(jù)集中,仍然命名為表student。thisAdapter.Fill(thisDataSet, "student");/*設(shè)置 dataGridView1的數(shù)據(jù)源為數(shù)據(jù)集中的數(shù)據(jù)*/this.dataGridView1.DataSource=thisDataSet.Tables0;th

25、isConnection.Close();/斷開(kāi)本次數(shù)據(jù)庫(kù)連接例題2:用ADO.NET訪問(wèn)SQL數(shù)據(jù)庫(kù)建立student數(shù)據(jù)庫(kù),在該庫(kù)中建立xs表,其中含xh(主鍵),xm字段。在Form1中制作一個(gè) dataGridView控件用來(lái)瀏覽數(shù)據(jù)庫(kù)表中的數(shù)據(jù)。制作一個(gè)ListBox用來(lái)顯示查詢結(jié)果。制作若干按鈕,分別完成瀏覽、添加、修改、刪除、查詢表中記錄的功能。詳見(jiàn)d:gz.C#課件2006新教案數(shù)據(jù)庫(kù)sqlado 程序1.本題界面設(shè)計(jì)如下:2.代碼如下:using System;using System.Drawing;using System.Collections;using System

26、.ComponentModel;using System.Windows.Forms;using System.Data;using System.Data.SqlClient;/用戶自己添加,可以引用SQL Server專用的.NET數(shù)據(jù)提供者staticvoidMain() Application.Run(new Form1();/以下功能是瀏覽數(shù)據(jù)庫(kù)student中的xs表的所有記錄privatevoid btnliulan_Click(object sender, System.EventArgs e)/用SqlConnection對(duì)象實(shí)例thisConnection連接SQL Ser

27、ver數(shù)據(jù)庫(kù)studentSqlConnection thisConnection=new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=student");/創(chuàng)建一個(gè)SqlDataAdapter對(duì)象實(shí)例thisAdapteSqlDataAdapter thisAdapter=new SqlDataAdapter();/生成數(shù)據(jù)集實(shí)例DataSet thisDataSet=new DataSet();/創(chuàng)建并返回一個(gè)與SqlConnection相關(guān)聯(lián)的SqlComm

28、and 對(duì)象SqlCommand thisCommand=thisConnection.CreateCommand();/獲取或設(shè)置要對(duì)數(shù)據(jù)源執(zhí)行的SQL語(yǔ)句thisCommand.CommandText="select xh,xm from xs"thisAdapter.SelectCommand =thisCommand ;/獲取一個(gè)SQL語(yǔ)句,用于在數(shù)據(jù)源中選擇記錄thisConnection.Open();/打開(kāi)本次設(shè)置的數(shù)據(jù)庫(kù)連接/將以上在數(shù)據(jù)源xs中選擇的記錄的所有行填充到數(shù)據(jù)集中,仍然命名為表xs。thisAdapter.Fill(thisDataSet,&q

29、uot;xs");thisConnection.Close();/斷開(kāi)本次數(shù)據(jù)庫(kù)連接/*設(shè)置 dataGridView1的數(shù)據(jù)源為數(shù)據(jù)集中的數(shù)據(jù)*/this.dataGridView1.DataSource = thisDataSet.Tables0;/以下功能是查詢數(shù)據(jù)庫(kù)student中的xs表中姓“a”的所有記錄/以下代碼是顯示查詢結(jié)果的另一種方法,即顯示在listBox1中。privatevoid btnchaxun_Click(object sender, System.EventArgs e)this.listBox1.Items.Clear();SqlConnection

30、 thisConnection=new SqlConnection("Data Source=laptop;Integrated Security=SSPI;Initial Catalog=student");SqlCommand thisCommand=thisConnection.CreateCommand();thisCommand.CommandText="select xm from xs where xm like 'a%'"thisConnection.Open();/*ExecuteReader()創(chuàng)建讀取器對(duì)象,用于讀取

31、生成的結(jié)果,將帶有所包含的結(jié)果的該讀取器指派給thisReader*/ SqlDataReader thisReader=thisCommand.ExecuteReader();/*Read()方法從查詢中讀取單獨(dú)的一行數(shù)據(jù).若有多個(gè)數(shù)據(jù)要讀,則返回true;如果沒(méi)有,則返回false.*/while(thisReader.Read()/*當(dāng)Read()返回true時(shí),輸出結(jié)果。SqlDataReader對(duì)象提供索引符屬性,允許引用列*/this.listBox1.Items.Add(thisReader"xm");thisReader.Close();thisConnec

32、tion.Close();/以下功能是向數(shù)據(jù)庫(kù)student中的xs表添加新記錄privatevoid btntj_Click(object sender, System.EventArgs e)SqlConnection thisConnection=new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=student");SqlDataAdapter thisAdapter=new SqlDataAdapter();DataSet thisDataSet=new

33、 DataSet();SqlCommand thisCommand=thisConnection.CreateCommand();thisCommand.CommandText="select xh,xm from xs "thisAdapter.SelectCommand =thisCommand ;thisConnection.Open();SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);thisAdapter.Fill(thisDataSet, "xs");Dat

34、aRow thisRow=thisDataSet.Tables"xs".NewRow();/在數(shù)據(jù)集的 xs Table中創(chuàng)建新行thisRow"xh"="555"thisRow"xm"="QQ"/設(shè)置新行中的個(gè)字段值thisDataSet.Tables"xs".Rows.Add(thisRow);/將新行添加到數(shù)據(jù)集的 xs Table中thisAdapter.Update(thisDataSet,"xs");/ 修改數(shù)據(jù)庫(kù)表thisConnection

35、.Close();/以下功能是修改數(shù)據(jù)庫(kù)student中的xs表的記錄privatevoid btnxg_Click(object sender, System.EventArgs e)SqlConnection thisConnection=new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=student");SqlDataAdapter thisAdapter=new SqlDataAdapter();DataSet thisDataSet=new Data

36、Set();SqlCommand thisCommand=thisConnection.CreateCommand();thisCommand.CommandText="select xh,xm from xs "thisAdapter.SelectCommand =thisCommand ;thisConnection.Open();SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);thisAdapter.Fill(thisDataSet, "xs"); thisDat

37、aSet.Tables"xs".Rows5"xm"="qq"thisAdapter.Update(thisDataSet,"xs");thisConnection.Close();/以下功能是刪除數(shù)據(jù)庫(kù)student中的xs表的記錄privatevoid btnsc_Click(object sender, System.EventArgs e)SqlConnection thisConnection=new SqlConnection("Data Source=localhost;Integrated

38、Security=SSPI;Initial Catalog=student");SqlDataAdapter thisAdapter=new SqlDataAdapter();DataSet thisDataSet=new DataSet();SqlCommand thisCommand=thisConnection.CreateCommand();thisCommand.CommandText="select xh,xm from xs "thisAdapter.SelectCommand =thisCommand ;thisConnection.Open();

39、SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);thisAdapter.Fill(thisDataSet, "xs");DataColumn keys=new DataColumn1;keys0=thisDataSet.Tables"xs".Columns"xh"/key0的值為該數(shù)據(jù)集xs Table中的xh值 thisDataSet.Tables"xs".PrimaryKey=keys;/設(shè)xh為主鍵DataRow findR

40、ow=thisDataSet.Tables"xs".Rows.Find("555");/獲取包含指定主鍵值"555"的行/ Call Update command to mark change in tableif(findRow !=null) /若找到該行findRow.Delete();/刪除該行thisAdapter.Update(thisDataSet,"xs");/修改數(shù)據(jù)庫(kù)表thisConnection.Close();privatevoid dataGrid1_Navigate(object sen

41、der, System.Windows.Forms.NavigateEventArgs ne)*7.5使用OLE DB.NET數(shù)據(jù)提供者應(yīng)用舉例1.使用ADO.NET獲取表中的數(shù)據(jù)。(參見(jiàn)D:GZC#備課DATA程序)using System;using System.Data;using System.Data.OleDb;namespace DATAclass DataReaderAccessExamplestaticvoidMain(string args)OleDbConnection thisConnection = new OleDbConnection("Provide

42、r=Microsoft.Jet.OLEDB.4.0;Data Source=D:gzc#備課db1.mdb");thisConnection.Open();/打開(kāi)連接對(duì)象,建立了到數(shù)據(jù)庫(kù)的連接OleDbCommand thisCommand=thisConnection.CreateCommand();/CreateCommand()方法可創(chuàng)建與此連接相關(guān)聯(lián)的命令thisCommand.CommandText ="SELECT xh,xm FROM t1"/命令本身被指派給命令對(duì)象的CommandText屬性O(shè)leDbDataReader thisReader=t

43、hisCommand.ExecuteReader();/*ExecuteReader()創(chuàng)建讀取器對(duì)象,用于讀取生成的結(jié)果,將帶有所包含的結(jié)果的該讀取器指派給thisReader*/while(thisReader.Read()/*Read()方法從查詢中讀取單獨(dú)的一行數(shù)據(jù).若有多個(gè)數(shù)據(jù)要讀,則返回true;如果沒(méi)有,則返回false.*/Console.WriteLine("t0t1",thisReader"xh",thisReader"xm");/*當(dāng)Read()返回true時(shí),輸出結(jié)果OleDbDataReader對(duì)象提供索引符

44、屬性,允許引用列,作為按列名稱的數(shù)組引用*/thisReader.Close();thisConnection.Close();2.使用ADO.NET修改表中的數(shù)據(jù)。(參見(jiàn)D:GZC#備課ModiData程序)sing System;using System.Data;using System.Data.OleDb;namespace UpdatingDataclass Class1staticvoidMain(string args)/ Specify SQL Server-specific connection stringOleDbConnection thisConnection =

45、new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:gzc#備課db1.mdb");thisConnection.Open();/ Create DataAdapter object for update and other operationsOleDbDataAdapter thisAdapter = new OleDbDataAdapter( "SELECT xh, xm FROM t1", thisConnection);/ Create CommandBuild

46、er object to build SQL commandsOleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter);/ Create DataSet to contain related data tables, rows, and columnsDataSet thisDataSet = new DataSet();/ Fill DataSet using query defined previously for DataAdapterthisAdapter.Fill(thisDataSet, "

47、;t1");/ Show data before changeConsole.WriteLine("name before change: 0", thisDataSet.Tables"t1".Rows0"xm");/ Change data in t1 table, row 0, xm columnthisDataSet.Tables"t1".Rows0"xm" = "ppp"/ Call Update command to mark change in tabl

48、ethisAdapter.Update(thisDataSet, "t1");Console.WriteLine("name after change: 0",thisDataSet.Tables"t1".Rows0"xm");3.使用ADO.NET添加表中數(shù)據(jù)行。(參見(jiàn)D:GZC#備課AddRow程序)using System;using System.Data;using System.Data.OleDb;namespace UpdatingDataclass Class1staticvoidMain(str

49、ing args)/ Specify SQL Server-specific connection stringOleDbConnection thisConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:gzc#備課db1.mdb");thisConnection.Open();/ Create DataAdapter object for update and other operationsOleDbDataAdapter thisAdapter = new O

50、leDbDataAdapter( "SELECT xh, xm,XB,NL FROM t1", thisConnection);/ Create CommandBuilder object to build SQL commandsOleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter);/ Create DataSet to contain related data tables, rows, and columnsDataSet thisDataSet = new DataSet();/

51、 Fill DataSet using query defined previously for DataAdapterthisAdapter.Fill(thisDataSet, "t1");/ Show data before changeConsole.WriteLine("#rows before change: 0", thisDataSet.Tables"t1".Rows.Count );DataRow thisRow=thisDataSet.Tables"t1".NewRow();thisRow&quo

52、t;xh"="888"thisRow"xm"="WQ"thisRow"xb"="m"thisRow"nl"=18;thisDataSet.Tables"t1".Rows.Add(thisRow);/ Call Update command to mark change in tableConsole.WriteLine("#rows after change: 0",thisDataSet.Tables"t1&quo

53、t;.Rows.Count );thisAdapter.Update(thisDataSet,"t1");4.使用ADO.NET刪除表中數(shù)據(jù)行。(參見(jiàn)D:GZC#備課DeleRow程序)using System;using System.Data;using System.Data.OleDb;namespace UpdatingDataclass Class1/ The main entry point for the application.staticvoidMain(string args)/ Specify SQL Server-specific connecti

54、on stringOleDbConnection thisConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:gzc#備課db1.mdb");thisConnection.Open();/ Create DataAdapter object for update and other operationsOleDbDataAdapter thisAdapter = new OleDbDataAdapter( "SELECT xh, xm,xb,nl FROM

55、 t1", thisConnection);/ Create CommandBuilder object to build SQL commandsOleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter);/ Create DataSet to contain related data tables, rows, and columnsDataSet thisDataSet = new DataSet();/ Fill DataSet using query defined previously fo

56、r DataAdapterthisAdapter.Fill(thisDataSet, "t1");/ Show data before changeConsole.WriteLine("#rows before change: 0", thisDataSet.Tables"t1".Rows.Count );DataColumn keys=new DataColumn1;keys0=thisDataSet.Tables"t1".Columns"xh"thisDataSet.Tables"t1".PrimaryKey=keys;DataRow findRow=thisDataSet

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論