跳到主要內容

ASP 語法 1

http://www.w3schools.com/vbscript/vbscript_ref_functions.asp
http://big5.webasp.net/article/4/3475.htm
--------------------------------------------------------------
字串處理
document.write(Round(2.323,2))  '2.32    四捨五入
document.write(UCase("DcCIx"))  'DCCIX   轉大寫
document.write(LCase("DcCIx")) 'dccix    轉小寫
document.write(StrReverse("acc123")) '321cca   反轉字串
document.write(String(5,"*"))   '*****     補滿字元
document.write(StrComp("AXBC","AxBC",1))  '0    比較字串 參數1:不分大小寫,比較相同結果為0
document.write(Right("1234567",3))  '567   取出右邊字串
document.write(Left("1234567",3))  '123   取出左邊字串
document.write(Replace("A5B5C5","5","X"))  'AXBXCX   替換字串
document.write(Mid("ABCDEFG",1,3))  'ABC    取出部份字串(從1開始)
document.write("S" & Trim(" ABCDE ") & "S")  'SABCDES    濾除空白
document.write("S" & LTrim(" ABCDE ") & "S") 'SABCDE S   濾除左邊空白
document.write("S" & RTrim(" ABCDE ") & "S") 'S ABCDES  濾除右邊空白
document.write(Len("CKLDD"))  '5    字串長度
document.write(Instr("AXKDOR","K"))  '3    indexOf(從1開始)
document.write(InstrRev("AXKDOR","K")) '3   lastIndexOf
randomize()  亂數
rnd()  '0.1647455
--------------------------------------------------------------
陣列處理
a = Array(5,10,15,20,52)
document.write(a(0))  '5

過濾陣列
b = Filter(a,"5",True) 'False : not contains
for each x in b
   'document.write(x & "<br>")   '5 15 25
next

取出陣列值.加分隔符
document.write(Join(a,"/"))  '5/10/15/20/52

分割陣列
c = "A/B/C/D/E"
d = Split(c,"/")
for each y in d
   'document.write(y & "<br>")   'A B C D E
next

document.write(LBound(a))   '0
document.write(UBound(a))   '4  (ubound+1 = length)
--------------------------------------------------------------
dim a(3)  '陣列
a(0) = "w"
a(1) = "x"
a(2) = "y"
a(3) = "z"
document.write(a(3))  'z
document.write(lbound(a))  '0
document.write(ubound(a))  '3
--------------------------------------------------------------
數值判斷及處理
document.write(isNumeric("123")) 'True   判斷是否為數值格式
document.write(Abs(-2.345))  '2.345
document.write(Int(2786.8458))  '2786  只取整數(無條件捨去)
document.write(Int(-27565.001))  '-27566  只取整數(無條件捨去)
document.write(Fix(2.345))   '2  只取整數(無條件捨去)
document.write(Fix(-2.345)) '-2  只取整數(無條件捨去)
document.write(Sgn(2.345))  ' 1   判斷正負
document.write(Sgn(-2.345))  ' -1   判斷正負
document.write(19 mod 5) '取餘數 4
document.write(28\11)  '只取整數2(無條件捨去)
--------------------------------------------------------------
格式化字串
document.write(FormatCurrency(12345.256,2))  'NT$12,345.26  金錢(四捨五入)
document.write(FormatNumber(22345.3457,3)) '22,345.346  數字(四捨五入)
document.write(FormatPercent(3/7,2))  '42.86%    百分比(四捨五入)
--------------------------------------------------------------
日期相減
fromDate="2010/11/20 09:30:30"
toDate="2012/11/23 13:45:45"
document.write(DateDiff("yyyy",fromDate,toDate))  'Year  '2
document.write(DateDiff("q",fromDate,toDate))   'Quarter  '8
document.write(DateDiff("m",fromDate,toDate))  'Month  '24
document.write(DateDiff("y",fromDate,toDate))  'Day of year  '734
document.write(DateDiff("d",fromDate,toDate))  'Day  '734
document.write(DateDiff("w",fromDate,toDate))  'Weekday  '104
document.write(DateDiff("ww",fromDate,toDate))  'Week of year  '105
document.write(DateDiff("h",fromDate,toDate))  'Hour  '17620
document.write(DateDiff("n",fromDate,toDate))  'Minute  '1057215
document.write(DateDiff("s",fromDate,toDate))  'Second  '63432915
--------------------------------------------------------------
日期處理
d=CDate("2012-11-26")
document.write(DatePart("w",d))  '2 monday   取出日期特定部分
document.write(WeekdayName(datepart("w",now)) )  '星期一
document.write(FormatDatetime(now,2) & " " & FormatDatetime(now,4) & ":" & datepart("S",now))  '2012/12/5 18:00:50

