ASP调用存储过程的技巧

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.aspalliance.com', 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

ASP调用带参数存储过程的几种方式

最近有很多的朋友问到调用存储过程的问题,这里简单介绍几种ASP调用带参数存储过程的方法。

1 这也是最简单的方法,两个输入参数,无返回值:
set connection = server.createobject('adodb.connection')
connection.open someDSN
Connection.Execute 'procname varvalue1, varvalue2'

''将所有对象清为nothing,释放资源
connection.close
set connection = nothing


2 如果要返回 Recordset 集:
set connection = server.createobject('adodb.connection')
connection.open someDSN
set rs = server.createobject('adodb.recordset')
rs.Open 'Exec procname varvalue1, varvalue2',connection

''将所有对象清为nothing,释放资源
rs.close
connection.close
set rs = nothing
set connection = nothing


3 以上两种方法都不能有返回值,(Recordset除外),如果要得到返回值,需要用Command的方法。
首先说明,返回值有两种。一种是在存储过程中直接return一个值,就象C和VB的函数返回值那样;另一种是可以返回多个值,存
储这些值的变量名称需要在调用参数中先行指定。
这个例子要处理多种参数,输入参数,输出参数,返回记录集以及一个直接返回值(够全了吧?)
存储过程如下:

use pubs
GO

-- 建立存储过程
create procedure sp_PubsTest

-- 定义三个参数变量,注意第三个,特别标记是用于输出
@au_lname varchar (20),
@intID int,
@intIDOut int OUTPUT

AS

Select @intIDOut = @intID + 1

Select *
FROM authors
Where au_lname LIKE @au_lname + ''%''

--直接返回一个值
RETURN @intID + 2


调用该存储过程的asp程序如下:


此外还有其他方式,稍微偏门一些,以后慢慢再说
本文参考了多篇文章,这里不一一列出。

在Asp中使用存储过程

  为了提高Asp程序的效率,有时需要在Asp中使用使用Sql Server的存储技术,下面简单作一个介绍。

存储过程的建立

  这里只简单介绍如何在Sql Server的企业管理器中如何建立存储过程:

(1)打开企业管理器Enterprise manager

(2)选择服务器组(SQL Server Group)、服务器、数据库(Database)以及相就的数据库,鼠标右击对应数据库下的Stored Procdures项,在弹出的菜单中选择New Stored Procedure,在Stored Procedures Properties中输入建立存储过程的语句。下面是一个例子:


  Create PROCEDURE proctest @mycola Char(10),@mycolb Char(10),@mycolc text  AS

  Insert into chatdata (mycola,mycolb,mycolc) values(@mycola,@mycolb,@mycolc)

在Sql Server的文档中它的语法为:

  Create PROC[EDURE] procedure_name [;number]   [

  {@parameter data_type} [VARYING] [= default] [OUTPUT]   ]

  [,...n]  [WITH    {   RECOMPILE   | ENCRYPTION

  | RECOMPILE, ENCRYPTION   }  ]  [FOR REPLICATION]  AS

   sql_statement [...n]

  如果你对Sql语法不熟悉,可以使用Check Syntax来检查语法。在上例中,表示建立存储过程名为mycola,带3个参数的存储过过程,其中第一个参数mycola数据类型为char,宽度10;第2个参数数据类型为char,宽度为10,第3个参数数据类型为text,在这里使用的是Sql Server的数据类型。

  存储过程建立后,下面就是如何在Asp程序中调用该存储过程:在Asp中调用存储过程 为了提高Asp程序的效率,有时需要在Asp中使用使用Sql Server的存储技术,下面简单作一个,在上面的增加参数的语句p.Append cm.CreateParameter('@mycolc',201,1,250)中,格式为:

p.Append cm.CreateParameter('参数名称',类型,方向,大小)

参许参数值的类型的意义如下:

名称值 整数值 功能


adDBTimeStamp 135 日期时间数据类型

adDecimal 14 十进制整数值

adDouble 5 双精度小数值

adError 10 系统错误信息

AdGUID 72 全域性唯一识别字(Globally unique identifier)

adDispath 9 COM/OLE自动对象(Automation Object)

adInteger 3 4字节有符号整数

adIUnknown 13 COM/OLE对象

adLongVarBinary 205 大型2字节值

adLongVarChar 201 大型字符串值

adLongVarWChar 203 大型未编码字符串

adNumeric 131 十进制整数值

adSingle 4 单精度浮点小数

adSmallInt 2 2字节有符号整数

adTinyInt 16 1字节有符号整数

adUnsignedBigInt 21 8字节无符号整数

adUnsignedInt 19 4字节无符号整数

adUnsignedSmallInt 18 2字节无符号整数

adUnsignedTinyInt 17 1字节无符号整数

adUserDefined 132 用户自定义数据类型

adVariant 12 OLE对象

adVarBinary 204 双字节字符变量值

adVarChar 200 字符变量值

advarchar 202 未编码字符串变量值

adWchar 130 未编码字符串


方向值的意义如下:


名称值 整数值 功能


adParamInput 1 允许数据输入至该参数当中

adParamOutput 2 允许数据输出至该参数当中

adParamInputOutput 3 允许数据输入、输出至该参数当中

adparamReturnValue 4 允许从一子程序中返回数据至该参数当中

更多详细资源请参考Sql Server的文档和IIS的文档资源。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值