0

我有以下存储过程,我从我的 Windows 窗体应用程序数据集中调用它,如下所示:

  Me.TransactionTableAdapter.spPaymentApply(130, iAmount, Now)

尽管我提供了 CustomerID,(并单步执行代码以查看它是否确实存在 - 确实存在)但在执行过程中出现以下错误:过程或函数“PaymentApply”需要参数“@CustomerID”,但未提供该参数。

这是我的SP:

USE [dbPB]
GO
/****** Object:  StoredProcedure [dbo].[PaymentApply]    Script Date: 05/30/2013 18:34:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[PaymentApply]
   @CustomerID int,
   @Amount int,
   @AsOfDate datetime
AS
WITH Totals AS (
   SELECT
      T.*,
      RunningTotal =
         Coalesce (
            (SELECT Sum(S.Buyin - Coalesce(S.CreditPaid, 0))
            FROM [Transaction] S
            WHERE
               T.CustomerID = S.CustomerID
               AND S.Type = 'Credit'
               AND S.Buyin > Coalesce(T.CreditPaid, 0)
               AND (
                  T.Starttime > S.Starttime
                  OR (
                     T.Starttime = S.Starttime
                     AND T.TransactionID > S.TransactionID
                  )
               )
            ),
        0)
   FROM
      [Transaction] T
   WHERE
      CustomerID = @CustomerID
      AND T.Type = 'Credit'
      AND T.Buyin > Coalesce(T.CreditPaid, 0)
)
UPDATE T
SET
   T.EndTime = P.EndTime,
   T.CreditPaid = Coalesce(T.CreditPaid, 0) + P.CreditPaid
FROM
   Totals T
   CROSS APPLY (
      SELECT TOP 1
         V.*
      FROM
         (VALUES
            (T.Buyin - Coalesce(T.CreditPaid, 0), @AsOfDate),
            (@Amount - RunningTotal, NULL)
         ) V (CreditPaid, EndTime)
      ORDER BY
         V.CreditPaid,
         V.EndTime DESC
   ) P
WHERE
   T.RunningTotal <= @Amount
   AND @Amount > 0;
;

VB代码:

Private Sub btnTransProcess_Click(sender As Object, e As EventArgs) Handles btnTransProcess.Click
        Dim iAmount As Integer
        Dim drv As DataRowView = CType(Me.cboCustomerName.SelectedItem, DataRowView)
        Dim SelCustId As Integer
        SelCustId = drv.Item("CustomerID")
        Try
            iAmount = CType(txtCreditPayment.Text, Integer)
        Catch ex As Exception
            MessageBox.Show("Enter a valid Credit Payment Amount!", "Invalid Amount", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End Try
        MsgBox(SelCustId)
        Me.TransactionTableAdapter.spPaymentApply(130, iAmount, Now)
        '
        'Dim command As New SqlCommand(queryString, connection)
        'command.Parameters.AddWithValue("@CustomerID", 123)
    End Sub
4

2 回答 2

2

发生了什么 ?

你没有提供参数。

但是..我仔细检查了...

也许你没有用正确的方式称呼它?
你有没有尝试过这样的事情:

  Dim queryString As String = "exec PaymentApply" 

    Using connection As New SqlConnection(ConnStrg)
        connection.Open()
        Dim command As New SqlCommand(queryString, connection)
        command.Parameters.AddWithValue("@CustomerID", 123)
        // ... same for other non-nullable parameters
        Dim reader As SqlDataReader = command.ExecuteReader()
    End Using

End Try

我为什么要这样做?

当您从代码中调用存储过程时,您不提供@CustomerID int参数。

由于参数不是nullable,因此它需要一个值,并且在您给它一个值之前不会进一步。

如果您希望它可以为空,您可以为其设置默认值。这样,如果您不提供新值,他将采用此值。例子:

从您的代码中,您传递一个像这样的参数:

someCommand.Parameters.AddWithValue("@CustomerID", 123)

如果您希望参数可以为空,请在 SQL 中执行类似的操作

@CustomerID int = 123 
于 2013-05-30T18:00:10.623 回答
1

听起来您使用的是表适配器而不是命令对象。

请参阅:表适配器 和 表适配器查询配置向导

您的调用Me.TransactionTableAdapter.spPaymentApply(130, iAmount, Now)看起来应该提供 130 作为 CustomerID- 但如果代码无法正常编译/工作,也许您应该使用配置向导。

或者,考虑改用命令对象。此处概述了两者的使用:

如何:创建并执行不返回值的 SQL 语句

于 2013-05-30T18:15:53.830 回答