網絡課程設計與開發(fā)12章_第1頁
網絡課程設計與開發(fā)12章_第2頁
網絡課程設計與開發(fā)12章_第3頁
網絡課程設計與開發(fā)12章_第4頁
網絡課程設計與開發(fā)12章_第5頁
已閱讀5頁,還剩26頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第12章ADO.NET與數(shù)據(jù)訪問12.1ADO.NET數(shù)據(jù)訪問框架ADO.NET代表.NET中許多與數(shù)據(jù)訪問相關的類及技術。通過建立ADO.NET類的對象,可以進行數(shù)據(jù)訪問的操作。ASP.NETWebFormADO.NET數(shù)據(jù)提供者數(shù)據(jù)庫Database網頁程序.NETDataProviderDatabase12.1.1.NET數(shù)據(jù)提供者.NET

Framework提供了4組數(shù)據(jù)提供者以供選擇,以便針對不同的數(shù)據(jù)庫提供最佳的訪問效能。System.Data.OleDb:支持OLEDB界面的數(shù)據(jù)庫,AccessSystem.Data.SqlClient:SQL

Server

7.0/2000/2005System.Data.Odbc:支持ODBC界面的數(shù)據(jù)庫System.Data.OracleClient:Oracle8.1.7之后的版本12.1.2ADO.NET數(shù)據(jù)訪問模式System.Dta.SqlDataClient數(shù)據(jù)提供者包括:SqlConnection、SqlDataReader、SqlDataAdapter、DataSet等5類,通過建立它們的對象,可以對數(shù)據(jù)庫進行查詢、新增、修改及刪除的處理。ADO.NET數(shù)據(jù)訪問有兩種模式:連線模式、離線模式。連線模式:讀取數(shù)據(jù)時必須與數(shù)據(jù)庫保持連接。1、以SqlConnection與SQLServer2005Express數(shù)據(jù)庫建立連接2、以SqlCommand對數(shù)據(jù)庫執(zhí)行SQL語句3、以DataReader逐條讀取數(shù)據(jù)離線模式:一旦將數(shù)據(jù)整批取回,便可切斷連接,后續(xù)的數(shù)據(jù)讀取操作不再依賴數(shù)據(jù)連接。1、以SqlConnection與SQLServer2005Express數(shù)據(jù)庫建立連接2、以SqlDataAdapter執(zhí)行SQL語句3、取回整批數(shù)據(jù)放入DataSet中的DataTable里4、數(shù)據(jù)整批取回便可切斷連接5、從DataTable中讀取數(shù)據(jù)12.2以SqlConnection建立數(shù)據(jù)庫連接在打開數(shù)據(jù)庫之前,必須先設置連接字符串,然后調用Open方法打開它,可對數(shù)據(jù)庫進行訪問,最后調用Close方法關閉它。12.2.1連接字符串(ConnectionString)DataSource=.\SQLEXPRESS;

AttachDBFilename=|DataDirectory|NORTHWND.MDF;IntegratedSecurity=True;UserInstance=True;12.2.2建立SqlConnection對象<%@ImportNamespace="System.Data.SqlClient"%>SqlConnectioncn=newSqlConnection();或者varcn=

newSqlConnection();或者SqlConnectioncn;cn=newSqlConnection();12.2.3打開及關閉連接protectedvoidPage_Load(objectsender,EventArgse)

{SqlConnectioncn=newSqlConnection();cn.ConnectionString=@"DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|NORTHWND.MDF;IntegratedSecurity=True;UserInstance=True";cn.Open();Response.Write("SQL連接打開.<p/>");cn.Close();Response.Write("SQL連接關閉.<p/>");}13.2.4判斷連接狀態(tài)若要得知目前的連接狀態(tài),可用State屬性加以判斷。State屬性是一個只讀屬性,有6種狀態(tài)值:Broken:連接中斷Closed:連接已關閉Connecting:連接中Executing:執(zhí)行指令中Fetching:讀取數(shù)據(jù)中Open:連接已打開protectedvoidPage_Load(objectsender,EventArgse)

