從數據庫讀取的三張查詢語句得到的表:
A庫 time,id,err B庫 time,id,err C庫 time,id,err
1:00,1,34 1:00,1,55 1:00,1,88
2:00,1,38 2:00,1,33 2:00,1,99
3:00,1,89 3:00,1,43 3:00,1,167
這是三個數據庫分別查的
效果表: time id errA errB errC
1:00 1 34 55 88
2:00 1 38 33 99
3:00 1 89 43 167
在後台代碼繪制datatable
DataTable dt = new DataTable("rawData");
//初始化數據
//新建行和列
dt.Columns.Add("Time");
dt.Columns.Add("ErrA");
dt.Columns.Add("ErrB");
dt.Columns.Add("ErrC");
dt.Rows.Add("", "",0, 0, 0);
dt.Rows.Add("", "",0, 0, 0);
dt.Rows.Add("", "",0, 0, 0);
接下來要用循環語句把數據一個一個加進datatable表內,好像是用foreach語句不太會寫,要和效果表一樣的那個,跪求大神
3庫一一對應
DataTable dt = new DataTable("rawData");
//初始化數據
//新建行和列
dt.Columns.Add("Time");
dt.Columns.Add("ErrA");
dt.Columns.Add("ErrB");
dt.Columns.Add("ErrC");
dt.Columns.Add("id");//id不要丟啊
//A B C三庫 是你已經得到的
DataTable a = new DataTable();
DataTable b = new DataTable();
DataTable c = new DataTable();
//循環A庫 循環一條A 找B和C 再插入到結果表裡
foreach (DataRow drA in a.Rows)
{
//先得到B庫和C庫的err
DataRow drB = b.Select("id=" + drA["id"] + " and time=" + drA["time"])[0];
DataRow drC = b.Select("id=" + drA["id"] + " and time=" + drA["time"])[0];
//新建一行
DataRow dr = dt.NewRow();
//賦值
dr["Time"] = drA["time"];
dr["ErrA"] = drA["err"];
dr["ErrB"] = drB["err"];
dr["ErrC"] = drC["err"];
dr["id"] = drA["id"];
//構造完dr後 插入dt
dt.Rows.Add(dr);
}