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 id="教职员工基本信息表">教职员工基本信息表</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?id=" & t1 & ">修改</a><a href=Delete.asp?id=" & t1 & ">||删除</a></td></tr>" response.write tt rst.MoveNext loop cnn.Close Set cnn=Nothing %> </table> </body> </html>
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>
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(?,?,?,?,?)" '创建五个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 id="记录添加成功">记录添加成功!</h3> <p><a href="add.htm">返回记录添加表单</a>||<a href="index.asp">返回主页</a> </center> </body> </html>
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>
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 id="记录更改成功">记录更改成功!</h3> <p><a href="index.asp">返回首页</a> </center> </body> </html>
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 员工姓名=? " '创建一个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 id="记录删除成功">记录删除成功!</h3> <p><a href="index.asp">返回主页</a> </center> </body> </html>
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 所在部门=?" 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>
以上就是关于ASP基础知识Command对象的入门教程,希望对大家的学习有所帮助,多多交流探讨。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools