ASP新手之常用源代码的总结(下)("ASP入门必看:常用源代码精华汇总(下篇)")
原创
一、数据库连接与操作
在ASP中,数据库连接和操作是常见的需求。以下是一些常用的源代码示例。
1. 使用ADO连接数据库
<%
Dim conn,rs
Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
' 连接SQL Server数据库
conn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=myDatabase;User ID=myUsername;Password=myPassword;"
conn.Open
' 执行SQL查询
rs.Open "SELECT * FROM myTable", conn
' 遍历查询导致
Do While Not rs.EOF
Response.Write(rs("columnName") & "<br>")
rs.MoveNext
Loop
' 关闭连接
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
%>
二、文件操作
ASP提供了充足的文件操作功能,以下是一些常用的文件操作代码。
1. 创建文本文件
<%
Dim fs, f, s
Set fs = CreateObject("Scripting.FileSystemObject")
' 创建文件
Set f = fs.CreateTextFile("C:\myFile.txt", True)
' 写入内容
f.WriteLine("Hello, World!")
f.WriteLine("This is a text file.")
' 关闭文件
f.Close
Set f = Nothing
Set fs = Nothing
%>
2. 读取文本文件
<%
Dim fs, f, s
Set fs = CreateObject("Scripting.FileSystemObject")
' 打开文件
Set f = fs.OpenTextFile("C:\myFile.txt", 1)
' 读取内容
s = f.ReadAll
' 显示内容
Response.Write(s)
' 关闭文件
f.Close
Set f = Nothing
Set fs = Nothing
%>
三、表单处理
在ASP中,处理表单数据是常见的任务。以下是一些表单处理的代码示例。
1. 获取表单数据
<form action="process.asp" method="post">
Name: <input type="text" name="name">
Age: <input type="text" name="age">
<input type="submit" value="Submit">
</form>
<%
' process.asp
Dim name, age
name = Request.Form("name")
age = Request.Form("age")
Response.Write("Name: " & name & "<br>")
Response.Write("Age: " & age & "<br>")
%>
2. 验证表单数据
<form action="process.asp" method="post">
Name: <input type="text" name="name">
Age: <input type="text" name="age">
<input type="submit" value="Submit">
</form>
<%
' process.asp
Dim name, age, errorMessage
name = Request.Form("name")
age = Request.Form("age")
errorMessage = ""
If name = "" Then
errorMessage = "Name is required.<br>"
End If
If age = "" Or Not IsNumeric(age) Then
errorMessage = errorMessage & "Age is required and must be a number.<br>"
End If
If errorMessage = "" Then
Response.Write("Name: " & name & "<br>")
Response.Write("Age: " & age & "<br>")
Else
Response.Write(errorMessage)
End If
%>
四、邮件发送
ASP提供了发送邮件的功能,以下是一个发送邮件的代码示例。
1. 使用CDONTS发送邮件
<%
Dim mail
Set mail = Server.CreateObject("CDONTS.NewMail")
' 设置邮件参数
mail.From = "yourEmail@example.com"
mail.To = "recipient@example.com"
mail.Subject = "Test Email"
mail.TextBody = "This is a test email sent from ASP."
' 发送邮件
mail.Send
' 清理对象
Set mail = Nothing
%>
五、会话管理
在ASP中,会话管理用于跟踪用户的状态。以下是一些会话管理的代码示例。
1. 设置和获取会话变量
<%
' 设置会话变量
Session("username") = "myUsername"
' 获取会话变量
Dim username
username = Session("username")
Response.Write("Hello, " & username & "!")
%>
2. 设置会话超时
<%
' 设置会话超时时间为20分钟
Session.Timeout = 20
%>
六、失误处理
在ASP编程中,失误处理是非常重要的。以下是一些失误处理的代码示例。
1. 使用On Error语句
<%
On Error Resume Next
' 大概引发失误的代码
Dim result
result = 1 / 0
If Err.Number <> 0 Then
Response.Write("Error: " & Err.Description)
Else
Response.Write("Result: " & result)
End If
' 清除失误
On Error GoTo 0
%>
2. 自定义失误页面
<%
' 在服务器上设置自定义失误页面
Server.Transfer "errorPage.asp"
%>
以上是ASP新手常用的源代码总结。通过这些示例,新手可以迅捷掌握ASP的基本用法,为后续的深入学习打下坚实的基础。