Home > Backend Development > PHP Tutorial > ASP basic knowledge Command object explanation_php basics

ASP basic knowledge Command object explanation_php basics

WBOY
Release: 2016-05-16 20:07:23
Original
1050 people have browsed it

The Coonamd object defines the commands that will be executed on the data source. It can be used to query the database table and return a record set, and can also be used to add, change and delete the database table.
1. Steps to use Command object:
When using the Command object to process data in an ASP page, you should first set the command type, command text, and related active database connections, etc., and pass the command parameters through the Parameter object, and then execute the SQL statement or call by calling the Execute method Stored procedures to complete the task of retrieving, adding, changing, and deleting database records. The steps are as follows:
1. Use the ActiveCommand attribute to set the relevant database connection;
2. Use the CommandType attribute to set the command type;
3. Use the CommandText attribute to define the executable text of a command (such as a SQL statement);
4. Use the CommandTimeout attribute to set the command timeout;
5. Use the Execute method to execute the command.
2. Properties of Command object:

3. Methods of Command object——Execute
This method executes the query specified in the CommandText property. The syntax format is divided into the following two forms.
1. For Command returned by row:
Set recordset=command.Execute(RecordsAffected,Parameters,Options)
2. For Command that does not return by line:
command.Execute RecordsAffected,Parameters,Options
The parameter RecordsAffected is the number of records affected by the provider return operation. Rarameters are parameter values ​​passed using SQL statements. Options instructs the provider how to assign a value to the Command object's CommandText property.
4. Use Parameters collection
Command objects have a Parameters collection consisting of Parameter objects that represent parameters or arguments associated with a Command object based on a parameterized query or stored procedure. You can pass the required data to a parameterized query by creating a Parameter object and adding it to the Parameter collection. The steps for using the Parameter collection are as follows:
Steps to use Parameter collection

5. Application examples of Command object
1. This is a simple employee basic situation management system. Its functions are: 1), add employee information; 2), change employee information; 3), delete employee information, and retrieve employee information. It contains seven pages and a database. They are:
1). Main page: index.asp
2). Add data page: add.htm
3). Save the add data page: add.asp
4). Change data page: Update.asp
5). Save the changed data page: Update1.asp
6). Delete record page: Detele.asp
7). Retrieve employee information page: shousho.asp
8) Database: RSGL.mdb. Use the "Employee Basic Information Table" in this database.
2. The code of each page is as follows:
1), main page: index.asp. The functions of this page are:
a) Create two objects, Connection object and Recordset object, whose purpose is to connect to the database and return a recordset;
b) Create a table and use the do while loop statement to display each record in the table;
c) Create three hyperlinks, one to connect to the add data page, another to connect to the change data page through the specified employee name, and one to connect to the delete page through the specified employee name.

<% @ Language="VBScript" %>
<html>
<head><title>员工基本情况管理系统</title></head>
<body background="../../../images/bj1.jpg">
<%
'****************创建两个对象(连接对象、记录集对象)*********************
dim cnn,rst
set cnn=Server.CreateObject("ADODB.Connection")
set rst=Server.CreateObject("ADODB.Recordset")
'指定连接字符串,
cnn.ConnectionString="PROVIDER=Microsoft.jet.OLEDB.4.0;Data Source=" & server.MapPath("../rsgl.mdb")
cnn.Open
sSQL="select * from 员工基本情况表"
'rst.Open sSQL,cnn,1,1
set rst=cnn.Execute(sSQL,,adCmdText)
%>
<!--************创建一个表格,用以显示数据库中的各条记录***********-->
<table align="center" border="1">
<caption><h3>教职员工基本信息表</h3></caption>
<tr colspan="5"><td><a href="shousho.asp">查询记录</a>||<a href="add.htm">添加记录</a></td></tr>
<!--显示各字段名-->
<tr><td align="center">员工姓名</td><td align="center">所在部门</td><td align="center">家庭住址</td><td align="center">家庭电话</td><td align="center">Email</td><td align="center">状态</td>
<%
'使用do while循环语句将各条记录显示出来。
do while Not rst.eof
t1=rst("员工姓名")
t2=rst("所在部门")
t3=rst("家庭住址")
t4=rst("家庭电话")
t5=rst("Email")
tt="<tr align='center'><td>" & t1 & "</td><td>" &t2& "</td><td>" &t3& "</td><td>" &t4& "</td><td>" &t5& "</td><td>"
tt=tt & "<a href=Update.asp&#63;id=" & t1 & ">修改</a><a href=Delete.asp&#63;id=" & t1 & ">||删除</a></td></tr>"
response.write tt
rst.MoveNext
loop
cnn.Close
Set cnn=Nothing
%>
</table>
</body>
</html>
Copy after login

