日期:2014-05-17 浏览次数:20839 次
DataTable dt = DBHelper.GetDataTable(sql); //添加列名 DataColumn dc = new DataColumn(); dc.ColumnName = "月用电量"; DataColumn dc2 = new DataColumn(); dc2.ColumnName = "年用电量"; dt.Columns.Add(dc); dt.Columns.Add(dc2); DataTable dt2 = null; DataTable dt3 = null; string times, msql; DateTime timess; for (int m = 0; m < dt.Rows.Count; m++) { times = dt.Rows[m]["Datatime"].ToString(); //记录时间 timess = Convert.ToDateTime(times); msql = "select sum(ElectricityConsumption) as 月用电量 From OF_Node_Report_Elec where NodeID = "+dt.Rows[m]["NodeID"].ToString()+" and Datatime <= '" + times + "' and Datatime >= '" + timess.Year + "/" + timess.Month + "/01'"; dt2 = DBHelper.GetDataTable(msql); dt.Rows[m]["月用电量"] = dt2.Rows[0]["月用电量"]; msql = "select sum(ElectricityConsumption) as 年用电量 From OF_Node_Report_Elec where NodeID = " + dt.Rows[m]["NodeID"].ToString() + " and Datatime <= '" + times + "' and Datatime >= '" + timess.Year + "/01/01'"; dt3 = DBHelper.GetDataTable(msql); dt.Rows[m]["年用电量"] = dt3.Rows[0]["年用电量"]; dt2.Clear(); dt3.Clear(); }