不长见的ASP调用存储过程的技巧

enchanter 发布于2004-5-7 23:40 903 次浏览 1 位用户参与讨论   [复制分享主题]

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区

您需要 登录 才可以下载或查看,没有账号?註冊

x
<P>1、最简单的如下9 m6 u; \, R5 N1 _' O
           Dim objConn
+ p5 a5 L' W+ S% f! h. c* X9 b    Set objConn = Server.CreateObject("ADOBD.Connection")
7 o4 F) t' y: G& k    objConn.Open Application("Connection_String"). V. G' S( U& {
    'Call the stored procedure to increment a counter on the page
  Y% G% p' m2 M# j$ B5 X    objConn.Execute "exec sp_AddHit"" U0 {  |' {2 l% \- o9 i; C
没有参数,没有返回,没有错误处理,就是这个了</P>
8 F$ [: \8 k. s' R3 |4 j, _<P>2、带参数的一种调用5 M9 c1 u9 z/ h' V
objConn.Execute "exec sp_AddHit 'http://www.aspalliance.com', 1"
$ Z/ T$ o  n+ J" L8 i请注意分割参数,该方法也不返回记录</P>
3 S4 R1 g" ~( V- O9 p5 M4 z( M<P>3、返回记录的% G6 D) K4 Z0 I0 Q
          Dim objConn. L& v2 ~4 J' n! A, y
    Dim objRs* w6 Y( J# M+ J5 f8 C2 c$ g
    Set objConn = Server.CreateObject("ADOBD.Connection")
8 a: e  M' U( ]: R5 l) }% p    Set objRs = Server.CreateObject("ADOBD.Recordset")7 t! c! ~6 C1 n% B7 z  t# d
    objConn.Open Application("Connection_String")
" |, S' X3 M% B; Q    'Call the stored procedure to increment a counter on the page
1 l  `) U# U& u- p/ y    objRs.Open objConn, "exec sp_ListArticles '1/15/2001'"
" v7 |+ V2 Y" p7 g3 G    'Loop through recordset and display each article0 z! _9 @) \, E
4、……
% d* Q1 j5 _. B. {0 g' K          Dim objConn
! ]) H* b/ L; l8 R6 U, ]: [" d          Dim objCmd</P>4 i, b% V1 f, v$ F/ I
<P>'Instantiate objects) H. Z! W. i, D: h
Set objConn        = Server.CreateObject("ADODB.Connection")
, p' A2 ~2 j! _8 m- Lset objCmd        = Server.CreateObject("ADODB.Command")% _1 O6 X4 R, J
conn.Open Application("ConnectionString")</P>1 a( }& c) n5 U" _+ g, [. b
<P>With objCmd7 O/ ^4 n4 \) T! s- P- j% D
    .ActiveConnection = conn 'You can also just specify a connection string here$ t" y1 K( |8 S4 N2 ?: v
    .CommandText = "sp_InsertArticle"
2 S8 _0 S- c/ F9 `    .CommandType = adCmdStoredProc 'Requires the adovbs.inc file or typelib meta tag
; }7 @6 R/ [, C- u, M/ h& D   
! i4 x3 y" a  i$ j) t2 \: u) {! S  V    'Add Input Parameters
* [+ H5 R+ y1 P7 u    .Parameters.Append .CreateParameter("@columnist_id", adDouble, adParamInput, , columnist_id): c( A( x+ j- }" x/ B5 V- e
    .Parameters.Append .CreateParameter("@url", adVarChar, adParamInput, 255, url)
- J- o% Q- [# J. t( J9 C    .Parameters.Append .CreateParameter("@title", adVarChar, adParamInput, 99, url)
8 D/ ]% A9 A) t4 v; J    .Parameters.Append .CreateParameter("@description", adLongVarChar, _
2 O4 l! j2 w& [5 `. @5 x# i        adParamInput, 2147483647, description)/ q( a5 Q/ ~8 A. h
   
/ c$ E( a) x8 Q2 o5 A# ~* Y  w    'Add Output Parameters2 `2 a' `# |% r& ]2 y' h
    .Parameters.Append .CreateParameter("@link_id", adInteger, adParamOutput, , 0)7 c2 b+ z% {# j4 r( R2 ^3 y
          C# o* A3 e* c9 @3 s$ ~
    'Execute the function3 n  H) U/ ?5 M; x$ }: j! o& T
    'If not returning a recordset, use the adExecuteNoRecords parameter option
- _0 S+ ^2 b( L# t! K/ b5 n    .Execute, , adExecuteNoRecords7 v* S) w8 k1 q
    link_id = .Parameters("@link_id"). _6 p2 Z( o  N1 d' l
End With</P>
0 U* {, d$ ^/ `+ j5 L<P>5、存储过程的代码
' ]; B( u! Z. aCreate PROCEDURE dbo.sp_InsertArticle
/ \& W7 H" l- G1 e: Q(
7 J1 z5 ?/ q& T, K7 Q/ k    @columnist_id int,9 Q! _) S/ i* L
    @url varchar(255),
# j- q+ `) O/ p; m. H! y7 O! H    @title varchar(99),1 d2 [# B4 l$ V8 b! L5 Z& ^  y/ p9 Z
    @description text
1 k; f' U% A: ^       @link_id int OUTPUT+ P  N8 Z/ ^0 [2 W* @
)
' k& x* L2 G/ C* \9 l5 U+ k, ^AS
: `0 o! x/ q" JBEGIN
$ T$ t  a0 d7 {5 I1 W! C0 q    INSERT INTO dbo.t_link    (columnist_id,url,title,description)/ {8 g3 Q6 l2 i) C7 K! q
    VALUES (@columnist_id,@url,@title,@description)</P>
, g/ W" B8 V1 @" w9 |% z- R3 p<P>    SELECT @link_id = @@IDENTITY
4 W! ~- ?8 P5 _+ Z: A1 }3 k  Q) \END</P>

已有(1)人评论

andyxu 发表于 2004-7-4 04:29:13 | 显示全部楼层
好复杂啊
您需要登录后才可以回帖 登录 | 註冊

本版积分规则

快速
回复
返回
列表
返回
顶部