2015-05-05 42 views
2

困惑考慮以下,過於簡單化DataTable對象:上DataSet.Merge和RowState的

enter image description here

Id是一個自動增量的整數,作用在表的主鍵,而Text是一個簡單的String類型列中。

現在考慮下面的代碼:

private static void Main(string[] args) 
{ 
    var dataSet = new TestingDataSet(); 

    var row = dataSet.TestingTable.AddTestingTableRow("First"); 

    dataSet.AcceptChanges(); 

    var copiedDataSet = dataSet.Copy(); 

    Console.WriteLine("Row after initialization:"); 
    Console.WriteLine(); 

    Console.WriteLine("Text : " + row.Text); 
    Console.WriteLine("State: " + row.RowState.ToString()); 
    Console.WriteLine(); 

    row.Text = "Modifications"; 

    Console.WriteLine("Row after changes:"); 
    Console.WriteLine(); 

    Console.WriteLine("Text : " + row.Text); 
    Console.WriteLine("State: " + row.RowState.ToString()); 
    Console.WriteLine(); 

    dataSet.Merge(copiedDataSet, false); 

    Console.WriteLine("Row after merge:"); 
    Console.WriteLine(); 

    Console.WriteLine("Text : " + row.Text); 
    Console.WriteLine("State: " + row.RowState.ToString()); 
    Console.WriteLine(); 

    Console.WriteLine("Press any key to exit..."); 
    Console.ReadKey(); 
} 

該代碼產生以下結果:

enter image description here

來看最後RowState值。它被修改。怎麼來的?複製的數據集實例沒有更改,我明確將preserveChanges參數設置爲false。所以現在不應該考慮這個行Unchanged

This MSDN article指出,當行合併時,它們的狀態也會跟隨。文章是錯誤的,或者是我的代碼中的東西是?

回答

0

所以我的文檔中挖了一個深一點,連同這個傳來:

如果輸入行有不變一個RowState的和現有的排 有一個RowState的修改,刪除或添加的現有行的 的RowState設置爲已修改。

因此,看起來這是在將未更改的行合併到修改的行時的預期行爲。

可以找到完整文章here

0

我遇到了同樣的問題,最終通過從主DataTable對象中提取所有更改,在覈心對象上運行AcceptChanges,然後在各個表上進行工作並將它們合併回主表中。這樣,核心DataTable對象中的所有內容都是「未修改的」,並且只會吸收合併數據的RowState,而不會發生任何意外調整。

相關問題