【问题标题】:SQL Stored Call not passing parameterSQL 存储调用未传递参数
【发布时间】:2013-04-25 09:39:25
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    你能试试下面sn-p中的代码是否有效吗?

    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
    

    【讨论】:

    • 在编辑中包含cmd.CommandType = CommandType.StoredProcedure
    • 以为默认的CommandType是StoredProcedure???它有效,但我现在很困惑。
    • @PhilMurray:不,默认是CommandType.Text - 例如内联 SQL 语句
    【解决方案2】:

    使用这个

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 2014-07-07
      • 1970-01-01
      • 2019-11-02
      • 2014-05-17
      • 1970-01-01
      • 2018-01-31
      相关资源
      最近更新 更多