为什么我只能执行两次存储过程?

时间:2009-11-24 17:31:00

标签: sql-server vb.net stored-procedures

在我的VB.NET代码中我构造了一个ODBC.COMMAND来调用一个存储过程来消除SQLSERVER 2008中的表中的记录,我在FOR NEXT循环中使用这个代码,它可能包含1到20个要传递给的record_ID存储过程以删除记录。

这段代码可以使用好几个月,但自上次编译以来,它只删除前两条记录。

在VS 2008的调试模式下,IDE工作正常。

VB.NET代码

    ''' <summary>
    ''' DELETE CANCELED CUSTOMER ORDERS
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub tsbDiarioVenta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbDiarioVenta.Click

        On Error GoTo Proc_Err

        Using frmrslt As New BuscarForm()

            frmrslt.Titulo = "Elimina Diarios de Venta en Contabilidad"
            frmrslt.TextFieldID = "Cg_MovCab_ID"

            frmrslt.Sql = " SELECT  TIPO_COMP, NUM_COMP, FEC_DOC, COD_AUX, COD_REF, NUM_REF, STATUS, Cg_MovCab_ID " & _
                        " FROM Cg_MovCab " & _
                        " WHERE COD_EMPR = " & DataShared.gintCODEMPR & _
                        " AND YEAR(FEC_DOC) = " & Me.txtPER_ANUAL.Text & _
                        " AND MONTH(FEC_DOC) = " & Me.txtPER_MENSUAL.Text & _
                        " AND COD_REF IN ('FAC', 'NV') " & _
                        " ORDER BY 1,2 "

            frmrslt.ShowDialog()

            If frmrslt.Selected Then

                Debug.Print(frmrslt.CountRows.ToString)

                If frmrslt.CountRows > 0 Then

                    Cursor.Current = Cursors.WaitCursor

                    frmrslt.StartRow()
                    Dim idx As Integer
                    For idx = 0 To frmrslt.CountRows

                        Debug.Print(frmrslt.KeyValueID)
                        ClsCgMovs.DeleteID(frmrslt.KeyValueID)
                        frmrslt.NextRow()


                    Next

                    MsgBox(frmrslt.CountRows.ToString & " Regs. Eliminados", MsgBoxStyle.Information, "Mensaje del Sistema")

                End If

            End If

        End Using

        Cursor.Current = Cursors.Default
        Exit Sub

Proc_Err:
        Cursor.Current = Cursors.Default
        ErrHnd(Err.Number, Err.Description, ONDEBUG, "Frm_VtActLote: tsbDiarioVenta_Click: ", Erl)
    End Sub



    Public Function DeleteID(ByVal PRowID As Integer) As Boolean

        On Error GoTo Proc_Err

        Using odbcconn As New OdbcConnection(DataShared.gstrCNN)

            odbcconn.Open()
            Dim OdbcCmd As New OdbcCommand( _
                "{ ?=CALL proc_Cg_MovCab_DeleteID(" & PRowID & ") }", odbcconn)

            OdbcCmd.Parameters.Add("@return", OdbcType.Int)
            OdbcCmd.Parameters("@return").Direction = ParameterDirection.ReturnValue

            OdbcCmd.ExecuteNonQuery()
            If CInt(OdbcCmd.Parameters("@return").Value) = 0 Then
                DeleteID = True
            Else
                DeleteID = False
            End If

        End Using

        Exit Function

Proc_Err:
        ErrHnd(Err.Number, Err.Description, ONDEBUG, "ClsCgMovs: DeleteID: ")
        DeleteID = False
    End Function




ALTER PROCEDURE [dbo].[proc_Cg_MovCab_DeleteID] 
(
    @ID int
)

AS
    SET NOCOUNT ON

    DECLARE @errornum int

    BEGIN TRAN

    DELETE  FROM Cg_MovCab
    WHERE Cg_MovCab_ID = @ID

    SET @errornum = @@ERROR
    IF @errornum <> 0 GOTO PROC_ERR

    COMMIT TRAN

    RETURN @errornum

PROC_ERR:
    ROLLBACK TRAN
    RETURN @errornum

2 个答案:

答案 0 :(得分:1)

可能是你的可用连接不足,在退出函数之前尝试调用odbcconn.Close()。这可以解释为什么它之前有效,如果通过更新修改或重置允许的连接数,或者有人修修补补。

答案 1 :(得分:0)

如果DELETE依赖于VIEW,您可以尝试删除并重新创建相关的VIEW。 如果基础表发生变化,VIEWS碰巧会返回错误的值(即切换列等)。

只是拍一下......

编辑:

ATPS:

AFAIR更改表结构(添加/删除字段)导致此问题 关于矿山生产系统。 SQL 2005 btw。