1.Asp是Active Server Pages的简称,是解释型的脚本语言环境; 2.Asp的运行需要Windows操作系统,9x下需要安装PWS;而NT/2000/XP则需要安装Internet Information Server(简称IIS); 3.Asp和jsp的脚本标签是“<%%>”,php的则可以设定为多种; 4.Asp的注释符号是“'”; 5.使用附加组件,可以扩展Asp的功能。 from www.VeVb.com
例子:
HelloWorld_1.asp <%="Hello,world"%>
效果: Hello,world
HelloWorld_2.asp <% for i=1 to 10 response.write "Hello,world" next %>
'判断用户输入是否正确 if user_id="" then response.write "User_id is null,please check it" response.end end if if user_name="" then response.write "User_name is null,please check it" response.end end if
效果: 当访问http://10.1.43.238/course/request_url.asp?user_name=j时: User_id is null,please check it 当访问http://10.1.43.238/course/request_url.asp?user_name=j&user_id=my_id时: my_id j
'判断用户输入是否正确 if user_id="" then response.write "User_id is null,please check it" response.end end if if user_name="" then response.write "User_name is null,please check it" response.end end if
use_db_1.asp <% Set conn=Server.CreateObject("ADODB.Connection") '创建连接数据库的对象 conn.Open "course_dsn","course_user","course_password" '使用该对象连接数据库 Set rs=Server.CreateObject("ADODB.RecordSet") '创建记录集对象 rs.Open "select * from user_info",conn,1,1 '使用记录集对象打开数据库 if rs.recordcount>0 then '如果有记录 response.write "User_id User_name<br>" for i=1 to rs.recordcount '循环读取所有纪录 response.write rs("id")&" "&rs("user_name")&"<br>" '向浏览器输出纪录的字段 rs.movenext '指针下移一行 if rs.eof then exit for '如果到达记录集底部则退出循环 next end if %>
效果: User_id User_name 1 ahyi 3 test
⑵.只使用connection操作数据库:
use_db_2.asp <% Set conn=Server.CreateObject("ADODB.Connection") '创建连接数据库的对象 conn.Open "course_dsn","course_user","course_password" '使用该对象连接数据库 conn.execute "delete from user_info" %>
CREATE PROCEDURE [output_1] @sid int output AS set @sid=2
CREATE PROCEDURE [return_1] (@user_name varchar(40),@password varchar(20)) AS if exists(select id from user_info where user_name=@user_name and password=@password) return 1 else return 0
CREATE PROCEDURE [user_info_1] (@user_name varchar(40),@password varchar(20)) AS select id from user_info where user_name=@user_name and password=@password
CREATE PROCEDURE [user_info_2] (@user_name varchar(40),@password varchar(20)) AS SET XACT_ABORT ON BEGIN TRANSACTION delete from user_info where user_name=@user_name and password=@password COMMIT TRANSACTION SET XACT_ABORT OFF
CREATE PROCEDURE [user_info_3] AS select * from user_info
②.在Asp中调用
use_proc.asp <!-- #include virtual="/adovbs.inc" --> <% Set conn=Server.CreateObject("ADODB.Connection") conn.Open "course_dsn","course_user","course_password"
'使用recordset调用带两个输入参数和返回纪录集的存储过程 'CREATE PROCEDURE [user_info_1] '(@user_name varchar(40),@password varchar(20)) 'AS 'select id from user_info where user_name=@user_name and password=@password response.write "普通的调用方法:<br>" set rs=server.createobject("adodb.recordset") sql="user_info_1 '"&request.querystring("user_name")&"','"&request.querystring("password")&"'" rs.open sql,conn,1,1 response.write rs("id")&"<br>" rs.close
'使用recordset调用无输入参数,返回纪录集的存储过程,可以使用recordcount等属性 'CREATE PROCEDURE [user_info_3] AS 'select * from user_info response.write "<br>返回纪录集,可以使用recordcount等属性:" sql="exec user_info_3" rs.open sql,conn,1,1 for i=1 to rs.recordcount response.write "<br>"&rs("user_name") rs.movenext next rs.close set rs=nothing
'使用command调用带输出参数的存储过程 'CREATE PROCEDURE [output_1] '@sid int output 'AS 'set @sid=2 response.write "<br><br>调用带输出参数的存储过程:<br>" set cmd=server.createobject("adodb.command") cmd.activeconnection=conn cmd.commandtext = "output_1" cmd.parameters.append cmd.createparameter("@sid",adinteger,adparamoutput) cmd("@sid")=10 cmd.execute() bbb=cmd("@sid") response.write bbb&"<br>" set cmd=nothing
'使用command调用带两个输入参数和返回值的存储过程 'CREATE PROCEDURE [return_1] '(@user_name varchar(40)) 'AS 'if exists(select id from user_info where user_name=@user_name) 'return 1 'else 'return 0 response.write "<br>调用带两个输入参数和返回值的存储过程:<br>" set cmd=server.createobject("adodb.command") cmd.activeconnection=conn cmd.commandtype = adcmdstoredproc cmd.commandtext = "return_1" cmd.parameters.append cmd.createparameter("@return_value",adinteger,adparamreturnvalue) cmd.parameters.append cmd.createparameter("@user_name",advarchar,adparaminput,40) cmd.parameters.append cmd.createparameter("@password",advarchar,adparaminput,20) cmd("@user_name")="tuth" cmd("@password")="yyuyu" cmd.execute() rrr=cmd("@return_value") response.write rrr set cmd=nothing
例子: use_transaction_1.asp <% 'Asp中使用事务 Set conn=Server.CreateObject("ADODB.Connection") conn.Open "course_dsn","course_user","course_password" conn.begintrans '开始事务
sql="delete from user_info" set rs=server.createobject("adodb.recordset") rs.open sql,conn,3,3 if conn.errors.count>0 then '有错误发生 conn.rollbacktrans '回滚 set rs=nothing conn.close set conn=nothing response.write "交易失败,回滚至修改前的状态!" response.end else conn.committrans '提交事务 set rs=nothing conn.close set conn=nothing response.write "交易成功!" response.end end if %>
②.数据库级的事务
i.创建存储过程
CREATE PROCEDURE [user_info_2] (@user_name varchar(40),@password varchar(20)) AS SET XACT_ABORT ON BEGIN TRANSACTION delete from user_info where user_name=@user_name and password=@password COMMIT TRANSACTION SET XACT_ABORT OFF
ii.在Asp中调用
use_transaction_2.asp <% Set conn=Server.CreateObject("ADODB.Connection") conn.Open "course_dsn","course_user","course_password"
db_page.asp <% on error resume next Set conn=Server.CreateObject("ADODB.Connection") '创建连接数据库的对象 conn.Open "course_dsn","course_user","course_password" '使用该对象连接数据库 set rs=server.createObject("adodb.recordset") sql="select * from user_info order by id desc" rs.open sql,conn,1,1
if rs.recordcount>0 then '如果有记录 rs.pagesize=2 '每页最多显示2条纪录 '从URL获取当前要显示的页 page=cint(request("page")) '页面参数异常处理 if page="" then page=1 if page<1 then page=1 if page>= rs.pagecount then page=rs.pagecount rs.absolutepage=page '当前页为page参数指定的页 for i=1 to rs.pagesize '根据pagesize参数的大小循环显示当前页中的纪录 response.write "User_id:"&rs("id")&"<br>" response.write "User_name:"&rs("user_name")&"<br><br>" rs.movenext '纪录指针下移 if rs.eof then exit for '如果到达纪录集底部则退出循环 next end if
'显示翻页按钮 if page>1 then response.write "<a href="&request.servervariables("document_name")&"?page=1>第一页</a> " response.write "<a href="&request.servervariables("document_name")&"?page="&(page-1)&">上一页</a> " end if if page<>rs.pagecount then response.write "<a href="&request.servervariables("document_name")&"?page="&(page+1)&">下一页</a> " response.write "<a href="&request.servervariables("document_name")&"?page="&rs.pagecount&">最后一页</a> " end if response.write"页码:"&page&"/"&rs.pagecount
'关闭对象,释放内存 rs.close set rs=nothing conn.close set conn=nothing %>