document.write(IsDate("2012/12/25"))  'True  判斷是否為日期格式
document.write(Now)   '2012/11/26 上午 09:47:40   現在日期時間
document.write(Date)  '2012/11/26   現在日期
document.write(Time)  '上午 10:24:11   現在時間
document.write(DateAdd("d",1,now)) '2012/11/27 上午 10:27:06   計算時間和
Response.Write(DateSerial(Year(now),Month(now),Day(now)-6))  '2012/12/1  任意加減年月日
document.write(TimeSerial(hour(now),minute(now) - 30,second(now))) '下午 01:44:29  任意加減時分秒

document.write(year(now))  '2012   取出日期特定部分
document.write(month(now))  '12
document.write(day(now))  '14
document.write(weekday(now))  '6 friday
document.write(hour(now))  '15
document.write(minute(now))  '58
document.write(second(now))  '59

document.write(FormatDateTime(now,1)) '2012年11月26日  格式化日期
document.write(FormatDateTime(now,2)) '2012/11/26
document.write(FormatDateTime(now,3)) '上午 10:37:47
document.write(FormatDateTime(now,4)) '10:38

If Time < #12:00:00# then
     Response.Write "AM"
else
     Response.Write "PM"
end if

Response.AddHeader "Refresh", "3;url=http://www.google.com.tw"
--------------------------------------------------------------
型別轉換
document.write(CBool("0"))  'False    型別轉換 布林
document.write(CDbl(123.34872))  '123.34872  浮點數
document.write(CLng(123.34872))  '123    長整數
document.write(CSng(123.34872))  '123.3487   短整數
document.write(CInt(123.34872))  '123   整數
document.write(CStr(123.34872))  '123.34872   字串
document.write(cbyte(255)) '255  byte 0~255
document.write(CCur(5.955553))  '5.9556
document.write(cdate("2012-12-12 05:25:56 PM"))  '2012/12/12 下午 05:25:56
--------------------------------------------------------------
document.write(chr(65)) 'A  字元  0~126
document.write(asc("A")) '65 ascii
document.write(Hex(10)) 'A  16進位
document.write(Oct(8)) '10  8進位
document.write(2 ^ 3)  '8   次方
document.write(sqr(9))   平方根
document.write(8^(1/3))  立方根
--------------------------------------------------------------
是否為空
vbNullString : 未定義變數、已定義變數.未給值、已定義變數.但為空字串
isEmpty(a) : 未定義變數、已定義變數.未給值
--------------------------------------------------------------
Scripting.Dictionary
dim d
set d = Server.CreateObject("Scripting.Dictionary")
d.Add "1", "A"
d.Add "2", "B"
d.Add "3", "C"
d.Add "4", "D"

'response.write(d.Item("2"))  'B  取得單一值
'response.write(d.Count)  '4   項目總數
'response.write(d.Exists("3"))   'True  key是否存在
'd.Remove("3")   '移除項目

'for each k in d.Keys
    'response.write(k)   '1 2 3 4  取得所有key
'next

'for each i in d.Items
    'response.write(i)   'A B C D  取得所有value
'next
--------------------------------------------------------------
'Session.LCID = 1028
'Response.Write(now)  '2012/12/18 下午 03:55:03

Session.LCID = 1041
Response.Write(now)  '2012/12/18 15:57:53
--------------------------------------------------------------
REDIM PRESERVE
dim a
a = Array(1,2,3)
'document.write(ubound(a)) 'lbound 0,ubound 2
for each x in a
   document.write(x & "<br>") '1 2 3
next

redim preserve a(4)
a(3) = "A1"
a(4) = "A2"
'document.write(ubound(a)) 'lbound 0,ubound 4
for each x in a
   document.write(x & "<br>") '1 2 3 A1 A2
next
--------------------------------------------------------------
dim a  '錯誤處理
a = Array("1","x","3","y","6")
for i = lbound(a) to ubound(a)
     document.write(cint(a(i)) & "<br>")
   
     If Err.Number <> 0 Then
          document.write(a(i) & " is not a valid number <br>")
     end if

     On Error Resume Next  
next
==============================================================
Set bc = Server.CreateObject("MSWC.BrowserType")
Response.Write bc.Browser & bc.Version & bc.majorver & bc.minorver  'IE6.060
--------------------------------------------------------------
Server.Execute("100.asp")
Server.HTMLEncode("A<br>B")  'A<br>B
Server.URLEncode("A&B")  'A%26B
Server.MapPath("/")   'F:\GMUW_DEV\WEB\WEB
Server.ScriptTimeout    '90 secs
--------------------------------------------------------------
Session.CodePage   '950
Session.LCID  '1028
Session.SessionID  '1051686005
Session.Timeout  '20   mins
Session("key") = "value"
--------------------------------------------------------------
Response.Redirect("bbb.asp")
Response.Expires = -1   'never cached
Response.CacheControl = "no-cache"  'never cached
Response.Charset = "ISO8859-1"
Response.ContentType = "text/html"
Response.Buffer = true / Response.Flush /Response.Clear
Response.Write
Response.End
--------------------------------------------------------------
Response.Cookies("Test").Expires = date + 1
Response.Cookies("Test") = "A"
Response.Cookies("Test")("Test1") = "A1"
Response.Write(Request.Cookies("Test"))   'Test1=A1
Response.Write(Request.Cookies("Test")("Test1"))   'A1
--------------------------------------------------------------
Request.ServerVariables("HTTP_REFERER") 'http://10.86.18.104:6667/aaa.asp
Request.ServerVariables("CONTENT_TYPE")  '
Request.ServerVariables("LOCAL_ADDR")  '10.86.18.104
Request.ServerVariables("LOGON_USER")  '
Request.ServerVariables("REMOTE_ADDR")  '10.86.10.61
Request.ServerVariables("REMOTE_HOST")  '10.86.10.61
Request.ServerVariables("REQUEST_METHOD")  'GET
Request.ServerVariables("SERVER_NAME")  '10.86.18.104
Request.ServerVariables("SERVER_PORT")  '6667
Request.ServerVariables("ALL_HTTP")  'HTTP_CONNECTION:Keep-Alive HTTP_ACCEPT:*/* HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:zh-tw HTTP_COOKIE:ASPCLIENTDEBUG=1; User=StaffId=01441&UserBranchId=999&UserFullName=%A5D%BA%DE%A4%A4%A4%40++++&UserId=T220278692&GroupId=0003&ServiceLimit=5&branch%5Fcounter%5Fid=; ASPSESSIONIDCSRBSRDA=LHDFCGLCOAPBFNHAMCMAIJMI; ASPSESSIONIDCQSASRCB=OPLNMOLCEOFDBHEBPCAKACCG; ASPSESSIONIDCQQCTQCA=BOFJKHMCMMGLDPDJCCNKNEPF; ASPSESSIONIDAQRBTQDB=OJKJENMCHFKDBOOBIGLMPKCD; ASPSESSIONIDCQQBSQCA=KDKNFAEDODICLDEKHMMGCFDH; ASPSESSIONIDASSCRQDA=AHEHPKODJJLLFLCCKAAEKCCF HTTP_HOST:10.86.18.104:6667 HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
Request.ServerVariables("HTTP_HOST")  '10.86.18.104:6667
Request.ServerVariables("HTTP_ACCEPT_LANGUAGE")  'zh-tw
Request.ServerVariables("SERVER_SOFTWARE")  'Microsoft-IIS/6.0

Request.QueryString("key")  'get
Request.Form("key")  'post
Request.Totalbytes   '
Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("SCRIPT_NAME")  '10.86.18.104:6667/aaa.asp
==============================================================
<%@LANGUAGE="VBScript" CODEPAGE="65001" %>
<!-- #include virtual ="/html/header.inc" -->
<!-- #include file ="headers\header.inc" -->
==============================================================
TypeName()
document.write(TypeName("test"))  'String
document.write(TypeName(8))  'Integer
document.write(TypeName(5.579))  'Double
document.write(TypeName(null))  'Null
document.write(TypeName(empty))  'Empty
document.write(TypeName(true))  'Boolean
==============================================================
eval()
dim xyz
xyz = replace(replace(replace(("x+y*z"),"x","2"),"y","3"),"z","7")
document.write(eval(xyz))  '23
==============================================================
顯示所有的cookie
dim x,y
for each x in Request.Cookies
  response.write("<p>")
  if Request.Cookies(x).HasKeys then
    for each y in Request.Cookies(x)
      response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
      response.write("<br>")
    next
  else
    Response.Write(x & "=" & Request.Cookies(x) & "<br>")
  end if
  response.write "</p>"
