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

enchanter 发布于2004-5-7 23:40 833 次浏览 1 位用户参与讨论   [复制分享主题]
<P>1、最简单的如下
/ X, J+ c+ i, r           Dim objConn! j' O$ |' R- a0 V
    Set objConn = Server.CreateObject("ADOBD.Connection"), V" }0 R! s+ t( _# d1 ^0 K
    objConn.Open Application("Connection_String")+ a. ^; ~  R+ B# y' C
    'Call the stored procedure to increment a counter on the page+ ]/ M5 @0 e4 q* o
    objConn.Execute "exec sp_AddHit"
: U6 X. x0 g/ l8 c1 R没有参数,没有返回,没有错误处理,就是这个了</P>3 B. R/ d- D- s; w; G0 U
<P>2、带参数的一种调用
( F2 O; z; b  x9 Y0 v9 D! qobjConn.Execute "exec sp_AddHit 'http://www.aspalliance.com', 1"
, u1 q" N0 T  S5 a3 Y0 V请注意分割参数,该方法也不返回记录</P>/ I. P1 A+ T; D  \: a$ y4 r
<P>3、返回记录的2 T$ w! c6 q- _1 ]6 \7 t/ _- \
          Dim objConn$ a, }; r. A9 R' p( X1 v% }. q
    Dim objRs$ e; U0 ~9 ?! u6 ^+ b
    Set objConn = Server.CreateObject("ADOBD.Connection")  K/ ?! I6 h7 f0 S
    Set objRs = Server.CreateObject("ADOBD.Recordset")  j, }  p. a  A; [  d$ O! N9 c5 t
    objConn.Open Application("Connection_String")
. }$ \, }8 s3 v+ ?% v/ i) k    'Call the stored procedure to increment a counter on the page: l! \$ p" {% F/ g- E1 [
    objRs.Open objConn, "exec sp_ListArticles '1/15/2001'"# u+ H3 A$ j' f4 `7 d0 k
    'Loop through recordset and display each article
6 I% l1 x9 c6 O+ H: `+ C! e3 v4、……- I% S, k' e1 d8 W* ^4 [; G
          Dim objConn
7 ]6 `# }; k/ Y3 A3 Y' ~5 Y          Dim objCmd</P>
" k' s8 `5 k) f& c% h5 Y<P>'Instantiate objects3 W  J0 k6 v8 B# {
Set objConn        = Server.CreateObject("ADODB.Connection")
! k  G- a3 ^# m  pset objCmd        = Server.CreateObject("ADODB.Command")$ S  Y4 h5 m1 v8 L; i
conn.Open Application("ConnectionString")</P>
9 a6 X8 {3 n8 }1 ^3 q<P>With objCmd
9 e: ^! E8 F/ j5 A" O    .ActiveConnection = conn 'You can also just specify a connection string here
, Y& ]/ m1 Y( s. G# p: k& g% \    .CommandText = "sp_InsertArticle"   O, w$ c3 P% |$ ]
    .CommandType = adCmdStoredProc 'Requires the adovbs.inc file or typelib meta tag
: W7 C! f/ A& n! w  H1 @    2 A( J9 |7 A9 y; @5 q( D
    'Add Input Parameters9 `4 j4 P# I1 d+ Q
    .Parameters.Append .CreateParameter("@columnist_id", adDouble, adParamInput, , columnist_id)
3 b3 c7 ^& {& @) L% Q. r    .Parameters.Append .CreateParameter("@url", adVarChar, adParamInput, 255, url)+ E' b! }' b& b' L6 M# o4 b  Y
    .Parameters.Append .CreateParameter("@title", adVarChar, adParamInput, 99, url)
3 X6 h- G( o) z# }    .Parameters.Append .CreateParameter("@description", adLongVarChar, _9 ^* u4 `) t/ o8 N, J5 W' r7 k5 S( E
        adParamInput, 2147483647, description)
) j" y5 ]7 a9 j9 U, r$ x/ d   
; E! G  i0 R0 b# M7 ^    'Add Output Parameters
, l1 q" W+ N6 `' E" ^    .Parameters.Append .CreateParameter("@link_id", adInteger, adParamOutput, , 0)
9 s* S( P2 Z. `        4 _+ U6 t$ V+ o0 G5 Q
    'Execute the function
6 U4 d# G* a0 A: M; E4 G: z    'If not returning a recordset, use the adExecuteNoRecords parameter option
+ J  @% ~  D2 T/ m    .Execute, , adExecuteNoRecords: u! i' T1 W+ ^
    link_id = .Parameters("@link_id")
, j5 D  }+ F2 |9 EEnd With</P>: d/ B# N: s& V$ j
<P>5、存储过程的代码
9 K, X' V7 w  T* GCreate PROCEDURE dbo.sp_InsertArticle
0 q0 C) ]# k2 F7 r$ h(
+ N: @8 ~( |8 H7 n4 n3 l# m    @columnist_id int,
/ l6 d) e) h) u+ P* p2 q    @url varchar(255),
% Q9 i1 A: b. s+ u+ L! d    @title varchar(99),
/ X: m$ ?5 o: b1 r' G0 S2 Y    @description text
* z; A- ~; ~" J/ I       @link_id int OUTPUT
7 X& P0 M# b' A" h" n6 \1 L)+ a: M" {5 K# H/ f# t& e8 s
AS
4 [' t3 Q, o( K$ e9 d) w: FBEGIN8 ?, t$ X3 c5 U  _0 k3 C
    INSERT INTO dbo.t_link    (columnist_id,url,title,description)
* `* K2 y- E$ P& D2 g- w/ z    VALUES (@columnist_id,@url,@title,@description)</P>
' Y! A1 z0 z7 J; j<P>    SELECT @link_id = @@IDENTITY* S, h3 d, L- q* n
END</P>

已有(1)人评论

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

本版积分规则

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