我有兩個DataTable DT1有標識countryID DT2有網址和標識添加兩個數據表中具有獨特的列
我想補充這兩個databales 如果我使用
foreach (DataColumn col in dt.Columns)
{
dt1.Columns.Add(col.ColumnName, col.DataType);
}
越來越像列錯誤網址已經屬於另一個數據庫 請幫助我如何添加這兩個表格
我有兩個DataTable DT1有標識countryID DT2有網址和標識添加兩個數據表中具有獨特的列
我想補充這兩個databales 如果我使用
foreach (DataColumn col in dt.Columns)
{
dt1.Columns.Add(col.ColumnName, col.DataType);
}
越來越像列錯誤網址已經屬於另一個數據庫 請幫助我如何添加這兩個表格
如果你的目標是建立一個單一的DataTable
有三列(Id
,CountryId
和Url
),只需創建每個Id
列主鍵,只需撥打dt1.Merge(dt2)
。
例子:
var dt1 = new DataTable();
var id1 = dt1.Columns.Add("Id");
dt1.Columns.Add("CountryId");
dt1.PrimaryKey = new []{id1};
dt1.Rows.Add(new []{"1", "Austria"});
dt1.Rows.Add(new []{"2", "Iceland"});
var dt2 = new DataTable();
var id2 = dt2.Columns.Add("Id");
dt2.Columns.Add("Url");
dt2.PrimaryKey = new []{id2};
dt2.Rows.Add(new []{"1", "www.austria.at"});
dt2.Rows.Add(new []{"2", "www.iceland.is"});
dt1.Merge(dt2);
dt1
現在看起來像:
感謝多米尼克.. – MuthuKumar
我有一個疑問,我們才能建立外鍵的數據表中手動 – MuthuKumar
你不需要一個外鍵,但是一個主鍵。是的,你可以像我在上面的代碼示例中那樣手動設置它。 – sloth