2), add data page: add.htm.

This page consists of a form whose function is to submit data to the save and add data page (add.asp).

<html>
<head><title>添加记录</title></head>
<body background="../../../images/bj1.jpg">
<div align="center">
<form name="form1" method="post" action="add.asp">
<table align="center" border="1">
<tr><td colspan="2" align="center">员工基本情况表</td></tr>
<tr><td align="right">员工姓名:</td>
<td><input type="text" name="txtName"></td></tr>
<tr><td align="right">所在部门:</td>
<td><input type="text" name="txtDepartment"></td></tr>
<tr><td align="right">家庭住址:</td>
<td><input type="text" name="txtAddr"></td></tr>
<tr><td align="right">家庭电话:</td>
<td><input type="text" name="txtTel"></td></tr>
<tr><td align="right">Email:</td>
<td><input type="text" name="txtemail"></td></tr>
<tr><td align="center"><input type="submit" value="提交"></td>
<td align="center"><input type="reset" value="全部重写"></td></tr>
</table>
</form>
</div>
</body>
</html>
Copy after login

3). Save the add data page: add.asp.

The functions of this page are:

a), use the Request object to obtain the value submitted from the add.htm page;

b), create three objects (connection object, recordset object and instruction object) and five parameters, and execute the INSERT insertion command by calling the parameters.

<% @ Language="VBScript" %>
<html>
<head>
<title>添加记录</title>
</head>
<body background="../../../images/bj1.jpg">
<!-- #include virtual ="/adovbs.inc" -->
<%
'****************创建三个对象(连接对象、记录集对象和指令对象)和五个参数*********************
dim cnn,rst,cmd
set cnn=Server.CreateObject("ADODB.Connection")
set rst=Server.CreateObject("ADODB.Recordset")
set cmd=Server.CreateObject("ADODB.Command")
'指定连接字符串,
cnn.ConnectionString="PROVIDER=Microsoft.jet.OLEDB.4.0;Data Source=" & server.MapPath("../rsgl.mdb")
cnn.Open
'设置ActiveConnection属性,使Command对象与打开的连接相关联
set cmd.ActiveConnection=cnn
'指定传送给数据提供者的命令文本是一条SQL语言。
cmd.CommandType=adCmdText
cmd.CommandText="INSERT INTO 员工基本情况表(员工姓名,所在部门,家庭住址,家庭电话,Email) values(&#63;,&#63;,&#63;,&#63;,&#63;)"
'创建五个Parameter对象
set PrmName=cmd.CreateParameter("员工姓名",adVarChar,adParamInput,10)
set PrmDepartment=cmd.CreateParameter("所在部门",adVarChar,adParamInput,10)
set PrmAddr=cmd.CreateParameter("家庭住址",adVarChar,adParamInput,12)
set PrmTel=cmd.CreateParameter("家庭电话",adVarChar,adParamInput,15)
set PrmEmail=cmd.CreateParameter("Email",adVarChar,adParamInput,20)
'将parameter对象添加到Parameters集合中。
cmd.Parameters.Append prmName
cmd.Parameters.Append prmDepartment
Cmd.Parameters.Append prmAddr
Cmd.Parameters.Append prmTel
Cmd.Parameters.Append prmEmail
'使用表单值设置参数值
PrmName.Value=Request.Form("txtName")
PrmDepartment.Value=Request.Form("txtDepartment")
PrmAddr.Value=Request.Form("txtAddr")
PrmTel.Value=Request.Form("txtTel")
PrmEmail.Value=Request.Form("txtEmail")
'执行INSERT插入命令
cmd.Execute
%>
<!--用表格显示记录。-->
<table align="center" border="1">
<tr><td colspan="2" align="center">员工基本情况表</td></tr>
<tr><td align="right" width="130">员工姓名:</td>
<td width="200"><%=prmName.Value %></td></tr>
<tr><td align="right">所在部门:</td>
<td><%=prmDepartment.Value %></td></tr>
<tr><td align="right">家庭住址:</td>
<td><%=prmAddr.Value %></td></tr>
<tr><td align="right">家庭电话:</td>
<td><%=prmTel.Value %></td></tr>
<tr><td align="right">Email:</td>
<td><%=prmEmail.Value %></td></tr>
</table>
<center><p><p><p>
<hr width="505" color="#cc9999">
 <p><p><h3>记录添加成功!</h3>
<p><a href="add.htm">返回记录添加表单</a>||<a href="index.asp">返回主页</a>
</center>
</body>
</html>
Copy after login


4), change data page: Update.asp. The functions of this page are:

a), create two objects, Connection object and Recordset object, whose purpose is to connect to the database and return a recordset;

