SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnnFinalProject"].ToString());
SqlCommand cmd = new SqlCommand(@"SELECT c.partID, p.name, p.categoryID, p.price, p.image, p.subCategoryID,
(CASE WHEN categoryID = 1 THEN 'Y' ELSE 'N' END) AS casesYN,
(CASE WHEN categoryID = 3 THEN 'Y' ELSE 'N' END) AS OSYN,
(CASE WHEN categoryID = 7 THEN 'Y' ELSE 'N' END) AS HDDYN,
(CASE WHEN subCategoryID = 1 THEN 'Y' ELSE 'N' END) AS powerYN,
(CASE WHEN subCategoryID = 2 THEN 'Y' ELSE 'N' END) AS processorYN,
(CASE WHEN subCategoryID = 3 THEN 'Y' ELSE 'N' END) AS moboYN,
(CASE WHEN subCategoryID = 4 THEN 'Y' ELSE 'N' END) AS memoryYN,
(CASE WHEN subCategoryID = 5 THEN 'Y' ELSE 'N' END) AS graphicsYN,
(CASE WHEN subCategoryID = 6 THEN 'Y' ELSE 'N' END) AS opticalYN,
(CASE WHEN subCategoryID = 7 THEN 'Y' ELSE 'N' END) AS soundYN
FROM configuration c JOIN parts p ON p.partID = c.partID
WHERE c.customID = @ID", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string[] cases = { dr["partID"].ToString() };
}
如何将只有'casesYN'值设置为Y的partID插入到个案数组中,因为现在它会在sql查询中插入每个'partID'。
答案 0 :(得分:5)
我假设您不想更改SQL查询。
使用List<string>
代替string[]
,因为在其中添加新项目会更容易:
var cases = new List<string>();
while (dr.Read())
{
if(dr["casesYN"].ToString() == "Y")
cases.Add(dr["partID"].ToString());
}
如果你真的需要一个数组,你可以在循环结束后调用ToArray()
方法:
string[] casesArray = cases.ToArray();
但是文件顶部需要using System.Linq
。