从SQL查询Visual Basic返回1条记录

时间:2015-11-06 21:28:22

标签: sql sql-server vb.net

我尝试使用SQL查询在VB中创建发票。我的查询以下列形式返回数据:

Name      Description      Price
John        Sandwich        4.00
John         Salad          5.00
John         Drink          2.00

我试图只返回该名称的1条记录,但我需要其他列中的所有记录。这是我到目前为止所拥有的,但这给了我每一个记录。有什么建议吗?

Public Sub RunQuery(Query As String)
    Try
        SQLCon.Open()
        SQLCmd = New SqlCommand(Query, SQLCon)

        Dim R As SqlDataReader = SQLCmd.ExecuteReader

        While R.Read
            Dim xName As String = (R(0).ToString)
            Dim xDescription As String = (R(1).ToString)
            Dim xPrice As String = (R(3).ToString

            MsgBox(xName & xDescription & xPrice)

        End While

        SQLCon.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

@Steve

这是查询。

SELECT
               --Invoice Details

'ACCREC' AS Type,
l.Name AS Name,
CONVERT(VARCHAR(10),GETDATE(),101) AS Date,
CONVERT(VARCHAR(10),(GETDATE() + 7),101) AS DueDate,
'TEST123' AS InvoiceNumber,
NULL AS Tracking,
NULL AS DiscountRate,
NULL AS BrandingTheme,
'Exclusive' AS LineAmountType,
'DRAFT' AS Status,

                 --Item Details

i.Name AS Description,
FORMAT(ABS(
    SUM(
        CASE 
            WHEN dr.RecordType = 0 
            THEN dr.CountOrQuantity 
            ELSE 0 
            END
            ) 
    - 
    SUM(
        CASE 
            WHEN dr.RecordType = 1 
            THEN dr.CountOrQuantity 
            ELSE 0 
            END)), '0.0000') 
        AS Quantity,
CASE
    WHEN 
        (SUM(CASE WHEN dr.RecordType = 0 THEN dr.CountOrQuantity ELSE 0 END)
            - 
        SUM(CASE    WHEN dr.RecordType = 1 THEN dr.CountOrQuantity ELSE 0 END)) <= 0
    THEN 
        FORMAT(i.Price * -1, '0.0000') 
    ELSE 
        FORMAT(i.Price, '0.0000') 
    END AS UnitAmount,
i.CustomID AS ItemCode

FROM 
Locations l JOIN Deliveries d
ON l.id = d.LocationID

JOIN DeliveryRecords dr 
ON d.ID = dr.DeliveryID

JOIN Items i 
ON dr.ItemID = i.ID

WHERE 
d.ID = (
    SELECT TOP(1) 
        dr.DeliveryID 
    FROM 
        Locations l JOIN Deliveries d
        on l.id = d.LocationID

        JOIN DeliveryRecords dr
        on d.ID = dr.DeliveryID
    WHERE 
        l.OrganizationOwnerGroupID = '5555555' AND
        l.Deleted is NULL AND
        ISNUMERIC(l.CustomID) = 1 AND
        dr.Created <= GETDATE()-2 AND 
        dr.Created >= GETDATE()-4 
    ORDER BY 
        d.ScheduledTime ASC)

GROUP BY 
l.Name,
d.ScheduledTime,
i.Name,
i.CustomID,
i.Price

基本上,我需要一次发票详细信息,但需要每个项目和项目明细。

0 个答案:

没有答案