按字母顺序排序DataAdapter - VB.Net

时间:2010-11-30 12:48:47

标签: asp.net vb.net tsql

这应该是一个相当简单的。我正在创建一个数据集,其中包含一个我想要排序的描述字段。我想对dataadapter进行排序而不是在我的SQL中的原因是我已经按照具有特定值的结果进行排序。

我的SQL看起来像这样:

SELECT pif_desc, pif_fund, psf_end, (CASE WHEN SUM(pmi_units) IS Null THEN 0 ELSE SUM(pmi_units) END) As fundunits 
    FROM tbl_mem INNER JOIN tbl_sfunds
    ON pm_scheme = psf_scheme
    INNER JOIN tbl_invfun
    ON tbl_fund = tbl_fund
    LEFT JOIN pe_minv
    ON pmi_fund = pif_fund AND pm_member = pmi_member
    WHERE pm_member = @pm_member 
    GROUP BY pif_desc, pif_fund, psf_end
    ORDER BY fundunits DESC

我的VB看起来像这样:

Dim cmd As New SqlCommand("getMembersFundsDCGENST", conn)
        cmd.CommandType = CommandType.StoredProcedure

        Dim p_pm_member As New SqlParameter("@pm_member", SqlDbType.Int)
        p_pm_member.Value = pm_member
        cmd.Parameters.Add(p_pm_member)

        Dim p_period_closing_date As New SqlParameter("@closingdate", SqlDbType.DateTime)
        p_period_closing_date.Value = period_closing_date
        cmd.Parameters.Add(p_closing_date)

        Dim da As New SqlDataAdapter(cmd)
        da.Fill(ds)

我想通过pif_desc对datarows进行排序是ds.tables(0),但仍然有基金单元的行> 0首先列出。

我也对如何实现正确排序的其他建议持开放态度。

2 个答案:

答案 0 :(得分:1)

只要在代码中应用排序或过滤,就应该使用

DataView。看一看。

答案 1 :(得分:1)

您正在寻找的输出更像是这样吗?

pif_desc    fundunits
ABCDEF      6
CBCDEG      2
DEFGHI      4
ADFKHG      0
BFJSKL      0
XDFDKF      0

如果是这样,请尝试以下SQL查询:

SELECT  pif_desc,
        pif_fund,
        psf_end,
        (CASE WHEN fundunits IS Null THEN 0
        ELSE fundunits END) As fundunits,
        (CASE WHEN fundunits IS Null THEN 0
        ELSE 1 END) As pif_order
FROM    (
            SELECT  pif_desc,
                    pif_fund,
                    psf_end,
                    SUM(pmi_units) As fundunits
            FROM    tbl_mem
            INNER   JOIN tbl_sfunds
                    ON pm_scheme = psf_scheme
            INNER   JOIN tbl_invfun
                    ON tbl_fund = tbl_fund
            LEFT    JOIN pe_minv
                    ON pmi_fund = pif_fund AND pm_member = pmi_member
            WHERE   pm_member = @pm_member 
            GROUP   BY pif_desc, pif_fund, psf_end
        ) pifs
ORDER   BY pif_order,pif_desc