ASP调用存储过程的技巧
程序员文章站
2024-02-07 13:57:52
1、最简单的如下 dim&n...
1、最简单的如下
dim objconn
set objconn = server.createobject("adobd.connection")
objconn.open application("connection_string")
'call the stored procedure to increment a counter on the page
objconn.execute "exec sp_addhit"
没有参数,没有返回,没有错误处理,就是这个了
2、带参数的一种调用
objconn.execute "exec sp_addhit,'http://www.asp001.net', 1"
请注意分割参数,该方法也不返回记录
3、返回记录的
dim objconn
dim objrs
set objconn = server.createobject("adobd.connection")
set objrs = server.createobject("adobd.recordset")
objconn.open application("connection_string")
'call the stored procedure to increment a counter on the page
objrs.open objconn, "exec sp_listarticles '1/15/2001'"
'loop through recordset and display each article
4、……
dim objconn
dim objcmd
'instantiate objects
set objconn = server.createobject("adodb.connection")
set objcmd = server.createobject("adodb.command")
conn.open application("connectionstring")
with objcmd
.activeconnection = conn 'you can also just specify a connection string here
.commandtext = "sp_insertarticle"
.commandtype = adcmdstoredproc 'requires the adovbs.inc file or typelib meta tag
'add input parameters
.parameters.append .createparameter("@columnist_id", addouble, adparaminput, , columnist_id)
.parameters.append .createparameter("@url", advarchar, adparaminput, 255, url)
.parameters.append .createparameter("@title", advarchar, adparaminput, 99, url)
.parameters.append .createparameter("@description", adlongvarchar, _
adparaminput, 2147483647, description)
'add output parameters
.parameters.append .createparameter("@link_id", adinteger, adparamoutput, , 0)
'execute the function
'if not returning a recordset, use the adexecutenorecords parameter option
.execute, , adexecutenorecords
link_id = .parameters("@link_id")
end with
5、存储过程的代码
create procedure dbo.sp_insertarticle
(
@columnist_id int,
@url varchar(255),
@title varchar(99),
@description text
@link_id int output
)
as
begin
insert into dbo.t_link (columnist_id,url,title,description)
values (@columnist_id,@url,@title,@description)
select @link_id = @@identity
end
dim objconn
set objconn = server.createobject("adobd.connection")
objconn.open application("connection_string")
'call the stored procedure to increment a counter on the page
objconn.execute "exec sp_addhit"
没有参数,没有返回,没有错误处理,就是这个了
2、带参数的一种调用
objconn.execute "exec sp_addhit,'http://www.asp001.net', 1"
请注意分割参数,该方法也不返回记录
3、返回记录的
dim objconn
dim objrs
set objconn = server.createobject("adobd.connection")
set objrs = server.createobject("adobd.recordset")
objconn.open application("connection_string")
'call the stored procedure to increment a counter on the page
objrs.open objconn, "exec sp_listarticles '1/15/2001'"
'loop through recordset and display each article
4、……
dim objconn
dim objcmd
'instantiate objects
set objconn = server.createobject("adodb.connection")
set objcmd = server.createobject("adodb.command")
conn.open application("connectionstring")
with objcmd
.activeconnection = conn 'you can also just specify a connection string here
.commandtext = "sp_insertarticle"
.commandtype = adcmdstoredproc 'requires the adovbs.inc file or typelib meta tag
'add input parameters
.parameters.append .createparameter("@columnist_id", addouble, adparaminput, , columnist_id)
.parameters.append .createparameter("@url", advarchar, adparaminput, 255, url)
.parameters.append .createparameter("@title", advarchar, adparaminput, 99, url)
.parameters.append .createparameter("@description", adlongvarchar, _
adparaminput, 2147483647, description)
'add output parameters
.parameters.append .createparameter("@link_id", adinteger, adparamoutput, , 0)
'execute the function
'if not returning a recordset, use the adexecutenorecords parameter option
.execute, , adexecutenorecords
link_id = .parameters("@link_id")
end with
5、存储过程的代码
create procedure dbo.sp_insertarticle
(
@columnist_id int,
@url varchar(255),
@title varchar(99),
@description text
@link_id int output
)
as
begin
insert into dbo.t_link (columnist_id,url,title,description)
values (@columnist_id,@url,@title,@description)
select @link_id = @@identity
end