从 VB.NET 调用存储过程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3655843/
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
Calling Stored Procedures from VB.NET
提问by George Trevour Dsouza
Can anyone please help me on the following:
任何人都可以帮助我解决以下问题:
I have created a SQL Server stored procedure as follows:
我创建了一个 SQL Server 存储过程,如下所示:
create procedure prcGet_sub_menu_list
@sub_menu char(5)
as
begin
select
'menu_code' = menu_code
'menu_name' = menu_name
from sub_menu_master
where menu_code = @sub_menu
end
return
Now i am calling this procedure from VB.NET, but i get an error like 'Stored procedure prcGet_sub_menu_list expects parameter @sub_menu which was not supplied'. Please help me on the same. The code which i have in VB.NET is as follows:
现在我正在从 VB.NET 调用这个过程,但我收到一个错误,如“存储过程 prcGet_sub_menu_list 需要未提供的参数 @sub_menu”。请帮助我。我在 VB.NET 中的代码如下:
Imports System.Data
Imports System.Data.SqlClient
Dim sqlConn as New SqlClient.SqlConnection
sqlConn.ConnectionString = "........"
sqlConn.Open()
Dim menuCode as string
menuCode = cboDetails.selectedItem
Dim sqlCmd as New SqlCommand
sqlCmd.Connection = Connection.sqlConn
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.CommandText = "prcGet_sub_menu_list"
sqlCmd.Parameter.Add("menuCode", SqlDbType.Char)
Dim sqlDA as New SqlDataAdapter()
sqlDA.SelectCommand = sqlCmd
Dim sqlDT as New DataTable
sqlDA.Fill(sqlDT)
This is the code that i have written and it gives me the error: 'Stored procedure prcGet_sub_menu_list expects parameter @sub_menu which was not supplied'.
这是我编写的代码,它给了我错误:“存储过程 prcGet_sub_menu_list 需要未提供的参数 @sub_menu”。
Please give me some help on the same.
请给我一些帮助。
Regards, George
问候, 乔治
采纳答案by Geeth
You have to give the same parameter name and type like in your stored procedure.
您必须提供与存储过程中相同的参数名称和类型。
sqlCmd.Parameters.Add(New SqlParameter("@sub_menu", SqlDbType.Char, 5)).Value = "Joe"
Geetha.
吉塔。
回答by EMP
As the message implies, you should add a parameter named "sub_menu" in the same way as you're adding the "menuCode" parameter. You should probably also give it a value:
正如消息所暗示的那样,您应该以与添加“menuCode”参数相同的方式添加一个名为“sub_menu”的参数。您可能还应该给它一个值:
sqlCmd.Parameter.Add("sub_menu", SqlDbType.Char).Value = "Blah"
(Of course, I don't know what the correct type is, so Char is just an example.)
(当然,我不知道正确的类型是什么,所以 Char 只是一个例子。)
回答by Adriaan Stander
You need to use the correct parameter name
您需要使用正确的参数名称
Something like
就像是
sqlCmd.Parameter.Add("@sub_menu", SqlDbType.Char)
And assign it a value
并为其赋值
Something like
就像是
sqlCmd.Parameters("@sub_menu").Value = val
Have a look at SqlCommand.Parameters Property
and maybe Lesson 07: Using Stored Procedures
可能还有第 7 课:使用存储过程
回答by MDM
A couple things. Parameter should be Parameters. Also sqlCmd.Parameters.Add is deprecated so use sqlCmd.Parameters.AddWithValue:
一些事情。参数应该是参数。此外 sqlCmd.Parameters.Add 已弃用,因此请使用 sqlCmd.Parameters.AddWithValue:
sqlCmd.Parameters.AddWithValue("menuCode", menuCode)
sqlCmd.Parameters.AddWithValue("menuCode", menuCode)