在ASP程序中使用SQL Server存儲過程是常見的事,主要的作用是提高程序運行的時間,這裡介紹一下調用不同類型的存儲過程的方法。
一、最簡單的調用
1
<%
2
Dim objConn
3
Set objConn = Server.CreateObject("ADOBD.Connection")
4
objConn.Open Application("Connection_String")
5
'Call the stored procedure to increment a counter on the page
6
objConn.Execute "exec sp_AddHit"
7
%>(鼠標移到代碼上去,在代碼的頂部會出現四個圖標,第一個是查看源代碼,第二個是復制代碼,第三個是打印代碼,第四個是幫助)沒有參數,沒有返回,沒有錯誤處理,就是這個了
二、帶參數的一種調用
1
<%
2
objConn.Execute "exec sp_AddHit,'http://www.ASPbc.com', 1"
3
%>(鼠標移到代碼上去,在代碼的頂部會出現四個圖標,第一個是查看源代碼,第二個是復制代碼,第三個是打印代碼,第四個是幫助)注意分割參數,該方法也不返回記錄
三、帶返回記錄集的
01
<%
02
Dim objConn
03
Dim objRs
04
Set objConn = Server.CreateObject("ADOBD.Connection")
05
Set objRs = Server.CreateObject("ADOBD.Recordset")
06
objConn.Open Application("Connection_String")
07
'Call the stored procedure to increment a counter on the page
08
objRs.Open objConn, "exec sp_ListArticles '1/15/2001'"
09
'Loop through recordset and display each article
10
%>(鼠標移到代碼上去,在代碼的頂部會出現四個圖標,第一個是查看源代碼,第二個是復制代碼,第三個是打印代碼,第四個是幫助)
四、帶參數和返回記錄集的
01
<%
02
Dim objConn
03
Dim objCmd
04
05
'Instantiate objects
06
Set objConn = Server.CreateObject("ADODB.Connection")
07
set objCmd = Server.CreateObject("ADODB.Command")
08
conn.Open Application("ConnectionString")
09
10
With objCmd
11
.ActiveConnection = conn 'You can also just specify a connection string here
12
.CommandText = "sp_InsertArticle"
13
.CommandType = adCmdStoredProc 'Requires the adovbs.inc file or typelib meta tag
14
15
'Add Input Parameters
16
.Parameters.Append .CreateParameter("@columnist_id", adDouble, adParamInput, , columnist_id)
17
.Parameters.Append .CreateParameter("@url", adVarChar, adParamInput, 255, url)
18
.Parameters.Append .CreateParameter("@title", adVarChar, adParamInput, 99, url)
19
.Parameters.Append .CreateParameter("@description", adLongVarChar, _
20
adParamInput, 2147483647, description)
21
22
'Add Output Parameters
23
.Parameters.Append .CreateParameter("@link_id", adInteger, adParamOutput, , 0)
24
25
'Execute the function
26
'If not returning a recordset, use the adExecuteNoRecords parameter option
27
.Execute, , adExecuteNoRecords
28
link_id = .Parameters("@link_id")
29
End With
30
%>(鼠標移到代碼上去,在代碼的頂部會出現四個圖標,第一個是查看源代碼,第二個是復制代碼,第三個是打印代碼,第四個是幫助)
五、存儲過程代碼
vIEw source