SQL 如何从 VB.NET 中的存储过程获取返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3650894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a return value from a stored procedure in VB.NET
提问by George Trevour Dsouza
I have a stored procedure in SQL Server for generating transaction numbers.
我在 SQL Server 中有一个用于生成事务号的存储过程。
Can anyone help me with how to call the Stored Procedure from VB.NET and how will i get the value that is returned from the procedure into the front end.
任何人都可以帮助我了解如何从 VB.NET 调用存储过程,以及如何将过程返回的值获取到前端。
Regards, George
问候, 乔治
回答by Ben
I think you want something like this:
我想你想要这样的东西:
Public Sub Foo()
Using sql As New SqlClient.SqlConnection("YourConnection")
sql.Open()
Using cmd As New SqlClient.SqlCommand("YourSPName", sql)
cmd.CommandType = CommandType.StoredProcedure
Dim myReturnValue As String = cmd.ExecuteScalar
End Using
End Using
End Sub
Where myReturnValue will be what ever your output param in SQL is.
其中 myReturnValue 将是您在 SQL 中的输出参数。
回答by Aseem Gautam
回答by m.edmondson
What kind of value is it you're returning? Will that value in turn result in another database action?
你返回的是什么价值?该值会反过来导致另一个数据库操作吗?
It might be best to return data instead of a single value.
最好返回数据而不是单个值。
For example if you were verifying the username and password for a potential login, instead of returning a simple true or false you would return the users information. No information returned means failed login.
例如,如果您正在验证潜在登录的用户名和密码,您将返回用户信息而不是简单的 true 或 false。没有返回信息表示登录失败。
This method has the advantage of minimising database requests, something which will have a serious effect if it is a common action.
这种方法的优点是可以最大限度地减少数据库请求,如果这是一个常见的操作,将会产生严重的影响。
Personally I've never needed to return a single value.
就我个人而言,我从不需要返回单个值。