{

SqlConnectioncn=newSqlConnection();cn.ConnectionString=……….cn.Open();if(cn.State==System.Data.ConnectionState.Open)

{

Response.Write("SQL連接打開.<p/>");cn.Close();}

if(cn.State==System.Data.ConnectionState.Closed)

{Response.Write("SQL連接關閉.<p/>");}

}12.3以SqlCommand執(zhí)行SQL語句連接建立后才可以通過對象修改、刪除、查詢數(shù)據(jù),SqlCommand對象要借助SqlConnection對象才能對數(shù)據(jù)庫執(zhí)行指令。12.3.1建立SqlCommand對象var

cmd=newSqlCommand();SqlCommand

cmd=newSqlCommand();或SqlCommand

cmd;

cmd=newSqlCommand();12.3.2SqlCommand常用屬性SqlCommand對象主要是對數(shù)據(jù)庫執(zhí)行SQL語句,要先設置好它的Connection及CommandText屬性。Connection屬性用來指定連接對象。protectedvoidPage_Load(objectsender,EventArgse)

{

varcn=newSqlConnection();cn.ConnectionString=……….;varcmd=newSqlCommand();cmd.Connection=cn;cmd.CommandText=“INSERTCustomers(CustomerID,CompanyName)VALUES(‘AAASP’,‘ASPNETCompany’)”;

}12.3.3執(zhí)行SQL語句根據(jù)SQL語句的類型及返回結果,SqlCommand提供幾個不同的執(zhí)行方法。SqlCommand執(zhí)行方法適用情況返回值ExecuteNonQueryINSERT、UPDATA、DELETE整數(shù)。代表影響的數(shù)據(jù)條數(shù)ExecuteReaderSELECTE語句SqlDataReader對象??勺x出所有查詢結果ExecuteScalarSELECTE語句,且只要第一條數(shù)據(jù)的第一個數(shù)據(jù)行Object對象。代表第一條數(shù)據(jù)的第一個數(shù)據(jù)行的內容INSERT語句protectedvoidPage_Load(objectsender,EventArgse)

{

varcn=newSqlConnection();cn.ConnectionString=………;varcmd=newSqlCommand();cmd.Connection=cn;cmd.CommandText="INSERTCustomers(CustomerID,CompanyName)VALUES('AAASP','ASPNETCompany')";cn.Open();varrecords=cmd.ExecuteNonQuery();cn.Close();Response.Write("添加了"+records.ToString()+"條數(shù)據(jù)");}12.4異常的捕捉與處理12.4.1打開數(shù)據(jù)庫連接可能產生的異常protectedvoidPage_Load(objectsender,EventArgse)

{

SqlConnectioncn=newSqlConnection();cn.ConnectionString=………;

try

{cn.Open();Response.Write("SQL連接打開.<p/>");}

catch(Exceptionex){Response.Write(ex.Message);}

finally

{if(cn.State==System.Data.ConnectionState.Open)

{cn.Close();Response.Write("SQL連接關閉.<p/>");}

}}12.4.2執(zhí)行SQL語句時發(fā)生的異常原因:違反數(shù)據(jù)表的條件約束;輸入了錯誤的數(shù)據(jù)表、列;SQL語法錯誤。1、新增或修改數(shù)據(jù)時,若發(fā)現(xiàn)與既有數(shù)據(jù)的主鍵值重復,或不允許其值為Null的列沒有設置值,此時會因為違反約束條件而發(fā)生異常。2、在SQL語句中輸入了錯誤的表、列名稱而引起異常。3、SQL語法錯誤可以使用SqlException異常捕捉程序產生的異常protectedvoidPage_Load(objectsender,EventArgse)