b), create a form whose purpose is to submit the changed data.

cnn.ConnectionString="PROVIDER=Microsoft.jet.OLEDB.4.0;Data Source=" & server.MapPath("../rsgl.mdb")
cnn.Open
sSQL="select * from 员工基本情况表 where 员工姓名='" & a & "'"
'rst.Open sSQL,cnn,1,1
set rst=cnn.Execute(sSQL,,adCmdText)
%>
<html>
<head><title>更改记录</title></head>
<body background="../../../images/bj1.jpg">
<div align="center">
<!--*****************创建一个表单****************************************-->
<form name="form1" method="post" action="Update1.asp">
<table align="center" border="1">
<tr><td colspan="2" align="center">员工基本情况表</td></tr>
<tr><td align="right">员工姓名:</td>
<td><input type="text" name="txtName" value=<%=rst("员工姓名")%> readonly></td></tr>
<tr><td align="right">所在部门:</td>
<td><input type="text" name="txtDepartment" value=<%=rst("所在部门")%>></td></tr>
<tr><td align="right">家庭住址:</td>
<td><input type="text" name="txtAddr" value=<%=rst("家庭住址")%>></td></tr>
<tr><td align="right">家庭电话:</td>
<td><input type="text" name="txtTel" value=<%=rst("家庭电话")%>></td></tr>
<tr><td align="right">Email:</td>
<td><input type="text" name="txtemail" value=<%=rst("Email")%>></td></tr>
<tr><td align="center"><input type="submit" value="提交"></td>
<td align="center"><input type="reset" value="全部重写"></td></tr>
</table>
</form>
</div>
</body>
</html>
Copy after login

5)、保存更改数据页面:Update2.asp。

该页面的功能有:

a)、使用Request对象获取从Update.asp页面提交的值;

b)、创建二个对象(连接对象、记录集对象);

c)、通过表格显示更改后的记录。

<% @ Language="VBScript" %>
<%
'*****************从提交表单中提取数值***************************
Dim Name,Department,Addr,Tel,Email
Name=Trim(Request.Form("txtName"))
Department=Trim(Request.Form("txtDepartment"))
Addr=Trim(Request.Form("txtAddr"))
Tel=Trim(Request.Form("txtTel"))
Email=Trim(Request.Form("txtEmail"))
%>
<html>
<head>
<title>更改记录</title>
</head>
<body>
<!-- #include virtual ="/adovbs.inc" -->
<%
'****************创建二个对象(连接对象、记录集对象)*********************
dim cnn,rst,cmd
set cnn=Server.CreateObject("ADODB.Connection")
set rst=Server.CreateObject("ADODB.Recordset")
'指定连接字符串,
cnn.ConnectionString="PROVIDER=Microsoft.jet.OLEDB.4.0;Data Source=" & server.MapPath("../rsgl.mdb")
cnn.Open
sSQL="update 员工基本情况表 set 所在部门='" & Department & "',家庭住址='" & Addr & "',家庭电话='" & Tel & "',Email='" & Email & "' where 员工姓名='" & name & "'"
rst.Open sSQL,cnn,1,2
set rst=nothing
%>
<!--**************************用表格显示记录。**********************-->
<table align="center" border="1">
<tr><td colspan="2" align="center">员工基本情况表</td></tr>
<tr><td align="right" width="130" align="center">员工姓名:</td>
<td width="200"><%=Name %></td></tr>
<tr><td align="right">所在部门:</td>
<td><%=Department %></td></tr>
<tr><td align="right">家庭住址:</td>
<td><%=Addr %></td></tr>
<tr><td align="right">家庭电话:</td>
<td><%=Tel %></td></tr>
<tr><td align="right">Email:</td>
<td><%=Email %></td></tr>
</table>
<center>
<p><hr width="505" color="#cc9999">
<h3>记录更改成功!</h3>
<p><a href="index.asp">返回首页</a>
</center>
</body>
</html>
Copy after login


6)、删除数据页面:Detele.asp。

a)、使用Request对象获取要删除的员工姓名;

b)、创建三个对象(连接对象、记录集对象和指令对象)和一个参数,通过参数指定的值删除记录;

c)、给出删除成功提示框。

