.Connection") '创建连接数据库的对象
conn.Open "course_dsn","course_user","course_password" '使用该对象连接数据库
conn.execute "delete from user_info"
%>
效果:
user_info表中所有数据被删除
思考:两种方式有和区别?各应用于什么场合?
3.如何使用事务处理、存储过程和视图?
⑴.使用存储过程
①.定义好存储过程
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
conn.close
set conn=nothing
%>
效果:
访问http://10.1.43.238/course/use_proc.asp?user_name=ahyi&password=ttt时,出现如下
普通的调用方法:
12
返回纪录集,可以使用recordcount等属性:
ahyi
tet
tuth
调用带输出参数的存储过程:
2
调用带两个输入参数和返回值的存储过程:
1
注意:若存储过程无参数,则调用的sql语句直接为存储过程名,一个参数为“存储过程名 参数”,若是多个参数,则“存储过程名 参数1,参数2,……,参数n”;如果在sql语句中加入exec,则在返回的记录集中可以使用recordcount等属性;如果想获得存储过程的返回值或输出参数,可以使用command对象。
⑵.使用事务处理
①.Asp内嵌的事务支持
例子:
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"
sql="user_info_2 '"&request.querystring("user_name")&"','"&request.querystring("password")&"'"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
set rs=nothing
conn.close
set conn=nothing
%>
讨论:两种方式的优劣?
⑶.使用视图
在数据库中定义好视图之后,在Asp中如同使用一个表一样来使用视图
4.一个数据库分页的例子
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
%>
思考:分页过程中使用了哪些额外的属性?
conn.Open "course_dsn","course_user","course_password" '使用该对象连接数据库
conn.execute "delete from user_info"
%>
效果:
user_info表中所有数据被删除
思考:两种方式有和区别?各应用于什么场合?
3.如何使用事务处理、存储过程和视图?
⑴.使用存储过程
①.定义好存储过程
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
conn.close
set conn=nothing
%>
效果:
访问http://10.1.43.238/course/use_proc.asp?user_name=ahyi&password=ttt时,出现如下
普通的调用方法:
12
返回纪录集,可以使用recordcount等属性:
ahyi
tet
tuth
调用带输出参数的存储过程:
2
调用带两个输入参数和返回值的存储过程:
1
注意:若存储过程无参数,则调用的sql语句直接为存储过程名,一个参数为“存储过程名 参数”,若是多个参数,则“存储过程名 参数1,参数2,……,参数n”;如果在sql语句中加入exec,则在返回的记录集中可以使用recordcount等属性;如果想获得存储过程的返回值或输出参数,可以使用command对象。
⑵.使用事务处理
①.Asp内嵌的事务支持
例子:
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"
sql="user_info_2 '"&request.querystring("user_name")&"','"&request.querystring("password")&"'"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
set rs=nothing
conn.close
set conn=nothing
%>
讨论:两种方式的优劣?
⑶.使用视图
在数据库中定义好视图之后,在Asp中如同使用一个表一样来使用视图
4.一个数据库分页的例子
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
%>
思考:分页过程中使用了哪些额外的属性?