Каталог: UDS -> Ebook -> CD1
CD1 -> H×nh 14. Phn tÝch modulus cña rabin víi mét ch­¬ng tr×nh con gi¶i m· cho tr­íc
CD1 -> ®¹i häc quèc gia hµ néi Tr­êng §¹i Häc Khoa Häc Tù Nhiªn Khoa c ng nghÖ Th ng tin
CD1 -> Diễn hoạt nhân vật với Progressive Morphing i-khái niệm Progressive Morphing
CD1 -> Ch­¬ng 9 T×m ra c¸c ®Þa chØ th­ ®iÖn tö Trong ch­¬ng nµy
CD1 -> Microsoft Frontpage là một phần mềm thiết kế web chuyên nghiệp của hãng phần mềm Microsoft
CD1 -> TÀi liệu tham khảo về google adsense
CD1 -> MỘt số kinh nghiệm với windows xp (phần I) Chạy chương trình không cần cài lại
CD1 -> Adobe photoshop cs 0
CD1 -> Tài liệu tham khảo của trường đại học wakayama Nhật

tải về 109.96 Kb.

Chia sẻ với bạn bè của bạn:

1. §Æc ®iÓm: Tªn file: asp



tải về 109.96 Kb.
Chuyển đổi dữ liệu30.08.2016
Kích109.96 Kb.
#28480

Next


Reponse.Write “”

End sub


Sub printAllRecordsInRS()

Do until rs.EOF

For each fld in rs.Fields

Reponse.Write “



Rs.MoveNext

Loop


End Sub

Sub CleanUp()

Set rs = nothing

Cnn1.Close

Set Cnn1 = nothing

End sub %>


Tãm l¹i c¸c b­íc thùc hiÖn:

  • T¹o mét kÕ nèi Connection, active nã.

  • T¹o ®èi t­îng kiÓu Command, g¸n nã víi mét Connection ®· kÝch ho¹t (thuéc tÝnh ActiveConnection), CommandText: tªn thñ tôc;

  • T¹o ta ®èi t­îng tham sè, b»ng ph­¬ng thøc CreateParameter ®Ó truyÒn tham sè cho thñ tôc.

  • Thùc thi vµ sö dông kÕt qu¶.


15. C¸c hµm VBScript (1.0)




1. §Æc ®iÓm:

Tªn file: .asp



  • Ph¶i cµi thªm Microsoft Active Server Page ®Ó bæ trî cho Web Server, chuyªn xö lý c¸c script viÕt trong file .asp. Cho phÐp viÕt øng dông theo kiÓu Server Side => c¸c trang web sÏ sÏ ®­îc xö lý t¹i Server tr­íc khi göi xuèng Client.

  • C¸c m· lÖnh ®­îc chÌn vµo trong kh¾p trang web vµ ®­îc xö lý tuÇn tù. M· lÖnh lµ c¸c Script, hiÖn ASP cho phÐp dïng 2 srcript lµ VBScript hoÆc JavaScript. Lùa chän Script ®­îc ®Æt ngay t¹i dßng ®Çu file:

<%@LANGUAGE = “VBSCRIPT”%>

<%@LANGUAGE = “JSCRIPT”%>

NgÇm ®Þnh lµ VBScript.



  • C¸c ®o¹n m· kh«ng ph©n biÖt dµi ng¾n ®­îc ®Æt trong dÊu hiÖu: <% %>

  • C¸c biÕn kh«ng cÇn khai b¸o mµ tù khi g¸n gi¸ trÞ cho nã sÏ x¸c ®Þnh biÕn.

  • §Ó in ra c¸c biÕn trong trang web sö dông nh­ m· sau:

<% =s %>

  • §Ó lµm viÖc víi hÖ thèng file, thao t¸c víi Database... => cÇn ph¶i sö dông c¸c ®èi t­îng kiÓu Active X cña ASP hay cµi ®Æt thªm vµo.

  • §Ó lÊy c¸c gi¸ trÞ truyÒn tõ client lªn dïng ph­¬ng thøc request.

Ex: s = request(“Company”)

=> LÊy gi¸ trÞ cña biÕn Company.



2. Sö dông c¸c ®èi t­îng:
ASP cho phÐp sö dông c¸c ®èi t­îng kiÓu Active X. §Ó khai b¸o ta dïng ph­¬ng thøc CreateObject cña ®èi t­îng Server cã s½n.

Ex: Set f = Server.CreateObject(“Scripting.FileSystemObject”);



Tæng quan: Set param = Server.CreateObject(“PacketName.ClassName”)
ProgID

Sau ®ã cã thÓ sö dông c¸c hµm cña ®èi t­îng ®ã.

§Ó gi¶i phãng dïng Set f = nothing

3. Truy nhËp hÖ thèng file:

<%

Dim objFile

Dim objStream

Set objFile = Server.CreateObject(“Scripting.FileSystemObject”);

Set objStream = objFile.OpenTextFile(Server.Mapath(“/ASP”) &_

“\Chapter05\Listing 5-9\tips.txt”)

Randomize Timer

IntLine = Int(Rnd*19)

For i=0 to intLine

ObjStream.SkipLine

Next

StrTxt = objStream.ReadLine



ObjStream.Close

Set objFile= Nothing

Set objStream= Nothing

%>

=> §Ó t¹o file míi dïng ph­¬ng thøc CrateTextFile cña objFile.



§Ó ghi mét dßng v¨n b¶n dïng objStream.WriteLine s.

4. Truy nhËp c¬ së d÷ liÖu:
<%

Dim objConnection

Set objConnection = Server.CreateObject(“ADODB.Connection”)

ObjConnection.Open “Publication”, ”sa”, “”

Dim objRecordSet

Set objRecordSet = Server.CreateObject(“ADODB.RecordSet”)

ObjRecordSet.Open “Select pub_name From Publishers”, objConnection

%>


  • §èi t­îng kiÓu Connection: t¹o kÕt nèi ADO tíi Database.




  • §èi t­îng kiÓu RecordSet: cho phÐp thao t¸c trªn table (select,update ...) cña Connection ®· thiÕt lËp.

<% Do While Not objRecordSet.EOF %>

<% = objRecordSet(“pub_name”)%>

<% objRecordSet.MoveNext

Loop


ObjRecordSet.close

ObjConnection.close

Set objRecordSet = nothing

Set objConnection = nothing %>



  • Chó ý: khi thªm, cËp nhËt d÷ liÖu ngoµi viÖc dïng SQL, cã thÓ thªm theo c¸ch :

With objRecordSet do

.AddNew

.Field(”Authors”) = “Homes”

. Field(”Title”) = “Iliad”

.Update

End With



  • Cã thÓ kÕt nèi trùc tiÕp kh«ng qua DSN cña ODBC nh­ sau :


<%

Set objConnection = Server.CreateObject(“ADODB.Connection”)

StrCnn = “driver = {SQL Server}; Server = smas; uid = sa; pwd= ; database= pubs”

objConnection.Open strCnn

%>
5. Ph©n trang RecordSet

Khi select => cho kÕt qu¶ 1 b¶ng gåm nhiÒu hµng. Tuy nhiªn mçi lÇn chØ muèn sö dông mét vµi hµng, vÝ dô: b¶ng 50 hµng, mçi lÇn hiÓn thÞ 10 hµng => sö dông kü thuËt paging. ADO cung cÊp c¸c thuéc tÝnh cña Recordset nh­: PageSize, PageCount, AbsolutePage.

PageCount: sè trang, PageSize: sè hµng.

ë ®©y ta dïng biÕn session CurrentPage ®Ó ghi nhËn trang hiÖn hµnh.



<% Select Case Request.QueryString(“Direction”)

Case “” ‘Null

Session(“CurrentPage”) = 1

Case “Next”

Session(“CurrentPage”) = Session(“CurrentPage”) + 1 Case “Previous”