{…cmd.CommandText="INSERTCustomers(CustomerID)VALUES('AAASP')";try{cn.Open();varrecords=cmd.ExecuteNonQuery();cn.Close();Response.Write("添加了"+records.ToString()+"條數(shù)據(jù)");}catch(SqlExceptionex)

{Response.Write(ex.Message);}finally{if(cn.State==System.Data.ConnectionState.Open)

{cn.Close();}

}}12.4.3其他異常若要報錯畫面不會直接呈現(xiàn)在用戶面前,可以在try...catch中捕捉一般性的異常。catch(SqlExceptionSqlex){Response.Write("數(shù)據(jù)庫異常:"+Sqlex.Message);}catch(Exceptionex){Response.Write("其他異常:"+ex.Message);}12.5以SqlDataReader讀取數(shù)據(jù)SqlDataReader用來在采用連線模式時讀取數(shù)據(jù)。若要判斷是否有數(shù)據(jù)可讀,可以先用SqlDataReader的HasRows屬性判斷是否有數(shù)據(jù)可以回傳,若有則回傳True,再調用Read方法可以往下讀取一條數(shù)據(jù),若有則回傳TtectedvoidPage_Load(objectsender,EventArgse)

{

varcn=newSqlConnection();cn.ConnectionString=………….;varcmd=newSqlCommand();cmd.Connection=cn;cmd.CommandText="Select*fromCustomers";cn.Open();vardr=cmd.ExecuteReader();if(dr.HasRows)

{while(dr.Read())

{Response.Write("客戶編號:"+dr["CustomerID"]);}}dr.Close();cn.Close();}12.6SqlDataAdapter與DataSet離線模式:SqlDataAdapter取回的數(shù)據(jù)放入DataSet的表中。數(shù)據(jù)庫SqlConnectionSqlDataAdapterDataSetDataTableWebFormprotectedvoidPage_Load(objectsender,EventArgse)

{

varcn=newSqlConnection();cn.ConnectionString=………;varda=newSqlDataAdapter("select*fromCustomers",cn);vards=newDataSet();//調用Fill方法執(zhí)行SQL語句,并將返回數(shù)據(jù)填入ds的

Customers表中intcounter=da.Fill(ds,"Customers");Response.Write("取回:"+counter.ToString()+"條數(shù)據(jù)");

}讀取DataSet中的表內容要訪問表中的內容,可以適用Row集合。DataTableRowsDataSetprotectedvoidPage_Load(objectsender,EventArgse)

{

varcn=newSqlConnection();cn.ConnectionString=……….;varda=newSqlDataAdapter("select*fromCustomers",cn);vards=newDataSet();intcounter=da.Fill(ds,"Customers");Response.Write("取回:"+counter.ToString()+"條數(shù)據(jù)");

foreach(DataRowdrinds.Tables["Customers"].Rows)

{

Response.Write(dr["CustomerID"]+"<br/>");}

}12.7設置SQL語句中的參數(shù)12.7.1在SQLCommand中加入@開頭的參數(shù)protectedvoidButtonSearch_Click(objectsender,EventArgse){

…cmd.CommandText="SelectProductNamefromProductswhereCategoryID=@CategoryID";cmd.Parameters.AddWithValue("@CategoryID",TextBox1.Text);cn.Open();

vardr=cmd.ExecuteReader();BulletedListProduct.DataSource=dr;BulletedListProduct.DataTextField="ProductName";BulletedListProduct.DataBind();cn.Close();

}12.7.2SqlDataAdapter與參數(shù)protectedvoidButtonSearch_Click(objectsender,EventArgse){…….cmd.CommandText="SelectProductNamefromProductswhereCategoryID=@CategoryID";cmd.Parameters.AddWithValue("@CategoryID",TextBoxCategory.Text);varda=newSqlDataAdapter();da.SelectCommand=cmd;vards=newDataSet();da.Fill(ds,"Products");…}

12.8使用服務器控件展示數(shù)據(jù)服務器控件有些可以與數(shù)據(jù)“綁定”在一起,只要將服務器控件的數(shù)據(jù)來源設置到某個SqlDataReader對象,就可以通過其讀取數(shù)據(jù)。13.8.1使用GridView展示數(shù)據(jù)protectedvoidPage_Load(objectsender,EventArgse)

{

varcn=newSqlConnection();cn.ConnectionString=………..;varcmd=newSqlCommand();cmd.Connection=cn;cmd.CommandText="SelectEmployeeID,Firstname,LastNamefromEmployees";cn.Open();vardr=cmd.ExecuteReader();

GridView1.DataSource

溫馨提示

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

評論

0/150

提交評論