.net - SQL存储调用不传递参数

标签 .net vb.net stored-procedures .net-4.0

我有一个 SQL Server 2008 存储过程,当通过下面所示的代码调用时,会返回 @BatchID 参数的缺失参数异常。

正如你所看到的,我正在传递参数

cmd.Parameters.AddWithValue("@BatchID", batchID)

但由于某种原因,在调用服务器时它没有接收到它。

这里可能缺少一些简单的东西,这是漫长的一天。

存储过程

ALTER PROCEDURE [dbo].[spIsEngineSixCylinderByBatchID]
    @BatchID as int
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @IsSixCylinder as bit
    SET @IsSixCylinder = 0

    SELECT @IsSixCylinder = tblBatches.SixCylinder
        FROM tblBatches
        WHERE IdBatch = @BatchID

    SELECT @IsSixCylinder
END

代码

Private Function IsBatchSixCylinder(batchID As Int32) As Boolean
    Dim isSixCylinder As Boolean = False

    Try
        Using cmd As New SqlClient.SqlCommand("[dbo].[spIsEngineSixCylinderByBatchID]", _conn)
            cmd.Parameters.AddWithValue("@BatchID", batchID)
            isSixCylinder = cmd.ExecuteScalar
        End Using

    Catch ex As SqlClient.SqlException
        LogError(ex.Message)
    End Try

    Return isSixCylinder

End Function

异常

Procedure or function 'spIsEngineSixCylinderByBatchID' expects parameter '@BatchID', which was not supplied.

调用堆栈

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteScalar()
   at ClassLib_SAPtoEPMS.clsSAPtoEPMS.IsBatchSixCylinder(Int32 batchID) in C:\Users\phil.murray\Desktop\EPMS_05022012\EPMS_05022012\Power Systems EPMS\ClassLib_SAPtoEPMS\ClassLib_SAPtoEPMS\clsSAPtoEPMS.vb:line 229

最佳答案

您可以尝试一下下面代码片段中的代码是否有效吗?

Using cmd As New SqlClient.SqlCommand("spIsEngineSixCylinderByBatchID", _conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@BatchID", batchID)
            isSixCylinder = cmd.ExecuteScalar
End Using

或者

Using cmd As New SqlClient.SqlCommand("spIsEngineSixCylinderByBatchID", _conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("@BatchID ", SqlDbType.Int)
            cmd.Parameters("@BatchID ").Value = batchID
End Using

关于.net - SQL存储调用不传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16211344/

相关文章:

c# - 在 ASP.NET 中从 Bin 加载程序集

vb.net - VB基于字符串变量从模块中引用属性

tsql - 经典 ASP - 如何获取所有 T-SQL 存储过程行集

c# - 为我的 C# .NET Winform 应用程序创建单个连接文件

.net - .NET将数字转换为字符串表示形式(1到1、2到2等,等等)

javascript - <%= x.ClientID %> 在重复的用户控件中返回错误值

vb.net - 你能在 ParamArray 参数中传递一个列表吗

sql - 如何授予数据库角色对架构的执行权限?我究竟做错了什么?

sql-server - SQL 服务器 : Case with the statement "NOT IN"

.net - System.IO.FileNotFoundException 与 Log4Net 发布时