网络编程
位置:首页>> 网络编程>> Asp编程>> 如何在ASP中使用SQL存储过程

如何在ASP中使用SQL存储过程

  发布时间:2008-02-26 12:09:00 

标签:存储过程

学习使用存储过程(Stored Procedure),是ASP程序员的必须课之一。所有的大型数据库都支持存储过程,比如Oracle、MS SQL等,(但MS Access不支持,不过,在Access里可以使用参数化的查询)。

 DB访问数据库,由于数据需要在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中使用存储过程原来是这样的简单。对!就是这么简单。

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com