如何通过存储过程获取标量值函数结果VB.net表格

时间:2018-09-13 03:36:06

标签: sql vb.net

VB.Net代码

' Getting Records Before Transfer to GL
Call OpenAccConnection(lblUserName.Text, lblPassword.Text)
Dim odcTotalsForTransferGL As OleDbCommand = New OleDbCommand("spPet_TotalsForTransferGL", conAccounts)
odcTotalsForTransferGL.CommandType = CommandType.StoredProcedure
' Parameter Assigning
Dim strCompanyCode As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("@ComCod", OleDbType.VarChar, 2)
strCompanyCode.Direction = ParameterDirection.Input
Dim strLocationCode As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("@LocCod", OleDbType.VarChar, 2)
strLocationCode.Direction = ParameterDirection.Input
Dim strPettyCashDate As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("@PetDat", OleDbType.VarChar, 8)
strPettyCashDate.Direction = ParameterDirection.Input
Dim strBegVNo As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("@BegVNo", OleDbType.Integer)
strBegVNo.Direction = ParameterDirection.Output
Dim strEndVNo As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("@EndVNo", OleDbType.Integer)
strEndVNo.Direction = ParameterDirection.Output
Dim strVouTotal As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("@VouTotal", OleDbType.Integer)
strVouTotal.Direction = ParameterDirection.Output
Dim decPetTotal As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("@PetTotal", OleDbType.Decimal)
decPetTotal.Direction = ParameterDirection.Output
Dim intFinancialDates As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("@FinancialDates", OleDbType.Integer)
intFinancialDates.Direction = ParameterDirection.Output
' Passing Parameters
' Company Code
strCompanyCode.Value = cboCompanyCode.SelectedItem.ToString.Substring(0, 2)
' Location Code
strLocationCode.Value = cboLocationCode.SelectedItem.ToString.Substring(0, 2)
' Petty Cash Date(Year & Month)
strPettyCashDate.Value = dtPettyCashDate.Value.Year.ToString + dtPettyCashDate.Value.Month.ToString("D2") + "01"
' Accounts Database Open
conAccounts.Open()
' Stored Procedure Process
Dim odrTotalsForTransferGL As OleDbDataReader = odcTotalsForTransferGL.ExecuteReader()
If odrTotalsForTransferGL.HasRows Then
    Do While odrTotalsForTransferGL.Read
        lblAccPeriod.Text = odrTotalsForTransferGL.GetValue(4).ToString.Substring(0, 4) + "/" + odrTotalsForTransferGL.GetValue(4).ToString.Substring(5, 4)
        lblFiscalMonth.Text = odrTotalsForTransferGL.GetValue(4).ToString.Substring(9, 2)
        lblBegVNo.Text = odrTotalsForTransferGL.GetValue(0).ToString
        lblEndVNo.Text = odrTotalsForTransferGL.GetValue(1).ToString
        lblPettyTotal.Text = odrTotalsForTransferGL.GetValue(3).ToString
    Loop
End If

存储过程

ALTER PROCEDURE [dbo].[spPet_TotalsForTransferGL] 

    -- Add the parameters for the stored procedure here
    @ComCod as varchar(2),
    @LocCod as varchar(2),
    @PetDat as varchar(8),  
    @BegVNo as int OUT,
    @EndVNo as int OUT,
    @VouTotal as int OUT,
    @PetTotal as decimal(12,2) OUT,
    @FinancialDates as varchar(10) OUT

    AS
    BEGIN

    SELECT MIN(PettyDetail.DPetVouNo),
           MAX(PettyDetail.DPetVouNo),
           MAX(PettyDetail.DPetVouNo) - MIN(PettyDetail.DPetVouNo),
           ISNULL(SUM(PettyDetail.DPetAmount), 0)
           FROM PettyDetail
                WHERE (PettyDetail.DPetComCode = @ComCod) AND
                      (PettyDetail.DPetLocCode = @LocCod) AND
                      (YEAR(PettyDetail.DPetDate) = YEAR(CONVERT(Date,@PetDat,111))) AND
                      (MONTH(PettyDetail.DPetDate) = MONTH(CONVERT(Date,@PetDat,111)))

    /* Getting Financial Dates */

    EXECUTE @FinancialDates = dbo.fnApp_GetFinancialDates @PetDat

END

标量函数

