从SQL存储过程向VB.Net表单返回值

时间:2018-07-09 07:09:42

标签: sql vb.net

我正在从VB.Net表单向SQL存储过程发送参数。所有参数均成功传递给过程(表更新成功)。我想将参数值从存储过程传递(返回)到VB.net表单。但它不会返回。

当我手动(在浏览中)运行SQL存储过程时,返回值。

请有人帮我解决这个问题

Call OpenAccConnection(lblUserName.Text, lblPassword.Text)
Dim odcVoucherInsert As OleDbCommand = New OleDbCommand("spPet_VoucherInsert", conAccounts)
odcVoucherInsert.CommandType = CommandType.StoredProcedure
' Parameter Assigning
Dim strCompanyCode As OleDbParameter = odcVoucherInsert.Parameters.Add("@ComCod", OleDbType.VarChar, 2)
strCompanyCode.Direction = ParameterDirection.Input
Dim strLocationCode As OleDbParameter = odcVoucherInsert.Parameters.Add("@LocCod", OleDbType.VarChar, 2)
strLocationCode.Direction = ParameterDirection.Input
Dim strVouDate As OleDbParameter = odcVoucherInsert.Parameters.Add("@PetDat", OleDbType.VarChar, 8)
strVouDate.Direction = ParameterDirection.Input
Dim strPayee As OleDbParameter = odcVoucherInsert.Parameters.Add("@PetPayee", OleDbType.VarChar, 50)
strPayee.Direction = ParameterDirection.Input
Dim strAcNo As OleDbParameter = odcVoucherInsert.Parameters.Add("@PetAcNo", OleDbType.VarChar, 6)
strAcNo.Direction = ParameterDirection.Input
Dim intPetVouNo As OleDbParameter = odcVoucherInsert.Parameters.Add("@PetVouNo", OleDbType.Integer)
intPetVouNo.Direction = ParameterDirection.InputOutput
Dim strDetails As OleDbParameter = odcVoucherInsert.Parameters.Add("@PetDetl", OleDbType.VarChar, 150)
strDetails.Direction = ParameterDirection.Input
Dim decAmount As OleDbParameter = odcVoucherInsert.Parameters.Add("@PetAmount", OleDbType.Decimal)
decAmount.Direction = ParameterDirection.Input
' Passing Parameters
' Company Code
strCompanyCode.Value = cboCompanyCode.SelectedItem.ToString.Substring(0, 2)
' Location Code
strLocationCode.Value = cboLocationCode.SelectedItem.ToString.Substring(0, 2)
' Date
strVouDate.Value = dtPmtDate.Value.ToString("yyyyMMdd")
' Payee
strPayee.Value = txtPayee.Text
' Account Number {1st Row (RowIndex = 0) of the Data Grid}
If CInt(dgPettyCashEnter("Account", 0).Value.ToString.Length) = 6 Then
    strAcNo.Value = dgPettyCashEnter("Account", 0).Value.ToString
ElseIf CInt(dgPettyCashEnter("Account", 0).Value.ToString.Length) < 6 Then
    strAcNo.Value = dgPettyCashEnter("Account", 0).Value.ToString.PadLeft(6, CChar("0"))
End If
' Details {1st Row (RowIndex = 0) of the Data Grid}
strDetails.Value = dgPettyCashEnter("Description", 0).Value
' Amount {1st Row (RowIndex = 0) of the Data Grid}
decAmount.Value = CDec(dgPettyCashEnter("Amount", 0).Value)
' Accounts Database Open
conAccounts.Open()
' Stored Procedure Process
Dim odrVoucherInsert As OleDbDataReader = odcVoucherInsert.ExecuteReader
If odrVoucherInsert.HasRows Then
    Do While odrVoucherInsert.Read
        MessageBox.Show(CStr(odrVoucherInsert.GetValue(0)))
    Loop
End If

SQL过程

spPet_VoucherInsert
-- Add the parameters for the stored procedure here
@ComCod as nvarchar(2),
@LocCod as nvarchar(2),
@PetDat as nvarchar(8), 
@PetPayee as varchar(50),
@PetAcNo as nvarchar(6),
@PetVouNo as int OUTPUT,
@PetDetl as varchar(150),
@PetAmount as decimal(10,2)
AS
BEGIN

WHILE @PetVouNo = 0 -- 1st Run
    BEGIN -- Retrive Voucher Number
        IF MONTH(CONVERT(date, @PetDat, 111)) = 4 -- Begining of Each & Every Financial Year
            IF (SELECT COUNT(*)
                   FROM PettyHeader
                        WHERE HPetComCode = @ComCod AND
                              HPetLocCode = @LocCod AND
                              YEAR(HPetDate) = YEAR(CONVERT(date,@PetDat,111))AND
                              MONTH(HPetDate) = '04') = 0 -- No Records Found for April Month
                SELECT @PetVouNo = ISNULL(MAX(HPetVouNo)+1,1)
                       FROM PettyHeader
                            WHERE HPetComCode = @ComCod AND
                                  HPetLocCode = @LocCod AND
                                  YEAR(HPetDate) = YEAR(CONVERT(date,@PetDat,111))AND
                                  MONTH(HPetDate) = '04'
                ELSE -- Records Found for April Month
                    GOTO NormalVoucher#Count
        ELSE -- Add 1 to Last Voucher Number
            GOTO NormalVoucher#Count -- Add 1 to Last Voucher Number
        ---- Save on PettyHeader
    END
        GOTO PettyDetail_Save
END
-- Sub Routine Section
NormalVoucher#Count: -- Add 1 to Last Voucher Number
SELECT @PetVouNo = (MAX(HPetVouNo)+1)
   FROM pettyHeader
        WHERE HPetComCode = @ComCod AND
              HPetLocCode = @LocCod AND
              (HPetDate >= CONVERT(date,(CONVERT(varchar,(YEAR(CONVERT(date,@PetDat,111)))) + '0401'),111) OR
              HPetDate <= CONVERT(date,(CONVERT(varchar,(CONVERT(int,(YEAR(CONVERT(date,@PetDat,111)))+1))) + '0331'),111))
-- <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
PettyHeader_Save:
-- Save on PettyHeader
INSERT INTO PettyHeader
            (HPetComCode,
             HPetLocCode,
             HPetDate,
             HPetVouNo,
             HPetPayee)
        VALUES (@ComCod,
                @LocCod,
                CONVERT(DATE, @PetDat, 111),
--                  @intNVouNo,
                @PetVouNo,
                RTRIM(@PetPayee))
-- <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
PettyDetail_Save:
-- Save on PettyDetail
INSERT INTO PettyDetail
        (DPetComCode,
         DPetLocCode,
         DPetDate,
         DPetVouNo,
         DPetAcNo,
         DPetDetail,
         DPetAmount)
    VALUES (@ComCod,
            @LocCod,
            CONVERT(DATE, RTRIM(@PetDat), 111),
--              @intNVouNo,
            @PetVouNo,
            RTRIM(@PetAcNo),
            RTRIM(@PetDetl),
            @PetAmount)
-- <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
return @PetVouNo

0 个答案:

没有答案