next
==============================================================
VBScript.RegExp
Dim reg, retVal ,mail
mail = "skx@hotmail.com"
Set reg = CreateObject("VBScript.RegExp")
reg.Pattern = "^\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$"
reg.IgnoreCase = True
retVal = reg.Test(mail)
Set reg = Nothing
document.write(retVal) 'True
--------------------------------------------------------------
RegExp-Test()
dim re , str
str = "007@68962"
Set re = New RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "^[0-9]*$"  '驗證格式
document.write(re.Test(str))  'False
--------------------------------------------------------------
RegExp-Replace()
dim re , str
str = "007@68@96!2"
Set re = New RegExp
re.IgnoreCase = True
re.Global = True
're.Pattern = "[@,!]"   '007-68-96-2
re.Pattern = "[^0-9]"   '007-68-96-2  排除不符合被取代的項目
document.write(re.Replace(str,"-"))
--------------------------------------------------------------
RegExp-Execute()
dim re , str ,colMatches
str = "007@68@96!2"
Set re = New RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "[0-9]*"   '找出符合的項目進行分組
're.Pattern = "[^@,!]*"
set colMatches = re.Execute(str)

for each x in colMatches
     if x.Value <> "" then
         document.write(x.Value  & "<br>")  '007 68 96 2
     end if
next
==============================================================
呼叫sub(有參數)
<%
sub sub1(num1,num2)
     Response.Write(num1*num2 & "sub1 done")  
end sub
%>
Result: <% sub1 3,6 %>
Result: <% call sub1(3,6) %>
--------------------------------------------------------------
呼叫sub(無參數)
<%
sub sub1()
     Response.Write("test")  
end sub
%>
Result: <% sub1 %>
Result: <% sub1() %>
Result: <% call sub1 %>
Result: <% call sub1() %>
--------------------------------------------------------------
呼叫function 可不使用();若有參數則必須使用()
<%
function vbFunction1()
     Response.Write("test")
     vbFunction1 = "vbFunction1 done"
end function
%>
Result: <%= vbFunction1 %>
Result: <%= vbFunction1() %>
Result: <%= vbFunction1(3,5) %>  
--------------------------------------------------------------
頁面中呼叫 vbs function
<%
function vbFunction1(num1,num2)
     Response.Write(num1*num2)
     vbFunction1 = "vbFunction1 done"
end function
%>
Result: <%= vbFunction1(3,6) %>
--------------------------------------------------------------
js function中呼叫 vbs function
<script language="javascript">
           function jsFunction1()
           {
                document.write('<%= vbFunction1(3,6) %>');
           }
</script>

<input type="button" value="call js" onClick="jsFunction1()">
--------------------------------------------------------------
js 事件中呼叫 vbs function
<script language="javascript">
           function jsFunction2(obj)
           {
                document.write(obj);
           }
</script>

<input type="button" value="call js" onClick="jsFunction2('<%= vbFunction1(3,6) %>')">
==============================================================
checkbox多選取值
<%
For i = 1 To Request.Form("chk").Count
Response.Write Request.Form("chk")(i) & "<br>"     '從1開始
Next
%>

