DataRow newDataRow;
DataColumn newDataColumn;
DataTable UserDataTable;
UserDataTable = new DataTable("stncount");//建立空的统计表
newDataColumn = new DataColumn();
newDataColumn.DataType = System.Type.GetType("System.String");
newDataColumn.ColumnName="市区名称";
UserDataTable.Columns.Add(newDataColumn);//把字段“市区名称”加入空表
newDataColumn=new DataColumn();
newDataColumn.DataType=System.Type.GetType("System.String");
newDataColumn.ColumnName="设备数量";
UserDataTable.Columns.Add(newDataColumn);
newDataColumn=new DataColumn();
newDataColumn.DataType=System.Type.GetType("System.String");
newDataColumn.ColumnName="正常设备";
UserDataTable.Columns.Add(newDataColumn);
newDataColumn=new DataColumn();
newDataColumn.DataType=System.Type.GetType("System.String");
newDataColumn.ColumnName="故障设备";
UserDataTable.Columns.Add(newDataColumn);
newDataColumn=new DataColumn();
newDataColumn.DataType=System.Type.GetType("System.String");
newDataColumn.ColumnName="已修复设备";
UserDataTable.Columns.Add(newDataColumn);
DataSet DS1 = new DataSet();
DS1.Tables.Add(UserDataTable);
int i;
DataSet DS2=new DataSet();
DS2=DBCls.DBCls.dbbind("select cityname from cityinf","cityinf");//得到所有市区名称
for(i=0;i<=DS2.Tables["cityinf"].Rows.Count-1;i++)
{
newDataRow=UserDataTable.NewRow();
newDataRow[0]=DS2.Tables["cityinf"].Rows[i][0].ToString();
string strSQL1="select count(distinct(stntel)) from transinf where " +
" cityname='" +DS2.Tables["cityinf"].Rows[i][0].ToString() + "'and stnstyle='" + style + "' ";
DataSet DS3=new DataSet();
DS3=DBCls.DBCls.dbbind(strSQL1,"countstn");
newDataRow[1]=DS3.Tables["countstn"].Rows[0][0].ToString();
string strSQL2="select count(distinct (stntel)) from transalarm where " +
" alarmtime between '" + startDate + "' and '" + endDate + " ' " +
" and stntel in (select stntel from transinf where cityname='" + DS2.Tables["cityinf"].Rows[i][0] + "')";
DataSet badstn=new DataSet();
badstn=DBCls.DBCls.dbbind(strSQL2,"badstn");
newDataRow[3]=badstn.Tables["badstn"].Rows[0][0].ToString();
UserDataTable.Rows.Add(newDataRow);
}
dataGrid1.DataSource=DS1.Tables["stncount"];
dataGrid1.DataMember="stncount";
5 个解决方案
#1
UserDataTable = new DataTable("stncount");//建立空的统计表
在这里出错吗?
在这里出错吗?
#2
你给的代码不能运行。帮不了你!up
#3
你写的太复杂了,效率也不高,不需要这样的,
DataTable UserDataTable = new DataTable( ) ;
OleDbCommand cmd = con.CreateCommand() ;
cmd.CommandText = "select cityname , ( select count(distinct(stntel)) from transinf where cityname = a.cityname and stnstyle = '1' ) as b , (select count() ... ) as b , ... from cityinf a group by a.cityname " ; //用SQL取想要的数据,想取多少列就取多少列。
OleDbDataAdapter dataAdapter = new OleDbDataAdapter( cmd ) ;
dataAdapter.Fill( UserDataTable ) ;
再从UserDataTable中取不就结了,何必要做这么复杂。
DataTable UserDataTable = new DataTable( ) ;
OleDbCommand cmd = con.CreateCommand() ;
cmd.CommandText = "select cityname , ( select count(distinct(stntel)) from transinf where cityname = a.cityname and stnstyle = '1' ) as b , (select count() ... ) as b , ... from cityinf a group by a.cityname " ; //用SQL取想要的数据,想取多少列就取多少列。
OleDbDataAdapter dataAdapter = new OleDbDataAdapter( cmd ) ;
dataAdapter.Fill( UserDataTable ) ;
再从UserDataTable中取不就结了,何必要做这么复杂。
#4
tyrone98(林林) ,你好,我选择的几个字段数据库里都没有的,是统计出来的新字段!!
所以,只能一列一列的写出来拉!!!! :(
所以,只能一列一列的写出来拉!!!! :(
#5
to qianli918(千里马) 不是这里出错的,异常总是 出现无法创建字段stncount的子列表!!
#1
UserDataTable = new DataTable("stncount");//建立空的统计表
在这里出错吗?
在这里出错吗?
#2
你给的代码不能运行。帮不了你!up
#3
你写的太复杂了,效率也不高,不需要这样的,
DataTable UserDataTable = new DataTable( ) ;
OleDbCommand cmd = con.CreateCommand() ;
cmd.CommandText = "select cityname , ( select count(distinct(stntel)) from transinf where cityname = a.cityname and stnstyle = '1' ) as b , (select count() ... ) as b , ... from cityinf a group by a.cityname " ; //用SQL取想要的数据,想取多少列就取多少列。
OleDbDataAdapter dataAdapter = new OleDbDataAdapter( cmd ) ;
dataAdapter.Fill( UserDataTable ) ;
再从UserDataTable中取不就结了,何必要做这么复杂。
DataTable UserDataTable = new DataTable( ) ;
OleDbCommand cmd = con.CreateCommand() ;
cmd.CommandText = "select cityname , ( select count(distinct(stntel)) from transinf where cityname = a.cityname and stnstyle = '1' ) as b , (select count() ... ) as b , ... from cityinf a group by a.cityname " ; //用SQL取想要的数据,想取多少列就取多少列。
OleDbDataAdapter dataAdapter = new OleDbDataAdapter( cmd ) ;
dataAdapter.Fill( UserDataTable ) ;
再从UserDataTable中取不就结了,何必要做这么复杂。
#4
tyrone98(林林) ,你好,我选择的几个字段数据库里都没有的,是统计出来的新字段!!
所以,只能一列一列的写出来拉!!!! :(
所以,只能一列一列的写出来拉!!!! :(
#5
to qianli918(千里马) 不是这里出错的,异常总是 出现无法创建字段stncount的子列表!!