[英]How to check if a column has no value (empty) in all rows in data table in c#
[英]C# Data Table Rows value is empty string, but there is Null value
當我查詢無記錄的sqlStr時,dt.Rows.Count的值始終等於1,並且dt.Rows [0] [0]和dt.Rows [0] [1]的值是空字符串。
但是我在數據庫中執行sqlStr。 是空的 這是我不知道為什么的數據庫屏幕截圖 。
string sqlStr = "select SUM(userIncome),SUM(userSpend) from tb_billlist where userPhone = '" + userPhone + "' and " +
"billTime between '" + beginDate + "' and '" + endDate + "'";
//execute sql
DataTable dt = new DataTable();
dt = MysqlHelper.ExecuteDataTable(sqlStr);
if (dt.Rows.Count > 0)
{
userBillInfo.userAllIncome = (float.Parse)(dt.Rows[0][0].ToString());
userBillInfo.userAllSpend = (float.Parse)(dt.Rows[0][1].ToString());
}
else
{
userBillInfo.userAllIncome = 0;
userBillInfo.userAllSpend = 0;
}
為了實現此目標,您可以更改查詢,如果為“空值總和”,則返回值為0:
string sqlStr = "select COALESCE(SUM(userIncome), 0), COALESCE(SUM(userSpend), 0) from tb_billlist where userPhone = '" + userPhone + "' and " +
"billTime between '" + beginDate + "' and '" + endDate + "'";
//execute sql
...
因此,您將使用0作為字段的返回值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.