在.net中调用存储过程的另一种方法
2024-07-10 12:58:46
供稿:网友
[存储过程]
create procedure test_procedure @cid int ,@y int, @n varchar(10) output
as
select * from city where [email protected]
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