ADO.NET數(shù)據(jù)庫訪問技術(shù)(C#)_第1頁
ADO.NET數(shù)據(jù)庫訪問技術(shù)(C#)_第2頁
ADO.NET數(shù)據(jù)庫訪問技術(shù)(C#)_第3頁
ADO.NET數(shù)據(jù)庫訪問技術(shù)(C#)_第4頁
ADO.NET數(shù)據(jù)庫訪問技術(shù)(C#)_第5頁
已閱讀5頁,還剩26頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

A

A網(wǎng)站開發(fā)本章重點(diǎn)理解ADO.NET對(duì)象模型定義。掌握SqlConnection對(duì)象與Command對(duì)象。掌握DataReader、DataSet、DataTable、DataAdapter及DataView對(duì)象。掌握連接與操作SqlServer數(shù)據(jù)庫的方法。掌握ADO.NET中使用XML文件的方法。ADO.NET基本概念A(yù)DO.NET是.NETFramework用于訪問數(shù)據(jù)的組件。ASP.NET通過ADO.NET操作數(shù)據(jù)庫。如圖:ADO.NET的一個(gè)優(yōu)點(diǎn)是可以離線操作數(shù)據(jù)庫,應(yīng)用程序只要在需要取得數(shù)據(jù)或更新數(shù)據(jù)的時(shí)候猜對(duì)數(shù)據(jù)源進(jìn)行聯(lián)機(jī),所以可以減少應(yīng)用程序?qū)Ψ?wù)器資源的占用,提高應(yīng)用程序的效率。ASP.NETADO.NET數(shù)據(jù)庫ADO.NET對(duì)象模型.NETFramework針對(duì)不同的數(shù)據(jù)庫,設(shè)計(jì)了下面四種數(shù)據(jù)提供程序:(1)SQLServer.NETFramework數(shù)據(jù)提供程序(2)OLEDB.NETFramework數(shù)據(jù)提供程序(3)ODBC.NETFramework數(shù)據(jù)提供程序(4)Oracle.NETFramework數(shù)據(jù)提供程序ADO.NET對(duì)象模型SQLConnection

建立與特定數(shù)據(jù)源的連接屬性說明DataSource設(shè)置要連接的SQLSERVER服務(wù)器名稱或IPServerDatabase要連接的數(shù)據(jù)庫InitialcatalogIntegratedSecrity指定是否使用信任連接Trusted_ConnectionUserID登陸數(shù)據(jù)庫的賬號(hào)UidPassword登陸數(shù)據(jù)庫的密碼pwdConnectionTimeout連接超時(shí)SQLSERVER數(shù)據(jù)庫的兩種連接模式1、混合模式連接使用此種連接模式必須輸入登錄名和登陸口令。例如:stringmyconnection="server=(local);database=news;uid=sa;pwd=sa";SqlConnectionconn=newSqlConnection(myconnection);2、windows模式連接以windows連接模式必須將trusted_connection=truestringmyconnection="server=(local);database=news;trusted_connection=true";SqlConnectionconn=newSqlConnection(myconnection);Command對(duì)數(shù)據(jù)源執(zhí)行命令屬性說明CommandTypeCommandType屬性可以用來指定CommandText屬性中的內(nèi)容是SQL語句、數(shù)據(jù)表名稱還是存儲(chǔ)過程。CommandType.TableDirect數(shù)據(jù)表名稱CommandType.TextSQL語句CommandType.StoredProcedure存儲(chǔ)過程名稱默認(rèn)為:CommandType.TextCommandText由CommandText屬性設(shè)置,表示內(nèi)容是SQL語句、數(shù)據(jù)表名稱或存儲(chǔ)過程CommandTimeout指令超時(shí)時(shí)間,默認(rèn)為30sCommand對(duì)象方法方法描述ExecuteReader執(zhí)行CommandText屬性所規(guī)定的操作,并創(chuàng)建DataReader對(duì)象ExecuteNonQuery執(zhí)行CommandText屬性所規(guī)定的操作,一般為update、insert、delete及其他沒有返回值的SQL命令,返回受影響的行數(shù)。ExecuteScalar執(zhí)行CommandText屬性所規(guī)定的操作,返回執(zhí)行結(jié)果中的首行首列的值。如結(jié)果集多于一行一列,它將忽略其余部分。一般用于執(zhí)行max、min、Count、sun之類的結(jié)果。使用方法stringmyconnection="server=.;database=news;uid=sa;pwd=sa";SqlConnectionconn=newSqlConnection(myconnection);SqlCommandcmd=newSqlCommand();cmd.CommandType=CommandType.Text;cmd.CommandText="select新聞標(biāo)題,發(fā)布日期fromnews";cmd.Connection=conn;conn.Open();綜合實(shí)例(一)查詢數(shù)據(jù)protectedvoidPage_Load(objectsender,EventArgse)

{stringconnection="server=.;database=news;uid=sa;pwd=123456";SqlConnectionconn=newSqlConnection(connection);stringcommand="select*fromnews";SqlCommandcmd=newSqlCommand(command,conn);conn.Open();

SqlDataReaderdr=cmd.ExecuteReader();this.GridView1.DataSource=dr;

this.GridView1.DataBind();dr.Close();conn.Close();

}DataReader從數(shù)據(jù)源中讀取只進(jìn)且只讀的數(shù)據(jù)流.當(dāng)我們只需要按順序讀取數(shù)據(jù)而不需要其他操作時(shí),可以使用DataReader對(duì)象。DataReader類是抽象類,因此不能直接實(shí)例化,而是通過執(zhí)行Command對(duì)象的ExecuteReader方法返回DataReader實(shí)例。DataBindDataBind方法表示綁定數(shù)據(jù)綜合實(shí)例(2)增加數(shù)據(jù)stringconnection="server=.;database=news;uid=sa;pwd=123456";SqlConnectionconn=newSqlConnection(connection);stringcommand="insertintonews(新聞標(biāo)題,新聞內(nèi)容,作者,新聞分類)values('ADO.NET基礎(chǔ)','ADO.NET基礎(chǔ)ADO.NET基礎(chǔ)ADO.NET基礎(chǔ)ADO.NET基礎(chǔ)ADO.NET基礎(chǔ)ADO.NET基礎(chǔ)','WEIWEI','IT技術(shù)')";SqlCommandcmd=newSqlCommand(command,conn);conn.Open();cmd.ExecuteNonQuery();conn.Close();Response.Write("<scriptlanguage=javascript>alert('數(shù)據(jù)插入成功!')</script>");ExecuteNonQuery該方法執(zhí)行Command屬性中所規(guī)定增刪改的SQL語句。綜合實(shí)例(3)修改數(shù)據(jù)protectedvoidPage_Load(objectsender,EventArgse){stringconnection="server=.;database=news;uid=sa;pwd=123456";SqlConnectionconn=newSqlConnection(connection);stringcommand="updatenewsset新聞標(biāo)題='實(shí)例教程'where新聞標(biāo)題='基礎(chǔ)'";SqlCommandcmd=newSqlCommand(command,conn);conn.Open();cmd.ExecuteNonQuery();conn.Close();Response.Write("<scriptlanguage=javascript>alert('數(shù)據(jù)修改成功!')</script>");}綜合實(shí)例(4)刪除數(shù)據(jù)protectedvoidPage_Load(objectsender,EventArgse)

{stringconnection="server=.;database=news;uid=sa;pwd=123456";SqlConnectionconn=newSqlConnection(connection);stringcommand="deletefromnewswhere新聞標(biāo)題='實(shí)例教程'";SqlCommandcmd=newSqlCommand(command,conn);conn.Open();cmd.ExecuteNonQuery();conn.Close();Response.Write("<scriptlanguage=javascript>alert('數(shù)據(jù)刪除成功!')</script>");

}ExecuteScalar方法ExecuteScalar方法返回執(zhí)行結(jié)果中首行首列的值,該方法只能執(zhí)行Select語句,一般用于取得最大值(Max)、最小值(Min)、平均值(Avg)、記錄數(shù)(Conut)綜合實(shí)例(4)ExecuteScalar方法protectedvoidPage_Load(objectsender,EventArgse)

{stringconnection="server=.;database=news;uid=sa;pwd=123456";SqlConnectionconn=newSqlConnection(connection);conn.Open();SqlCommandcmd=newSqlCommand("selectcount(*)fromnews",conn);stringcount=cmd.ExecuteScalar().ToString();this.Label1.Text="<fontcolor=red>"+count+"</font>";

}執(zhí)行帶參數(shù)的commandprotectedvoidButton1_Click(objectsender,EventArgse)

{stringconnection="server=.;database=news;uid=sa;pwd=123456;";SqlConnectionconn=newSqlConnection(connection);SqlCommandcmd=newSqlCommand("select*fromnewswhere新聞標(biāo)題=@ttitle",conn);cmd.Parameters.Add("@ttitle",SqlDbType.VarChar).Value=this.TextBox1.Text;conn.Open();SqlDataReadersdr=cmd.ExecuteReader();this.GridView1.DataSource=sdr;GridView1.DataBind();sdr.Close();conn.Close();

}DataReader對(duì)象當(dāng)我們需要按順序讀取數(shù)據(jù)而不需要其他操作時(shí),可以使用DataReader對(duì)象。DataReader對(duì)象一次讀取一條記錄,而且這些數(shù)據(jù)是只讀的,并不允許做其他操作,所以使用DataReader不但節(jié)省資源而且效率很高。DataReader類是抽象的類,因此不能直接實(shí)例化,而是通過執(zhí)行Command對(duì)象的ExecuteReader方法返回DataReader實(shí)例。DataReader綜合實(shí)例protectedvoidPage_Load(objectsender,EventArgse)

{stringconnection="server=.;database=news;uid=sa;pwd=123456";SqlConnectionconn=newSqlConnection(connection);stringcommand="selectusername,passwordfromusers";SqlCommandcmd=newSqlCommand(command,conn);conn.Open();SqlDataReadersdr=cmd.ExecuteReader();while(sdr.Read())//read表示讓DataReader對(duì)象讀取下一條記錄,如果獨(dú)到數(shù)據(jù)則返回true,若沒有讀到數(shù)據(jù)就返回false

{Response.Write(sdr["username"]+"  ");Response.Write(sdr["password"]+"<br>");

}sdr.Close();conn.Close();

}DataSet對(duì)象DataSet對(duì)象可以視為一個(gè)內(nèi)存數(shù)據(jù)庫,由許多表(DataTable)數(shù)據(jù)表關(guān)系(Relation)、約束(Constraint)、記錄(DataRow)以及字段(DataColumn)對(duì)象的集合組成的。DataSet對(duì)象的結(jié)構(gòu)也與數(shù)據(jù)庫類似,DataSet對(duì)象由一個(gè)或多個(gè)DataTable組成,DataTable相當(dāng)于數(shù)據(jù)庫中的表;有列DataColumn與行DataRow,分別對(duì)應(yīng)數(shù)據(jù)庫的字段與記錄。DataSet對(duì)象DataSet對(duì)象一個(gè)重要的特性是離線操作,即從數(shù)據(jù)庫中取回?cái)?shù)據(jù),存到DataSet對(duì)象中后,程序可以馬上斷開與數(shù)據(jù)庫的連接,用戶可以對(duì)內(nèi)存中DataSet對(duì)象中的數(shù)據(jù)進(jìn)行增刪改,二檔要吧改動(dòng)反映到數(shù)據(jù)庫時(shí)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論