14 ASP_SQL Server開(kāi)發(fā)與編程
單擊此處編輯母版標(biāo)題樣式,單擊此處編輯母版文本樣式,第二級(jí),第三級(jí),第四級(jí),第五級(jí),14 ASP/SQL Server開(kāi)發(fā)與編程,*,14.1,Web,環(huán)境操作,SQL Server,數(shù)據(jù)庫(kù),基于,Web,的計(jì)算技術(shù)采用,B/C/S(Browser/Client/Server),三層體系結(jié)構(gòu),1,14 ASP/SQL Server開(kāi)發(fā)與編程,14.2,ASP,技術(shù)概述,ASP,概述,ASP,是微軟公司隨其,IIS3.0,推出的一種主要用于,Web,服務(wù)器訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)的技術(shù),它提供使用,VBScript,或,Jscript(Microsoft,的,JavaScript,實(shí)現(xiàn))的服務(wù)器端腳本環(huán)境,可用來(lái)創(chuàng)建和運(yùn)行動(dòng)態(tài)、交互的,Web,服務(wù)器應(yīng)用程序。,2,14 ASP/SQL Server開(kāi)發(fā)與編程,14.2,ASP,技術(shù)概述,IIS/Web,服務(wù)器的配置,首先啟動(dòng),Internet,服務(wù)管理器:開(kāi)始,管理工具,Internet,服務(wù)管理器,,Internet,服務(wù)管理器界面如圖所示。,3,14 ASP/SQL Server開(kāi)發(fā)與編程,14.2,ASP,技術(shù)概述,在“管理,Web,站點(diǎn)”上單擊鼠標(biāo)右鍵,選擇“新建”,“站點(diǎn)”,4,14 ASP/SQL Server開(kāi)發(fā)與編程,14.2,ASP,技術(shù)概述,ASP,使用的腳本語(yǔ)言,ASP,中可嵌入的腳本語(yǔ)言可以是,VBScript,和,JScript,,,此外,還可通過(guò),plug-in,方式,使用由第三方提供的腳本語(yǔ)言,如,Perl,、Tel,等。,5,14 ASP/SQL Server開(kāi)發(fā)與編程,14.3,ASP,的內(nèi)建對(duì)象,Response,對(duì)象,6,14 ASP/SQL Server開(kāi)發(fā)與編程,14.3,ASP,的內(nèi)建對(duì)象,Request,對(duì)象,7,14 ASP/SQL Server開(kāi)發(fā)與編程,14.3,ASP,的內(nèi)建對(duì)象,Server,對(duì)象,8,14 ASP/SQL Server開(kāi)發(fā)與編程,14.3,ASP,的內(nèi)建對(duì)象,Application,對(duì)象,一個(gè),ASP,文件執(zhí)行結(jié)束后,其變量或?qū)ο蟮闹涤?Application,對(duì)象還能被保存下來(lái),Application(“,變量名”)=變量名 或,Set Application(“,對(duì)象名”)=對(duì)象名,恢復(fù)出變量或?qū)ο蟮闹担?變量名=,Application(“,變量名”)或,Set,對(duì)象名=,Application(“,對(duì)象名”),Application,對(duì)象有兩個(gè)方法:,Lock,和,UnLock,,,它們是為并發(fā)控制而設(shè)置的。,Application.Lock,Application(“Count”)=Application(“Count”)+1,Application.,UnLock,9,14 ASP/SQL Server開(kāi)發(fā)與編程,14.3,ASP,的內(nèi)建對(duì)象,Session,對(duì)象,Session,對(duì)象有3個(gè)屬性:,SessionID,、Timeout,和,Value,,其中,Timeout,用于設(shè)置,Session,對(duì)象的生存時(shí)間(缺省值為20分鐘,與,Application,對(duì)象相同)。,Session,對(duì)象也提供了兩個(gè)事件驅(qū)動(dòng)接口:,OnStart,和,OnEnd,事件,會(huì)話(huà)開(kāi)始時(shí),激活,OnStart,事件;會(huì)話(huà)結(jié)束或超時(shí),將激活,OnEnd,事件。,10,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.1,ADO,數(shù)據(jù)庫(kù)接口簡(jiǎn)介,ADO,訪(fǎng)問(wèn),SQL Server,的編程接口,11,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.1,ADO,數(shù)據(jù)庫(kù)接口簡(jiǎn)介,ADO,編程模型,Connection,對(duì)象,Command,對(duì)象,Recordset,對(duì)象,12,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.2,ASP,訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)簡(jiǎn)介,13,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,在,ASP,中引用,ADO,對(duì)象,要加上前綴“,ADODB”,14,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,Connection,對(duì)象,%,創(chuàng)建一個(gè),ADO Connection,對(duì)象,Set,cn,=Server.,CreateObject,(ADODB.Connection),指定,OLE DB,提供者,cn,.Provider=,sqloledb,賦予,OLE DB,連接串,ProvStr,=Server=USER1;Database=XSCJ;UID=lo;PWD=l123;,cn,.Open,ProvStr,Response.Write(&,cn,.Attributes),Response.Write(&,cn,.,CommandTimeout,),Response.Write(&,cn,.,ConnectionString,),Response.Write(&,cn,.,CursorLocation,),Response.Write(&,cn,.,DefaultDatabase,),Response.Write(&,cn,.,IsolationLevel,),Response.Write(&,cn,.Provider),Response.Write(&,cn,.Version),%,15,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,16,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,Recordset,對(duì)象,17,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,18,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,【例】建立與數(shù)據(jù)庫(kù),XSCJ,的連接,返回,XS,表的所有記錄,在瀏覽器,中以表格(,table),形式顯示。,%,Set,cn,=Server.,CreateObject,(ADODB.Connection),cn,.Provider=,sqloledb,ProvStr,=Server=USER1;Database=XSCJ;UID=lo;PWD=l123;,cn,.Open,ProvStr,Set,rs,=,cn,.Execute(select*from XS),Response.Write,Response.Write,FOR i=0 TO,rs,.Fields.Count-1 ,在,HTML,的,table,中輸出表頭,Response.WRITE&,rs,.Fields(i).Name&,NEXT,Response.Write,19,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,WHILE Not,rs,.EOF ,輸出表中各行,Response.Write,FOR i=0 TO,rs,.Fields.Count-1,Response.Write&,rs,.Fields(i).Value&,NEXT,Response.Write,rs,.,MoveNext,當(dāng)前記錄位置下移,WEND,Response.Write,rs,.close ,關(guān)閉結(jié)果集,cn,.close ,關(guān)閉連接,%,20,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,創(chuàng)建,Recordset,對(duì)象還有另一種方法:,Set,rs,=Server.,CreateObject,(“ADODB.,Recordset,”),rs,.Open T-SQL,語(yǔ)句,Connection,對(duì)象,Recordset,類(lèi)型,鎖定類(lèi)型,如上例中,我們可以把語(yǔ)句,Set,rs,=,cn,.Execute(select*from XS),替換為:,Set,rs,=Server.,CreateObject,(ADODB.,Recordset,),rs,.Open Select*from XS,cn,adOpenStatic,這種方法還有另一種寫(xiě)法:,Set,rs,=Server.,CreateObject,(ADODB.,Recordset,),rs,.,ActiveConnection,=,cn,rs,.Open Select*from XS,adOpenStatic,此時(shí)可省略連接參數(shù),21,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,Recordset,對(duì)象的,Open,方法有4個(gè)參數(shù):,SQL,語(yǔ)句,Connection,對(duì)象,Recordset,類(lèi)型,AdOpenForward,AdOpenStatic,AdOpenKeyset,AdOpenDynamic,鎖定類(lèi)型,adLockReadOnly,adLockPessimistic,adLockOptimistic,adLockBatchOptimistic,22,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,引用字段數(shù)據(jù)的方法,方法一,rs,.Fields(i).Value,方法二,rs,.Fields(,字段名).,Value,數(shù)據(jù)增加,rs,.,AddNew,假設(shè)已經(jīng)創(chuàng)建了記錄集,rs,,,用,AddNew,要求增加一條記錄,rs,(“,學(xué)號(hào)”)=“101116”逐字段設(shè)置值,rs,(“,姓名”)=“王小明,rs,(“,專(zhuān)業(yè)名”=“計(jì)算機(jī)”,rs,(“,性別”)=,true,rs,(“,出生時(shí)間”)=1978-1-31,rs,(“,總學(xué)分”)=40,rs,.Update ,寫(xiě)入數(shù)據(jù)庫(kù),數(shù)據(jù)修改,rs,(“,總學(xué)分”)=50,rs,.Update,23,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,數(shù)據(jù)刪除,rs,.Delete,數(shù)據(jù)分頁(yè)顯示,當(dāng)結(jié)果集中的記錄數(shù)比較多時(shí),采用分頁(yè)顯示的方式可使結(jié)果顯示結(jié)構(gòu)清晰,并能使用戶(hù)有選擇地查看信息。,結(jié)合,Session,對(duì)象,25,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,Command,對(duì)象,命令類(lèi)型的設(shè)置值可以是:,adCmdText,(,值為1),其中命令是一個(gè)查詢(xún)或數(shù)據(jù)定義語(yǔ)句;,adCmdTable,(,值為2),其中命令是一個(gè)表名;,adCmdStoredProc,(,值為4),其中命令是一個(gè)服務(wù)器端存儲(chǔ)過(guò)程的引用,adCmdUnknow,(,值為8),其中命令是一個(gè)未知命令。,例,Set,cmd,=Server.,CreateObject,(“ADODB.Command”),Set,cmd,.,ActiveConnection,=,cn,;,sql,=“select*from XS where,總學(xué)分40”,cmd,.,CommandText,=,sql,Set,rs,=,cmd,.Execute,26,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,27,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,執(zhí)行帶參數(shù)的存儲(chǔ)過(guò)程的方法是:,創(chuàng)建,Commmand,對(duì)象和各參數(shù)對(duì)象,分別設(shè)置,Command,對(duì)象的,ActiveConnection,、,CommandType,和,CommandText,對(duì)象或?qū)傩灾担?設(shè)置各參數(shù)對(duì)象的屬性值;,用,Command,對(duì)象的,Append,方法將各參數(shù)對(duì)象加入到其參數(shù)表中;,為,Command,對(duì)象的,Value,屬性賦值;,用,Command,對(duì)象的,Execute,方法執(zhí)行存儲(chǔ)過(guò)程。,28,14 ASP/SQL Server開(kāi)發(fā)與編程,14.4.3 在,ASP,中使用,ADO,對(duì)象,Parameter