C# 将sql case语句放入组合框

C# 将sql case语句放入组合框,c#,.net,sql-server,C#,.net,Sql Server,我有这样一个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'" & _ "whe

我有这样一个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"

如何将所有的case语句条件(如new、qualified、registered和flowstatus)加载到vb.net上的组合框中?你能给我举个例子吗?我已经尝试了很长时间。谢谢。

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

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" },
    .... 
};
第二种方法是创建一个表或临时表,其中包含如下值列表:

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');
然后,在使用从该表中读取并填充的数据源之前,可以以相同的方式使用它。但是这个解决方案将使您的查询更容易。您可以这样连接此表:

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;

请注意:您的表需要以这种方式进行重构。如果可能的话,最好去掉这些状态名称,并在第二个表中添加一个新的表状态,其id为外键。

是否要将这些状态的单独列表加载到组合框中?不是来自这个查询?我的意思是,将有2个组合框,每个组合框。对于状态,它将是新的、合格的、被列入黑名单的和被拒绝的,而对于flowstatus,正在审查的,可能是普通的和可能是排他的。
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;