我試圖使用SQL查詢到C#列表獲取計數(1)值的列表在C#
我的示例代碼如下
"Select count(1) as Values, SM.Name from SM INNER JOIN Bill on SM.ID = Bill.AL INNER JOIN Details on Bill.ID = Details.ID"
得到一個表中的列數
我需要將計數添加到列表中<>有人能告訴我該怎麼做嗎?
由於該計數值沒有我給它取名爲「值」 我試圖獲得價值爲下面的代碼
public IHttpActionResult Post(Brands brand)
{
DataTable dt = new DataTable();
List<Brands> list = new List<Brands>();
try
{
SqlConnection con =
new SqlConnection(
"MyConnectionString");
con.Open();
var query = "Select count(1) as Values, SM.Name from SM INNER JOIN Bill on SM.ID = Bill.AL INNER JOIN Details on Bill.ID = Details.ID";
SqlCommand com = new SqlCommand(query, con);
com.ExecuteNonQuery();
con.Close();
SqlDataAdapter adptr = new SqlDataAdapter(com);
adptr.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
Brands GetAll = new Brands();
GetAll.Count = Convert.ToInt32(dt.Rows[i]["Values"]);
GetAll.Name = dt.Rows[i]["Name"].ToString();
list.Add(GetAll);
}
}
catch (Exception e)
{
}
return Ok(list);
}
}
}
這裏'1'是什麼,是列名 –
COUNT以列名作爲參數。不清楚Count(1) –
@ un-lucky count(1)的含義是否等於count(*) – Kason