简体   繁体   English

找不到存储过程''

[英]Could not find stored procedure ''

I'm trying to send my SQL DB a query as a stored procedure. 我正在尝试向我的SQL DB发送查询作为存储过程。 It's not really stored in the DB, I'm creating it on the fly. 它并没有真正存储在数据库中,我正在动态创建它。 Here is the code (excuse my VB): 这是代码(对不起,我的VB):

Dim idOfChosenRecord As Integer = {Some value}
Dim myQuery As String  "SELECT [field] FROM [myDB].[myTable] WHERE [id]=@id"
Dim cmd As SqlCommand = New SqlCommand(myQuery, myConnectionInitializedAndOpen)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter("@id", idOfChosenRecord))
Dim Reader As SqlDataReader = cmdView.ExecuteReader()

A similar function is running well right before my code (only it's calling a real stored procedure), and all over the module, as a matter of fact. 实际上,一个类似的函数在我的代码之前(只是在调用一个真正的存储过程)以及整个模块都运行良好。 The difference is I'm trying to create the query on the spot. 所不同的是,我正在尝试当场创建查询。

The error message is the strange title of this question: Could not find stored procedure '' - caught on the last line of the above code. 错误消息是该问题的奇怪标题: Could not find stored procedure '' -捕获在上述代码的最后一行。

How would this be fixed (preferably without digging into the DB itself...)? 如何解决此问题(最好不深入数据库本身...)?

tx TX

Replace 更换

cmd.CommandType = CommandType.StoredProcedure

with

cmd.CommandType = CommandType.Text

The problem is that you specified that you want to call stored procedure when in fact you have just a query. 问题是实际上您只有一个查询时,您指定要调用存储过程。 So you should specify that this is a query. 因此,您应该指定这是一个查询。

You should change : 您应该更改:

cmd.CommandType = CommandType.StoredProcedure

To

cmd.CommandType = CommandType.Text

You can't do it this way. 你不能这样子。 You can't create stored procedure on the fly. 您不能动态创建存储过程。 Once you choose the CommandType.StoredProcedure then it will expect you to pass the procedure name in myQuery . 选择CommandType.StoredProcedure之后,它将期望您在myQuery传递过程名称。 So you have to use cmd.CommandType = CommandType.Text 因此,您必须使用cmd.CommandType = CommandType.Text

You need to change it to 您需要将其更改为

cmd.CommandType = CommandType.Text

A Stored Procedure is different from running a text query (like you're doing here). 存储过程不同于运行文本查询(就像您在此处所做的那样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM