欢迎光临
我们一直在努力

在.net中调用存储过程的另一种方法-.NET教程,.NET Framework

建站超值云服务器,限时71元/月

[存储过程] 
create procedure test_procedure @cid int ,@y int, @n varchar(10) output 
as 
select * from city where cid=@cid 
if @y=1 
begin 
  set @n = 成功! 
  return 0 
end 
else 
begin 
  set @n = 失败! 
  return -1 
end 
go 
[程序中调用存储过程]  

private sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load 
        dim conn as oledbconnection 
        try 
            conn = new oledbconnection(“provider=sqloledb;data source=localhost;user id=sa;password=;initial catalog=jiang”) 
            dim cmd as new oledbcommand(” {? = call test_procedure(?,?,?)}”, conn) 
            conn.open() 
            with cmd 
                .commandtype = commandtype.text ‘这句可以省略,因为是默认设置,但这里还是显式地写出以引起注意 
                .parameters.add(“@retval”, oledbtype.integer) 
                .parameters.add(“@cid”, oledbtype.integer) 
                .parameters.add(“@y”, oledbtype.integer) 
                .parameters.add(“@n”, oledbtype.varchar, 10) 
                .parameters(“@n”).direction = parameterdirection.output 
                .parameters(“@retval”).direction = parameterdirection.returnvalue 

                .parameters(“@cid”).value = 1 
                .parameters(“@y”).value = 1 
                .executenonquery() 
                messagebox.show(.parameters(“@n”).value) ‘测试结果为“成功!” 

            end with 
        catch ex as exception 
            messagebox.show(ex.tostring) 
       finally 
            conn.close() 
        end try 
    end sub

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在.net中调用存储过程的另一种方法-.NET教程,.NET Framework
分享到: 更多 (0)