3

我有一个 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

例外

过程或函数“spIsEngineSixCylinderByBatchID”需要未提供的参数“@BatchID”。

调用堆栈

   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
4

2 回答 2

2

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

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
于 2013-04-25T09:56:26.803 回答
0

用这个

Private Function IsBatchSixCylinder(batchID As Int32) As Boolean

Dim isSixCylinder As Boolean = False



Try
    Using cmd As New SqlClient.SqlCommand("[dbo].[spIsEngineSixCylinderByBatchID]", _conn)

    cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@BatchID", batchID)
        isSixCylinder = cmd.ExecuteScalar
    End Using

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

Return isSixCylinder

End Function
于 2013-04-25T09:57:45.957 回答