VB6.0 调用存储过程的例子 (前言)
2024-07-21 02:20:55
供稿:网友
 
vb调用存储过程的例子 前言
(说明:以下代码摘自微软的msdn,经过测试没问题。)
 vb调用存储过程的方法很多,如利用ado对象的recordset.open方法,ado对象的connection.excute方法等,都可以获得记录集信息。本主题讨论的是使用parameter对象调用存储过程,而且可以获得许多意外的信息。
首先需要在sql server中建立一个存储过程。请确定已安装了sql server 2000的任何版本,且含有pubs数据库。
打开“查询分析器”,启动你本地的sqlserver,然后将以下的sql语句copy到所打开的查询编辑框中。按下“f5”键。ok,存储过程adotestrpe便生成了。
 
use pubs
 go
 if exists (select * from sysobjects where id =
 object_id('dbo.adotestrpe') and sysstat & 0xf = 4)
 drop procedure dbo.adotestrpe
go
 
 create procedure adotestrpe
 (
 @setrtn int=0 output,
 @r1num int=1,
 @p1num int=1,
 @e1num int=1,
 @r2num int=2,
 @p2num int=2,
 @e2num int=2
 )
 as
 declare @iloop int
 declare @printtext varchar(255)
 declare @ierrnum int
 
 /* check for no resultsets - needed to get the return value back */
 if @r1num + @r2num = 0 select null
 
 /* resultset 1 ******************************* */
 
 if @r1num > 0
 begin
 set rowcount @r1num
 select 'resultset 1' rsnum, title
 from pubs..titles
 set rowcount 0
 end
 
 /* must raise a default error context in which to return the print */
 /* statement */
 /* (if none present) since print statements are a severity level of */
 /*0. */
 if (@p1num > 0) and (@e1num = 0) raiserror ("raiserror.perror1",
 11, 2)
 
 if @p1num > 0
 begin
 select @iloop = 0
 while @iloop < @p1num
 begin
 select @iloop = @iloop + 1
 select @printtext = 'print.resultset.1: line ' +
 convert(char(2), @iloop)
 print @printtext
 end
 end
 
 if @e1num > 0
 begin
 select @iloop = 0
 while @iloop < @e1num
 begin
 select @iloop = @iloop + 1
 select @ierrnum = @iloop + 201000
 raiserror ("raiserror.resultset.1", 11, 2)
 end
 end
 
 /* resultset 2 ******************************* */
 
 if @r2num > 0
 begin
 set rowcount @r2num
 select 'resultset 2' rsnum, title
 from pubs..titles
 set rowcount 0
 end
 
 /* must raise a default error context in which to return the print */
 /* statement */
 /* (if none present) since print statements are a severity level of */
 /* 0. */
 if (@p2num > 0) and (@e2num = 0) raiserror ("raiserror.perror2",
 11, 2)
 
 if @p2num > 0
 begin
 select @iloop = 0
 while @iloop < @p2num
 begin
 select @iloop = @iloop + 1
 select @printtext = 'print.resultset.2: line ' +
 convert(char(2), @iloop)
 print @printtext
 end
 end
 
 if @e2num > 0
 begin
 select @iloop = 0
 while @iloop < @e2num
 begin
 select @iloop = @iloop + 1
 
 select @ierrnum = @iloop + 202000
 raiserror ("raiserror.resultset.2", 11, 2)
 end
 end
 
 /* return & output ************************************ */
 
 select @setrtn = -1
 return @setrtn
go 
 
运行完毕后,若不存在任何错误,请关闭“查询分析器”,然后继续下面的操作。否则可能是你的sql server 2000没有安装正确或copy时出了问题。
 
打开vb6.0,新建一个工程,默认有一个窗体form1(若没有请添加一个新的窗体,命名为form1),在该窗体中添加一个commandbutton。保存该工程。 
,欢迎访问网页设计爱好者web开发。