Session(“CurrentPage”) = Session(“CurrentPage”) - 1

End Select %>


  • Chó ý: Request.QueryString(“Direction”) <=> QueryString(“Direction”)

TiÕn hµnh kÕt nèi truy vÊn d÷ liÖu.

<%

const adOpenKeySet = 1



dim objConnection kh«ng cÇn khai b¸o còng ®­îc

dim objRexordset

Set objConnection = Server.CreateObject(“ADODB.Connection”)

Set ObjConnection.Open “Biblio”, “”, “”

Dim strSQL

StrSQL = “Select * from Authors”

Set objRecordSet = Server.CreateObject(“ADODB.RecordSet”)

ObjRecordSet.PageSize = 10

ObjRecordSet.Open strSQL, objConnection, adOpenKeyset

ObjRecordSet.AbsolutePage = CLng(Session(“CurrentPage”))

%>

Page = <%=Session(“CurrentPage”) %> of <%=ObjRecordSet.PageCount>



<% Dim i

For i = 1 to ObjRecordSet.PageSize %>



<% = ObjRecordSet(“Authors”) %>

...


<% ObjRecordSet.MoveNext

Next %>


<% if CLng(Session(“CurrentPage”)) < ObjRecordSet.PageCount Then %>

Next Page




<%Enf if%>

<% If CLng(Session(“CurrentPage”)) > 1 then %>

Previous Page




<%Enf if%>

<% ObjRecordSet.close

ObjConnection.close

Set objRecordSet = nothing

Set objConnection = nothing %>


6. §èi t­îng Application:

§èi t­îng nµy cho phÐp t¹o c¸c biÕn øng dông. §ã lµ c¸c biÕn cho tÊt c¶ c¸c user cña mét øng dông web. TÊt c¶ c¸c user mµ yªu cÇu c¸c trang web tõ mét th­ môc web cã thÓ chia xÎ víi nhau c¸c biÕn ®Þnh nghÜa trong c¸c trang ®ã.



<% Application.Lock

Application(“Company”) = “VASC”

Application.Unlock %>

ë ®©y ®Þnh nghÜa 1 biÕn lµ Company, cã gi¸ trÞ lµ VASC. V× r»ng biÕn cã thÓ bÞ tranh chÊp nªn ph¶i ®Æt gi÷a Lock vµ Unlock.



<% Application.Lock

Application(“Time”) = Now

Application.Unlock %>
7. §èi t­îng Session:

Cho phÐp t¹o ra c¸c biÕn øng víi tõng ng­êi dïng. Ch¼ng h¹n biÕn cã tªn lµ UserName, øng víi mçi user cã mét gi¸ trÞ cô thÓ kh¸c nhau, ph©n biÖt. Nh­ng khi sö dông th× tæng qu¸t cho c¸c user. Thùc ra mçi biÕn sÏ ®­îc server ph¸t sinh vµ g¸n cho nã mét gi¸ trÞ ®Þnh danh GUID (Globally Unique Identifier, 128 bit) vµ göi tíi Browser. Browser sÏ l­u tr÷ GUID ®ã vµ sö dông ®Ó yªu cÇu d÷ liÖu tõ biÕn cã GUID t­¬ng øng trªn Server.



<% Session(“Company”) = “NewTech”

Session(“Email”) = “leha@hotmail.com” %>

My Company : <% Session(“Company”) %>

Email : <% Session(“Email”) %>

C¸c biÕn Session nµy sÏ bÞ huû bá sau mét kho¶ng thêi gian (kho¶ng 20’) ng­êi dïng th«i truy xuÊt c¸c trang web trong th­ môc web.


  • Chó ý lµ trong file global.asa => cho ph¸p ®Æt c¸c biÕn, code vµo c¸c thñ tôc Session_OuStart vµ Session OuEnd. VÝ dô:


8. §èi t­îng Request:

§Ó göi d÷ liÖu lªn Server, ta sö dông Form trong trang Web. Tªn cña c¸c ®åi t­îng web nh­ Text, Button n»m trong form sÏ ®­îc dïng nh­ tªn biÕn khi ph©n tÝch, lÊy d÷ liÖu trªn Server. Trong form cã button kiÓu Submit ®Ó ng­êi dïng ®Èy d÷ liÖu lªn Server.

Form cã hai thuéc tÝnh lµ Action vµ Method. Action quy ®Þnh file (.asp) mµ sÏ ®ãn nhËn vµ xö lý d÷ liÖu ®Èy lªn. method quy ®Þnh c¸ch thøc göi d÷ liÖu; nÕu lµ POST th× tÊt c¶ d÷ liÖu trong form sÏ ®ãng gãi vµ ®Èy lªn Server; NÕu lµ GET th× d÷ liÖu ®­îc göi ®i nh­ mét phÇn cña URL, th­êng lµ sau dÊu “?”.



  • KiÓu göi d÷ liÖu GET còng cã thÓ lîi dông ®Ó göi ®i d­íi d¹ng mét link.

< A href =

“ http://www.vnn.vn/data.asp?name=ha&company=VASC” >Test



  • Göi theo GET th× ®é lín bÞ h¹n chÕ vµ kh«ng che dÊu d÷ liÖu.

§Ó lÊy d÷ liÖu ta dïng ph­¬ng thøc cña ®èi t­îng Request.

S = Request.Form(“Name”)

<=> S = Request(“Name”)

Trong tr­êng hîp d÷ liÖu ®­îc ®­a lªn tõ 1 hyperlink, cã thÓ dïng:

S = Request.QueryString(“Name”)

<=> S = Request(“Name”)


  • §èi t­îng Request còng cho phÐp lÊy gi¸ trÞ c¸c biÕn m«i tr­êng nh­ LOGON_USER, HTTP_USER_AGENT, vÝ dô:

Request.ServerVariables(“LOGON_USER”)

Request.ServerVariables(“HTTP_USER_AGENT”)


9. §èi t­îng Reponse:

§èi t­îng nµy qu¶n lý tÊt c¶ d÷ liÖu, néi dung sÏ ®­îc gëi cho Browser tõ ASP.



<% = Company%>

<=> <% Reponse.Write Company %>

Reponse cung cÊp 1 thuéc tÝnh lµ Expires, quy ®Þnh thêi gian (phót) mµ trang Web chøa trong cache cña IE. VËy, nÕu ®Æt dßng <% Response.Epires = 0 %> th× trang Web sÏ kh«ng chøa trong cache cña IE.


10. §èi t­îng Server:

§èi t­îng Server cung cÊp hµm CreateObject, ®Ó t¹o ra c¸c ®èi t­îng (instance) cña Active X. Tham sè truyÒn vµo lµ ProgID (Program Identifier) cña Component cÇn t¹o instance.

Set MyObject = Server.CreateObject(“Mail.Connector”)

§o¹n m· sau in ra c¸c biÕn Server :


< % For each name in Request.ServerVariables %>

<% = name %> <% = Request. ServerVariables(name) %>

<% Next %>
11. File Global.asa:

øng dông web = tËp c¸c trang Web, asp trong mét th­ môc web vµ c¸c th­ môc con.

File Global.asa x¸c ®Þnh khëi ®Çu vµ kÕt thóc cña mét øng dông web còng nh­ cña c¸c session cña tõng user ®¬n thÓ ®èi víi øng dông.

File nµy cho phÐp ®Æt 4 thñ tôc vµo: Session_OnStart, Session_OnEnd,

Application_OnStart, Application _OnEnd.


  • Session_OnStart: thùc hiÖn lÇn ®Çu tiªn khi cã mét user yªu cÇu trang web trong øng dông. (øng víi user ®ã)

  • Session_OnEnd: thùc hiÖn khi user tho¸t khái øng dông hoÆc timeout.

  • Application_OnStart: thùc hiÖn 1 lÇn khi trang web ®Çu tiªn cña øng dông ®­îc ch¹y lÇn ®Çu tiªn bëi bÊt kú user nµo.

  • Application _OnEnd: thùc hiÖn mét lÇn khi web server shutdown hoÆc khi tÊt c¶ c¸c session ®· ®ãng.