<title>更改记录</title>
</head>
<body background="../../../images/bj1.jpg">
<!-- #include virtual ="/adovbs.inc" -->
<%
'****************创建三个对象(连接对象、记录集对象和指令对象)和一个参数*********************
dim cnn,rst,cmd
set cnn=Server.CreateObject("ADODB.Connection")
set rst=Server.CreateObject("ADODB.Recordset")
set cmd=Server.CreateObject("ADODB.Command")
'指定连接字符串,
cnn.ConnectionString="PROVIDER=Microsoft.jet.OLEDB.4.0;Data Source=" & server.MapPath("../rsgl.mdb")
cnn.Open
'设置ActiveConnection属性,使Command对象与打开的连接相关联
set cmd.ActiveConnection=cnn
'指定传送给数据提供者的命令文本是一条SQL语言。
cmd.CommandType=adCmdText
cmd.CommandText="Delete from 员工基本情况表 where 员工姓名=&#63; "
'创建一个Parameter对象
set PrmName=cmd.CreateParameter("员工姓名",adVarChar,adParamInput,10)
'将parameter对象添加到Parameters集合中。
cmd.Parameters.Append prmName
'使用表单值设置参数值
PrmName.Value=Name
'执行Delete删除命令
cmd.Execute
%>
 <p><p><p>
<hr width="505" color="#cc9999">
<center><h3>记录删除成功!</h3>
<p><a href="index.asp">返回主页</a>
</center>
</body>
</html>
Copy after login


7)、检索员工资料页面 :shousho.asp。

a)、使用一个列表框用以提交检索的条件;

b)、创建三个对象(连接对象、记录集对象和指令对象)和一个参数,使用 Parameter 对象的 Value 属性将表单提交的值赋给参数;

c)、使用for 循环语句将检索出的记录集中的每一条记录都通过表格显示出来。

<% @ Language="VBScript" %>
<html>
<head>
<title>使用参数化检索记录</title>
</head background="../../../images/bj1.jpg>
<body background="../../../images/bj1.jpg">
<!--*************开始创建表单*****************-->
<div align="center">
<p>查询各部门员工的基本情况
<form name="form1" method="post" action="Shousho.asp">
选择部门:
<select size="1" name="department">
<option selected value="all">全部记录</option>
<option value="教务处">教务处</option>
<option value="英语教研室">英语教研室</option>
<option value="语文教研室">语文教研室</option>
<option value="数学教研室">数学教研室</option>
<option value="财务处">财务处</option>
</select>
<input type="submit" value="提交">||<a href="index.asp">返回主页</a>
</form>
<!-- #include virtual ="/adovbs.inc" -->
<%
'****************创建三个对象(连接对象、记录集对象和指令对象)和一个参数*********************
dim cnn,rst,cmd,i
set cnn=Server.CreateObject("ADODB.Connection")
set rst=Server.CreateObject("ADODB.Recordset")
set cmd=Server.CreateObject("ADODB.Command")
'指定连接字符串,
cnn.ConnectionString="PROVIDER=Microsoft.jet.OLEDB.4.0;Data Source=" & server.MapPath("../rsgl.mdb")
cnn.Open
'创建一个Parameter对象
set PrmDepartment=cmd.CreateParameter("所在部门",adVarChar,adParamInput,10)
'将Parameter对象添加到Prmameters集合中
cmd.Parameters.Append prmDepartment
'将用户提交的所在部门名称作为parameter对象的值
prmDepartment.Value=Request.Form("department")
'指定传送给数据提供者的命令文本是一条SQL语言。
cmd.CommandType=adCmdText
'设置ActiveConnection属性,使Command对象与打开的连接相关联
set cmd.ActiveConnection=cnn
'******如果没有提交所在部门名称,或选择所有部门,则显示所有记录,否则按参数进行查询。****************
if PrmDepartment.Value="" or Request.Form("department")="all" then
cmd.CommandText="select * from 员工基本情况表"
Else
'便用参数化查询语句作为命令文本
cmd.CommandText="select * from 员工基本情况表 where 所在部门=&#63;"
end if
'向服务器发送SQL语句并返回一个记录集
Set rst=cmd.Execute
'-----------如果记录集不存在,则显示一条提示信息,否则,列出符合条件的记录。----------
if rst.EOF then
%>
<p><b>没有找到符合条件的记录!</b></p>
<% else %>
<table border="1">
<tr>
<!--用for 循环语句列出字段名。-->
<% for i=0 to rst.Fields.Count-1 %>
<th><%=rst(i).Name %></th>
<% next %>
<!--用while条件语句列出每条记录-->
<% while not rst.eof %>
<tr>
<%
'用for循环语句列出某条记录的各字段的值。
for i=0 to rst.Fields.Count-1
'如果字段值为空,则显示一个空格
if IsNull(rst(i)) then
%>
<td> </td>
<% else %>
<td nowrap><% =rst(i) %></td>
<% end if %>
<% next %>
</tr>
<%
rst.MoveNext
wend
%>
</table>
<% end if %>
</div>
</body>
</html>
Copy after login

以上就是关于ASP基础知识Command对象的入门教程,希望对大家的学习有所帮助,多多交流探讨。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template