學會在ASP中使用預存程序

來源:互聯網
上載者:User

學習使用預存程序(Stored Procedure),是ASP程式員的必須課之一。所有的大型資料庫都支援預存程序,比如Oracle、MS SQL等,(但MS Access不支援,不過,在Access裡可以使用參數化的查詢)。
使用預存程序有許多好處,它可以封裝複雜的資料邏輯,充分發揮大型資料庫本身的優勢。我們知道,ASP並不適合做複雜的資料運算,而通過OLD DB訪問資料庫,由於資料需要在ASP和資料庫之間傳遞,相當消耗系統資源。事實上,如果資料庫僅僅起著資料存放區的作用,那麼它的功能是遠遠沒有得到利用的。
關於如何建立預存程序,請參考MS SQL的相關文檔。
本文介紹預存程序如何在ASP中運用。
簡單的一個SQL語句:
select ID,Name,Picture,Time,Duty from employ 
我們可以建立一個預存程序:
CREATE PROCEDURE sp_employ
AS
select ID,Name,Picture,Time,Duty from employ 
Go
 

而SQL語句:
select ID,Name,Picture,Time,Duty from employ where ID=10230
對應的預存程序是:(用Alter替換我們已有的預存程序)
ALTER PROCEDURE sp_employ
@inID  int
AS
select ID,Name,Picture,Time,Duty from employ  where ID=@inID
Go
 

下面對比一下SQL和預存程序在ASP中的情況。首先看看直接執行SQL的情況:
<%
dim Conn, strSQL, rs
set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open  "DSN=webData;uid=user;pwd=password" 
strSQL = " select ID,Name,Picture,Time,Duty from employ "
Set rs = Conn.Execute(strSQL) 
%> 

再看看如何執行Stored Procedure:
<%
dim Conn, strSQL, rs
set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open  "DSN=webData;uid=user;pwd=password" ’make connection
strSQL = "sp_employ"
Set rs = Conn.Execute(strSQL) 
%> 

而執行帶參數的Stored Procedure也是相當類似的:
<%
dim Conn, strSQL, rs, myInt
myInt = 1 
set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open  "DSN=webData;uid=user;pwd=password"
strSQL = "sp_myStoredProcedure " & myInt
Set rs = Conn.Execute(strSQL) 
%> 

你可能覺得在ASP中使用預存程序原來是這樣的簡單。對!就是這麼簡單。

轉自: http://goaler.xicp.net/ShowLog.asp?ID=503

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.