12. C¸c ph­¬ng thøc vµ thuéc tÝinh cña mét sè ®èi t­îng:

  • Reponse:

  • Collection: Cookies, form, ChentCertificates, QueryString, ServerVariables

  • Properties: Buffer, Charset, ContentType, Expires, ExpiresAbsolute, IsClientConnectied, Status, Pics, TotalByte.

  • Method: AddHeadder, AppendToLog, BinaryWrite, Clear, End, Flush, Redirect, Write, BinaryRead.

  • Server

  • Properties: ScriptTimeout

  • Method: CreateObject, HTMLEncode, MapPath, URLEncode.

  • Application

  • Collection: Contents, StaticObject.

  • Method: Lock, Unlock

  • Events: Application_OnStart, Application _OnEnd.

  • Session

  • Collection: Contents, StaticObject.

  • Method: Abandon

  • Properties: SessionID, TimeOut, CodePage, LCID

  • Events: Session_OnStart, Session_OnEnd


13. Sö dông Cookies:

Dïng ®èi t­îng Request ®Ó nhËn gi¸ trÞ Cookies.

Dïng ®èi t­îng Reponse ®Ó ®Æt gi¸ trÞ Cookies.

Cookie cã thÓ chØ mét gi¸ trÞ, nÕu nhiÒu gi¸ trÞ th× ph¶i dïng nhiÒu key.

Ex:

<% Reponse.Buffer = True %>

<% Dim dLastVisited

dLastVisited = Request.Cookies(“LastVisit”)

If dLastVisited = Empty Then

Reponse.Write “Welcome! This is your first visit”

Else

Reponse.Write “Welcome Back!


Reponse.Write “Your last visited on” & dLastVisited

End If

Reponse.Cookies(“LastVisit”) = Now()



Reponse.Cookies(“LastVisit”).Expires = “December 31,2000”

Session.Abandon %>



  • Session.Abandon tho¸t khái session, kh«ng cßn l¹i biÕn session nµo.


14. Gäi Stored Procedure trong ASP

<% Set Cnn1 = Server.CreateObject(“ADODB.Connection”)

StrCnn = “driver = {SQL Server}; Server = smas; uid = sa; pwd= ; database= pubs”

Cnn1.Open strCnn

Et sp = CreateObject(“ADODB.Command”)

Set sp.ActiveConnection = Cnn1

Sp.CommandType = adCmdStoredProc

Sp.CommandText = “sp_Help”

Set pm = sp.CrateParameter(“objname” = 200, 1, 92, “authors”)

Sp.parameters.Append pm

Set rs = sp.Excute()

Do Until rs is Nothing

Reponse.Write “”

Call PrintHeadings

Call PrintAllRecordsInRS

Reponse.Write “

Set rs=rs.NextRecordSet



Loop

Call CleanUp %>



<% Sub PrintHeadings()

For each hdFld in rs.Fields

Reponse.Write “

” & hdFld.Name & “
” & fld

Next

Reponse.Write “


Abs

Asc

Atn

Cbool

Cbyte

Ccur

Cdate

CDbl

Chr

Clnt

Cos

Csng

Cstr

Date

DateSerial

DateValue

Day

Exp

Fix

Hex

Hour

InputBox

InStr

Int

IsArray

IsDate

IsEmpty

IsNull

IsNumeric

IsObject

LBound

Lcase

Left

Len

Log

LTrim

Mid

Minute

Month

MsgBox

Now

Oct

Right

Rnd

Rtim

Second

Sgn

Sin

Space

Sqr

StrComp

Tan

Time

TimeSerial

TimeValue

Trim

Ubound

Ucase

VarType

Weekday

Year











