-4

我有一个这样的 sql case 语句

select Distinct y.SupplierID,y.Name,y.AddWho , 
        "StatusDesc=CASE when  y.status='N' then 'NEW' " & _
        "when y.status='B' then 'BLACKLISTED'" & _
        "when y.status='Q' then 'QUALIFIED'" & _
        "when y.status='R' then 'REJECTED' end , " & _
        "FlowStatusDesc = CASE when  y.flowstatus='RC' then 'UNDER REVIEW'" & _
        "when y.flowstatus='PE' then 'POTENTIAL EXCLUSIVE'" & _
        "when y.flowstatus='PO' then 'POTENTIAL ORDINARY' ELSE '' end," & _
        "OrderNo=case when y.status='N' and flowstatus='' then '1'" & _
        "when y.status='N' and y.flowstatus<>'' then '2'    " & _
        "when y.status='R' and y.flowstatus='' then '3'" & _
        "when y.status='R' and y.flowstatus<>'' then '4'" & _
        "when y.status='Q' and y.flowstatus='' then '5'" & _
        "when y.status='Q' and y.flowstatus<>'' then '6'" & _
        "when y.status='B' and y.flowstatus=''  then '7'" & _
        "when y.status='B' and y.flowstatus<>'' then '8' else '9' end " & _
        "from AP_Supplier y" & _
        " left outer join SC_User u on y.addwho=u.userid " & _
        "left outer join SC_Company co on co.companycode=u.companycode " & _
        "where flowstatus is not null " & _
        "group by y.SupplierID,y.name,y.status,y.flowstatus,y.addwho " & _
        "order by orderno"

我如何将所有案例语句条件(如“new”、“qualified”、“registered”和 flowstatuses 加载到 vb.net 上的组合框中?你能给我一个例子吗?我已经尝试这样做了一段时间。谢谢。

4

1 回答 1

0

有很多方法可以做到这一点,第一种是从列表中填充组合框。在这种情况下,您应该有一个类,例如:

public class Status(){
    public string Symbol { get; set; } // or Id
    public string Name { get; set; }
}

然后是您拥有的状态列表:

var ds = new List<Status>(){
    new Status { Symbol = "N", Name = "New" },
    new Status { Symbol = "Q", Name = "Qualified" },
    .... 
};

然后,您可以使用以下两个属性轻松地从此列表中填充组合框:

像这样:

YourcomboboxName.DataSource = ds;
YourcomboboxName.ValueMember = "Symbol";
YourcomboboxName.DisplayMember = "Name";

第二种方法是创建一个表或临时表,其中包含这些值列表,如下所示:

CREATE TABLE Statuses(StatusId INT, 
                      StatusSymbol VARCHAR(2), 
                      StatusName VARCHAR(20));

INSERT INTO Statuses(StatusId, StatusSymbol, STatusName) VALUES
                    (1, 'N' , 'NEW'),
                    (2, 'B' , 'BLACKLISTED'),
                    (3, 'Q' , 'QUALIFIED'),
                    (4, 'R' , 'REJECTED'),
                    (5, 'UR', 'UNDER REVIEW'),
                    (6, 'PE', 'POTENTIAL EXCLUSIVE'),
                    (7, 'PO', 'POTENTIAL ORDINARY');

然后,您可以在使用从该表读取并填充它的数据源之前以相同的方式使用它。但是此解决方案将使您的查询更容易。您可以JOIN像这样使用此表:

SELECT DISTINCT 
  y.SupplierID,
  y.Name,
  y.AddWho,
  StatusDesc = s.STatusName,
  FlowStatusDesc = CASE WHEN ... END
  orderno = CASE WHEN ...
FROM AP_Supplier y
INNER JOIN statuses   s ON y.status       = s.StatusSymbol
LEFT JOIN SC_User     u ON y.addwho       = u.userid
LEFT JOIN SC_Company co ON co.companycode = u.companycode 
WHERE flowstatus IS NOT NULL
GROUP BY y.SupplierID,
         y.name,
         y.status,
         y.flowstatus,
         y.addwho
ORDER BY orderno;

请注意:如果可能,您的表需要重构,最好去掉这些状态名称,并Statuses在第二个表中创建一个以 id 作为外键的新表。

于 2012-12-24T08:03:07.517 回答