来源:www.cncfan.com | 2006-11-3 | (有2243人读过)
[一] 纯ASP脚本表现功能[增/删/查/改]
Test_Tb.asp ----------------------------------------------------------------------------------------------------------
<script language="javascript">
function operate(obj)
{ window.location.href='operate.asp?action='+obj.value+'&cd_id='+f1.cd_id.value+'&cd_name='+f1.cd_name.value+'&cd_author='+f1.cd_author.value;
}
</script>
<html>
<body>
Temp_CD Table
<form name="f1">
<ul>
<li>cd_id:<input type="text" name="cd_id" />
<li>cd_name:<input type="text" name="cd_name" />
<li>cd_author:<input type="text" name="cd_author" />
</ul>
<input type="button" value="Add" onClick="operate(this)" />
<input type="button" value="Delete" onClick="operate(this)" />
<input type="button" value="Search" onClick="operate(this)" />
<input type="button" value="Update" onClick="operate(this)" />
</form>
</body>
</html>
operate.asp ----------------------------------------------------------------------------------------------------------
<%
Option Explicit
Response.Buffer = True
On Error Resume Next
Dim StrConn,Conn,RS,Sql
Dim action,cd_id,cd_name,cd_author
Dim Msg
StrConn = "driver={sql server};server=(local);uid=sa;password=love!@#;database=Northwind"
Set Conn = Server.CreateObject("ADODB.Connection")
action = Request.QueryString("action")
cd_id = Request.QueryString("cd_id")
cd_name = Request.QueryString("cd_name")
cd_author = Request.QueryString("cd_author")
Response.Write("action:"&action&"<br>")
Response.Write("CD_ID:"&cd_id&"<br>")
Response.Write("CD_Name:"&cd_name&"<br>")
Response.Write("CD_Author:"&cd_author&"<p>")
Conn.Open StrConn
If Err.Number <> 0 Then
Msg = "The Database Connect Fail !"
Else
If action = "Search" Then
Sql = "Select * From Temp_CD Where CD_ID = "&cd_id
Set Rs = Conn.execute(Sql)
If Err.Number <> 0 Then
Msg = action&" Fail!"
Else
If Rs.Eof Then
Msg = "No Any Records!"
Else
Msg = "CD_Name:"&Rs("CD_Name")&"<br>CD_Author:"&Rs("CD_Author")
End If
End If
Rs.Close
Set Rs = Nothing
Else
Select Case action
Case "Add"
Sql = "Insert Temp_CD (CD_Name,CD_Author)values('"&cd_name&"','"&cd_author&"')"
Case "Delete"
Sql = "Delete Temp_CD Where CD_ID = "&cd_id
Case "Update"
Sql = "Update Temp_CD Set CD_Name = '"&cd_name&"',CD_Author = '"&cd_author&"'"
End Select
Conn.execute(Sql)
If Err.Number <> 0 Then
Msg = action&" Fail!"
Else
Msg = action&" Success!"
End If
End If
Conn.Close
Set Conn = Nothing
End If
Response.Write(Msg)
%>
附: ----------------------------------------------------------------------------------------------------------
不知代码是否可以更精练?
|