ALTER FUNCTION [dbo].[fnApp_GetFinancialDates] 
(
    -- Add the parameters for the function here
    @PetDat as varchar(8)
)
--RETURNS int(10)
RETURNS varchar(10)
AS
BEGIN

    -- Declare the return variable here
    --DECLARE @FinancialDates int(10)
    DECLARE @FinancialDates varchar(10)

    -- Add the T-SQL statements to compute the return value here
    IF MONTH(CONVERT(date,@PetDat,111)) BETWEEN 4 AND 12
        BEGIN
            SELECT @FinancialDates = (SELECT
                                             CAST((YEAR(CONVERT(date,@PetDat,111))) as varchar) +
                                             CAST((YEAR(CONVERT(date,@PetDat,111)) + 1) as varchar) +
                                             REPLICATE('0',(2-(LEN(CAST((MONTH(CONVERT(date,@PetDat,111)) - 3) as varchar))))) + (CAST((MONTH(CONVERT(date,@PetDat,111)) - 3) as varchar)))
        END
    ELSE
        BEGIN
            SELECT @FinancialDates = (SELECT
                                             CAST((YEAR(CONVERT(date,@PetDat,111)) - 1)as varchar) +
                                             CAST((YEAR(CONVERT(date,@PetDat,111))) as varchar) +
                                             CAST((MONTH(CONVERT(date,@PetDat,111)) + 9) as varchar))
        END
    -- Return the result of the function
    RETURN @FinancialDates

END

以上函数@FinancialDates的值未返回到.Net表单。但是其他结果将返回表格。

任何人都可以帮助我解决此问题。过程和函数可以在查询管理器中正确运行。

enter image description here

1 个答案:

答案 0 :(得分:1)

两个选项:

  • 选项1:更改EXECUTE @FinancialDates = dbo.fnApp_GetFinancialDates @PetDat, 至 :
      

    SET @FinancialDates = dbo.fnApp_GetFinancialDates(@PetDat)

  • 选项2:在SELECT语句中包含fnApp_GetFinancialDates函数(您可以将@FinancialDates删除为varchar(10)OUT参数语句。

    ALTER PROCEDURE [dbo].[spPet_TotalsForTransferGL] 
    -- Add the parameters for the stored procedure here
    @ComCod as varchar(2),
    @LocCod as varchar(2),
    @PetDat as varchar(8)
    
    AS
    BEGIN
    
    SELECT MIN(PettyDetail.DPetVouNo) AS 'BegVNo',
       MAX(PettyDetail.DPetVouNo) AS 'EndVNo',
       MAX(PettyDetail.DPetVouNo) - MIN(PettyDetail.DPetVouNo) AS 'VouTotal',
       ISNULL(SUM(PettyDetail.DPetAmount), 0) AS 'PetTotal'
       dbo.fnApp_GetFinancialDates (@PetDat) AS 'FinancialDates'
       FROM PettyDetail
            WHERE (PettyDetail.DPetComCode = @ComCod) AND
                  (PettyDetail.DPetLocCode = @LocCod) AND
                  (YEAR(PettyDetail.DPetDate) = YEAR(CONVERT(Date,@PetDat,111))) AND
                  (MONTH(PettyDetail.DPetDate) = MONTH(CONVERT(Date,@PetDat,111)))
    END
    

,对于选项2 VB代码:

  If odrTotalsForTransferGL.HasRows Then
  Do While odrTotalsForTransferGL.Read
    lblAccPeriod.Text = odrTotalsForTransferGL("FinancialDates").ToString.Substring(0, 4) + "/" + odrTotalsForTransferGL("FinancialDates").ToString.Substring(5, 4)
    lblFiscalMonth.Text = odrTotalsForTransferGL("FinancialDates").ToString.Substring(9, 2)
    lblBegVNo.Text = odrTotalsForTransferGL("BegVNo").ToString
    lblEndVNo.Text = odrTotalsForTransferGL("EndVNo").ToString
    lblPettyTotal.Text = odrTotalsForTransferGL("PetTotal").ToString
  Loop
End If

edited:并且不要忘记删除以下代码,因为它们不是必需的,并且由于存储的proc不再具有输出参数,因此会产生错误:

Dim strBegVNo As OleDbParameter = 
odcTotalsForTransferGL.Parameters.Add("@BegVNo", OleDbType.Integer)
strBegVNo.Direction = ParameterDirection.Output
Dim strEndVNo As OleDbParameter = 
odcTotalsForTransferGL.Parameters.Add("@EndVNo", OleDbType.Integer)
strEndVNo.Direction = ParameterDirection.Output
Dim strVouTotal As OleDbParameter = 
odcTotalsForTransferGL.Parameters.Add("@VouTotal", OleDbType.Integer)
strVouTotal.Direction = ParameterDirection.Output
Dim decPetTotal As OleDbParameter = 
odcTotalsForTransferGL.Parameters.Add("@PetTotal", OleDbType.Decimal)
decPetTotal.Direction = ParameterDirection.Output
Dim intFinancialDates As OleDbParameter = 
odcTotalsForTransferGL.Parameters.Add("@FinancialDates", OleDbType.Integer)
intFinancialDates.Direction = ParameterDirection.Output