[ad_1]
Need to sum last column SATI
我尝试过的:
while (rd.Read()) { usersNB.Add(new userNB { //my sql Vrijeme = rd.IsDBNull(rd.GetOrdinal("VRIJEME")) ? string.Empty : rd.GetString(rd.GetOrdinal("VRIJEME")), Osoba = rd.GetString("OSOBA"), Sati = rd.IsDBNull(rd.GetOrdinal("SATI")) ? string.Empty : rd.GetInt32(rd.GetOrdinal("SATI")).ToString() }
解决方案1
所以,你可能想写这样的东西:
C#
long runningTotal = 0; while (rd.Read()) { string sati = string.Empty; if (!rd.IsDBNull(rd.GetOrdinal("SATI")) { int satiVal = rd.GetInt32(rd.GetOrdinal("SATI")); runningTotal += satiVal; sati = satiVal.ToString(); } string vrijeme = rd.IsDBNull(rd.GetOrdinal("VRIJEME")) ? string.Empty : rd.GetString(rd.GetOrdinal("VRIJEME")); usersNB.Add(new userNB{ Vrijeme = vrijeme, Osoba = rd.GetString("OSOBA"), sati }); }
您所做的就是在浏览阅读器时保持运行总计。 runningTotal 是 long 而不是 int 的原因只是为了满足最大或最小(下溢)值的溢出。
[ad_2]
コメント