<form method="POST" action="bbb.asp">
<input type="checkbox" name="chk" value="A">
<input type="checkbox" name="chk" value="B">
<input type="checkbox" name="chk" value="C">
<input type="submit" value="submit">
</form>
--------------------------------------------------------------
是否包含不合法字元
Dim sBadChars, iCounter ,IllegalChars ,sInput
sInput = "ekX*k83D"
IllegalChars = False
sBadChars = Array("--", "'", """", "+", "*", "%", _
   "/", "\", "<", ">", "=", "[", "]", "?", "`", "|", "select", "drop", "delete", "insert")

For iCounter = 0 To UBound(sBadChars)
      If InStr(sInput, sBadChars(iCounter)) > 0 Then
            IllegalChars = True
            Exit For
      End If
Next
document.write(IllegalChars)
--------------------------------------------------------------
以多個字元 拆解字串
Dim delimiter, value, x
delimiter = Array("-", " ", " ")
for each x in delimiter
   value = "213-234-89-7"
   if UBound(Split(value, x)) > 0 then
lsTel = Split(value, x)
exit for
   End if
   lsTel = Split(value)
next
ReDim Preserve lsTel(3)
       
for each y in lsTel
        document.write(y & "<br>")
next
--------------------------------------------------------------
'無條件進至整數
if instr(cstr(g1),".") > 0 then
       if mid(split(cstr(g1),".")(1),1,1) <> 0 then
            g1 = cint(split(cstr(g1),".")(0)) +  1                  
       else
            g1 = cint(split(cstr(g1),".")(0))
       end if    
end if
--------------------------------------------------------------
'無條件進位到小數第一位
d1 = d1 * 10   '根據計算小數點位數改變        
if instr(cstr(d1),".") > 0 then                
     if mid(split(cstr(d1),".")(1),1,1) <> 0 then
           d1 = cint(split(cstr(d1),".")(0)) + 1
     else
           d1 = cint(split(cstr(d1),".")(0))                                        
     end if
end if                                    
d1 = d1 / 10
--------------------------------------------------------------
'無條件捨去(只取整數)
fix(g1)
--------------------------------------------------------------
'無條件捨去(小數點後第二位)
dim  g1
g1 = 12.6592
g1 = g1*100   '根據計算小數點位數改變
g1 = fix(g1) /100
--------------------------------------------------------------
'四捨五入到整數            
dim  x , x1
x = 12.56813
if instr(x,".") > 0 then
x=split(cstr(x),".")
if ubound(x)+1>1 then          
if CInt(Mid(x(1),1,1)) >= 5 then
if x(0)<>"" then
     x(0)=x(0)+1
else
             x(0)=0+1
       end if
end if
end if
        x1=x(0)
else
x1=x
end if
--------------------------------------------------------------
'四捨五入到小數點第二位
dim  x , x1
x = 12.561013
x = x * 100  '根據計算小數點位數改變
if instr(x,".") > 0 then
x=split(cstr(x),".")
if ubound(x)+1>1 then          
if CInt(Mid(x(1),1,1)) >= 5 then
if x(0)<>"" then
x(0)=x(0)+1
else
             x(0)=0+1
       end if
end if
end if
        x1=x(0)
else
x1=x
end if
x1 = x1/100
--------------------------------------------------------------
左方補滿
Function Lpad (inputStr, padChar, lengthStr)
    Lpad = string(lengthStr - Len(inputStr),padChar) & inputStr
End Function

Response.Write Lpad("8392","0","6")  '008392
--------------------------------------------------------------
右方補滿
Function Rpad (inputStr, padChar, lengthStr)
    Rpad = inputStr & string(lengthStr - Len(inputStr), padChar)
End Function

Response.Write Rpad("8392","0","6")  '839200
--------------------------------------------------------------
'補足長度 (內容 補足長度 補足字元 左或右)
Function PAD(S, N, C , LR )
Dim SZ , L , I , CH
SZ = Trim(S)
L = 0
If SZ <> "" Then
For I = 1 To Len(SZ)
If Asc(Mid(SZ, I, 1)) >= 0 And Asc(Mid(SZ, I, 1)) <= 255 Then
L = L + 1
Else
L = L + 2
End If
Next
End If
If L < N Then
If C = "" Then
CH = " "
Else
CH = Left(C, 1)
End If
Select Case UCase(LR)
Case "R"
SZ = SZ & String(N - L, CH)
Case "L"
SZ = String(N - L, CH) & SZ
End Select
ElseIf L > N Then
SZ=Left(SZ,N)
End If
PAD = SZ
End Function
--------------------------------------------------------------
是否為數值
function isReallyNumeric(ByVal sNum,ByVal hasDigit)
        dim d
        if cbool(hasDigit) = true then
            sNum = replace(sNum,".","")
        end if

        isReallyNumeric = true
        for i = 1 to len(sNum)
            d = mid(sNum, i, 1)
            if asc(d) < 48 OR asc(d) > 57 then
                isReallyNumeric = false
                exit for
            end if
        next
end function

isReallyNumeric("234.53",true)  'True
===============================================================
Do / Loop
If oVBFunc.OpenRecordSet(Rs, conn, "select branch_id,branch_name from branch where branch_id < '900'", sErrMsg) Then
data = data & "<OPTION>請選擇分行</OPTION>"
Do Until Rs.EOF
branch_id = Trim(Rs("branch_id"))
data = data & "<OPTION value=""" & branch_id & """>" & Rs("branch_name") & "(" & branch_id & ")</OPTION>"
Rs.MoveNext
Loop
Rs.Close
Set Rs = Nothing
Else
Set Rs = Nothing
End If
--------------------------------------------------------------
while / wend
function PF_Common(idName, firstOption)
dim conn, Rs,tsql
Call pf_OpenConnection(conn)
set Rs = CreateObject("ADODB.Recordset")
tsql= "select item_no, name from common where type = '" & idName & "' order by ""order"" "
Rs.Open tsql, conn
Response.Write "<select id='" & idName & "' name='" & idName & "'>" & vbcrlf
If TRIM(firstOption) <> "" then
Response.Write "<option value=''>" & firstOption & "</option>" & vbcrlf
end if
while not(Rs.EOF)
Response.Write  "<option value='" & Rs(0) & "'>" & Rs(1) & "</option>" & vbcrlf
Rs.MoveNext
WEND
Response.Write "</SELECT>" & vbcrlf
Rs.Close
conn.CLOSE
SET Rs=NOTHING
SET conn=NOTHING
end function
--------------------------------------------------------------
if /end if  (exec storedprocedure)
dim cmd,rvalue,rs
rvalue = ""
set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = "exec sp_test '" & customer_id & "'"
set rs = cmd.Execute
if not rs.EOF then
rvalue = Trim(rs.fields(0))
end if
rs.close
set rs = nothing
set cmd = nothing
--------------------------------------------------------------
'select case
Function ApplyType(ByVal stype)
   Select Case stype
      Case "1" : ApplyType = "一般"
      Case "2" : ApplyType = "學生"
      Case "3" : ApplyType = "軍人"
      Case "4" : ApplyType = "主婦"
      Case Else: ApplyType = "錯誤" & stype
   End Select
End Function
===============================================================
rs.Fields.Count: 欄位數
rs(i).Name: 第 i 欄位名稱, i = 0 ~ rs.Fields.Count-1
rs(i): 第 i 欄位值, i = 0 ~ rs.Fields.Count-1
rs("欄位名稱"): 讀取欄位值
rs.RecordCount: 資料筆數
rs.EOF: 是否為最後一筆
rs.MoveNext: 移至下一筆
rs.MovePrev: 移至上一筆
rs.MoveFirst: 移至第一筆
rs.MoveLast: 移至最後筆
rs.Close: 關閉資料集
--------------------------------------------------------------
DSN
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open "DSN=MyDSN;UID=user;PWD=password;DATABASE=mydatabase"
%>

無DSN
<%
Set Conn = Server.CreateObject("ADODB.Connection")
DSNtest="DRIVER={SQL Server};SERVER=ServerName;UID=USER;PWD=password;DATABASE=mydatabase"
Conn.open DSNtest
%>
--------------------------------------------------------------
'Modify Db
dim connstr ,Conn ,effectRows, strSql
connstr = "driver={SQL Server};server=10.86.18.104,1744;uid=gmuw;pwd=gmuw;database=gmcard_rd"
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open connstr
strSql = "update application_status set customer_name = 'xxx2' where case_no = 604974 "
Conn.Execute strSql, effectRows

Response.Write "<h1>" & effectRows  & " rows effected </h1>"

Conn.Close
set Conn = Nothing
--------------------------------------------------------------
'Recordset
dim connstr ,Conn ,Rs, strSql
connstr = "driver={SQL Server};server=10.86.18.104,1744;uid=gmuw;pwd=gmuw;database=gmcard_rd"
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open connstr
strSql = "select top 10 * from application_status "

set Rs = CreateObject("ADODB.Recordset")
Rs.Open strSql,Conn

while not Rs.eof
Response.Write Rs("case_no") & "<br/>"
Rs.MoveNext
WEND
Rs.Close
Conn.Close
set Rs = Nothing
set Conn = Nothing
--------------------------------------------------------------
'RecordCount
dim connstr ,Conn ,Rs, strSql
connstr = "driver={SQL Server};server=10.86.18.104,1744;uid=gmuw;pwd=gmuw;database=gmcard_rd"
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open connstr
strSql = "select top 10 * from application_status "

set Rs = CreateObject("ADODB.Recordset")
Rs.Open strSql,Conn , 3, 1 '開啟一個靜態資料指標, 唯讀資料無法變更 (會回傳RecordCount)

Response.Write Rs.RecordCount

Rs.Close
Conn.Close
set Rs = Nothing
set Conn = Nothing
--------------------------------------------------------------
'exec Command
dim connstr ,Conn ,Rs,Cmd, strSql
connstr = "driver={SQL Server};server=10.86.18.104,1744;uid=gmuw;pwd=gmuw;database=gmcard_rd"
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.ConnectionTimeout = 15 'sec
Conn.Open connstr
strSql = "select * from dbo.fn_attation_code(1) "

set Cmd = CreateObject("ADODB.Command")
Cmd.CommandTimeOut = 30 'sec

Cmd.ActiveConnection = Conn
Cmd.CommandText = strSql
set Rs = Cmd.Execute

while not Rs.eof
Response.Write Rs("attention_target") & "<br/>"
Rs.MoveNext
WEND

Conn.Close
set Conn = Nothing
--------------------------------------------------------------
'commit rollback
dim connstr ,Conn ,effectRows, strSql,ErrMsg
connstr = "driver={SQL Server};server=10.86.18.104,1744;uid=gmuw;pwd=gmuw;database=gmcard_rd"
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open connstr
Conn.BeginTrans

On Error Resume Next   'must exists else the Err cant catch

strSql = "update application_status set customer_name = 'x20121' where case_no = 604974 "
strSql = strSql + "; update application_status set customer_name = 'x20122' where case_no = 605101 "  'A605101

Conn.Execute strSql, effectRows

If Err.Number<>0 Then  
    ErrMsg = now & "   SQL:" & strSql & ";錯誤訊息為: " & Err.Description
                '2012/12/11 上午 11:08:07 SQL:update application_status set customer_name = 'x20123' where case_no = 604974 ; update application_status set
           'customer_name = 'x20124' where case_no = A605101 ;錯誤訊息為: [Microsoft][ODBC SQL Server Driver][SQL Server]無效的資料行名稱 'A605101'。
        Response.Write ErrMsg
Conn.RollbackTrans  
Else
Conn.CommitTrans
Response.Write effectRows & " rows effected."  '1 rows effected.
End If

Conn.Close
set Conn = Nothing
==============================================================
檔案處理
On Error Resume Next
dim fso, textstream
set fso = Server.CreateObject("Scripting.FileSystemObject")

'1=ForReading/2=ForWriting/8=ForAppending, true:if file not exists then create
'read-------------------------------------------------------
'set textstream = fso.OpenTextFile(Server.MapPath("all.txt"),1,true)
'While textstream.AtEndOfStream = False
    'Response.Write textstream.ReadLine & "<br>"
'Wend
'Response.Write textstream.ReadAll

'write-------------------------------------------------------
set textstream = fso.OpenTextFile(Server.MapPath("all.txt"),8,true)
textstream.WriteLine("test20121212")
If Err.Number<>0 Then
      Response.Write Err.Description  '沒有使用權限
end if

'create------------------------------------------------------
set textstream = fso.CreateTextFile("c:\somefile.txt",true)   'true is override exist file
textstream.WriteLine(now)
textstream.close
set textstream = nothing
set fso = nothing

'delete-------------------------------------------------------
if fso.FileExists("c:\test.txt") then
    fso.DeleteFile("c:\test.txt")
end if
set fso = nothing

textstream.Close
set textstream = Nothing
set fso = Nothing
==============================================================
//發送信件
Set mail = CreateObject("CDO.Message")
mail.Subject = "主旨"
mail.From = "abc@mail.com"
mail.To = "xtz@smail.com;mno@smail.com"
mail.Bcc="red@mail.com"
mail.Cc="white@mail.com"

mail.TextBody = "內容"
mail.HTMLBody = "<h1>This is a message.</h1>"
mail.CreateMHTMLBody "http://www.w3schools.com/asp/"
mail.CreateMHTMLBody "file://c:/mydocuments/test.htm"

mail.AddAttachment "c:\mydocuments\test.txt"

mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.server.com"
mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

mail.Configuration.Fields.Update
mail.Send
set mail = nothing