16. Göi mail

Göi mail theo giao thøc SMTP

Dim objNewMail

Set objNewMail = Server.CreateObject(“CDONTS.NewMail”)

ObjNewMail.send sFromName, sToName, sSubject, sMsg

Set objNewMail = nothing


17.C¸c lÖnh ASP vµ VBScript (1.0)


Call

Dim

Do... loop

Empty

Erase

Exit

False

For... next

If... then... Else

Nothing

Null

On Error

Option Explicit

Randomize

Redim

Rem

Select case

Set

Sub

True

While...wend











18. C¸c h»ng (2.0)


Color

Comparion

Date and Time

Date format

File Input/Output

Miscellaneous

Msgbox

String

Var type


19. C¸c hµm:


Array

Create Object

DateAdd

DateDiff

DateParl

Filter

FormatCurrency

FormatDatetime

FormatNumber

FormatPercent

GetObject

InstrRev

Join

LoadPicture

MonthName

Replace

RGB

Round

ScriptEngineMajorVersion

ScriptEngineBuilVersion

ScriptEngine

ScriptEngineMinorVersion

Split

StrReverse

TypeName

WeekDayName






20. C¸c ®èi t­îng vµ hµm: (version 2.0)

Object: Dictionary; FileSystemObject; TextStream

Method:

Add

Close

CreateTextFile

FileExists

Items

Key

OpenTextFile

Read

ReadAll

ReadLine

Remove

RemoveAll

Skip

SkipLine

Write

WriteBlankLines

WriteLine





C¸c properties:

AtEndOfLine

AtEndOfStream

Column

CompareMode

Count

HelpContext

HelpFile

Item

Key

Line








C¸c lÖnh:

Const Private

For each... Next Public
21. §èi t­îng Error: (1.0)

Properties: Decription, Number, Source



Method: Clear, Raise
22. VBScript 3.0

  • Collection: Drives, Files, Folders

  • Constant: DriveType, SpecialFolder, File Attribute

  • Properties:

Attributes

FileSystem

ShareName

AvailableSpace

FileSpace

ShortName

DateCreated

IsReady

ShortPath

DateLastAccessed

IsRootFolder

Size

DateLastModified

Name

SubFolder

Drive

ParentFolder

TotalSize

DriveLetter

Path

Type

Drives

RootFolder

VolumeName

DriveType

SerialNumber




Files










  • Method:

AddFolder

BuildPath

Copy

CopyFile

CopyFolder

CreateFolder

Delete

DeleteFile

DeleteFolder

DriveExists

FileExists

FolderExist

GetAbsolutePathName

GetBaseName

GetDrive

GetDriveName

GetExtensionName

GetFile

GetFileName

GetFolder

GetParentFolderName

GetSpecialFolder

GetTempName

Move

MoveFile

MoveFolder

OpenAsTextStream


23. Sö dông Transaction:

C¸c yªu cÇu:

  • Dßng ®Çu tiªn ph¶i lµ: <% @TRANSACTION = REQUIRED%>

  • Cã thÓ ®iÒu chØnh chÊp nhËn hoÆc huûe bá giao dÞch b»ng c¸ch dïng SetComplete anh SetAbort tõ ®èi t­îng ObjectContext.

  • NÕu giao dÞch thµnh c«ng (commited), hµm sù kiÖn OnTransacton_Commit() ®­îc thùc thi.

  • NÕu giao dÞch thÊt b¹i (Aborted), sù kiÖn OnTransacton_Abort ®­îc thùc thi.

  • Yªu cÇu MSDTC (Distributed Transaction Coordinator)


VÝ dô 1:

<% @TRANSACTION = REQUIRED language = “VBSCRIPT”%>

<a href="/demo-nina-net-vnvppthanhdinh.html">Transaction Demo</a>

This is Demostration of using transaction with HTML.


For this example to work, MSDTC must be running on the web Server.

This next line will genegate an error.



<% set x = boo.boo %>


Does this line print ?




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương