0
如何在第一個數據表中匹配列名「station」並更新其他值,如何使用同一列更新另一個數據表中的數據表。如果「station」列不是然後添加新行datatable1並複製數據表中的數據2如何從另一個數據表更新或添加數據表中的新行
DataSet ds = new DataSet();
DataSet ds1 = new DataSet();
ds1.ReadXml(HttpContext.Current.Server.MapPath("XML/myFile.xml"));
DataTable dt1 = ds1.Tables[0];
//dt1.Merge(dt);
//dt1.AcceptChanges();
//DataView dw = new DataView(dt);
//DataTable dt2 = dw.ToTable(true, "DateTime", "Station", "Max_Temp", "Min_Temp", "Weather_Detail");
for (int i = 0; i <= dt.Rows.Count; i++)
{
for (int j = 0; j <= dt1.Rows.Count; j++)
//{
if (dt1.Rows[i]["Station"].ToString().Contains(dt.Rows[i]["Station"].ToString()))
{
//&& dt1.Rows[i]["Max_Temp"].ToString() == dt.Rows[j]["Max_Temp"].ToString() && dt1.Rows[i]["Station"].ToString() == dt.Rows[j]["Station"].ToString())
dt1.Rows[i]["Max_Temp"] = dt.Rows[i]["Max_Temp"];
dt1.Rows[i]["Min_Temp"] = dt.Rows[i]["Min_Temp"];
dt1.Rows[i]["Weather_Detail"] = dt.Rows[i]["Weather_Detail"];
dt1.Rows[i]["DateTime"] = dt.Rows[i]["DateTime"];
}
else
{
DataRow dr = null;
dr = dt1.NewRow();
dt1.Rows.Add(dr);
dt1.Rows[i]["Station"]= dt.Rows[i]["Station"].ToString();
dt1.Rows[i]["Max_Temp"] = dt.Rows[i]["Max_Temp"];
dt1.Rows[i]["Min_Temp"] = dt.Rows[i]["Min_Temp"];
dt1.Rows[i]["Weather_Detail"] = dt.Rows[i]["Weather_Detail"];
dt1.Rows[i]["DateTime"] = dt.Rows[i]["DateTime"];
}
}
dt1.AcceptChanges();
}