2011-08-12 17 views
1

在下面的例子中,它返回了2行,並且importrow沒有拋出任何異常,但是當我看到foreach後的數據表爲空時,它也應該有2行。ImportRow不工作

foreach (int refmDossierId in distinctREFMDossierIds) 
{ 
    DataRow[] datarows = 
        _uc090_WingsIntegrationDataSet.WingsBookingInterface.Select("REFMDossierID =" + refmDossierId); 
    if(datarows.Length>0) 
    { 
     foreach(DataRow dr in datarows) 
     { 
      _uc090_WingsIntegrationDataSet.WingsBookingInterface.Clear(); 
      _uc090_WingsIntegrationDataSet.WingsBookingInterface.ImportRow(dr); 
     } 
    } 

    //2.  foreach master row 
    foreach (UC090_WingsIntegrationDataSet.WingsBookingInterfaceRow row in _uc090_WingsIntegrationDataSet.WingsBookingInterface.Rows) 
    { 
+0

我思考的問題是,我是裏面的清理foreach,我在foreach之前移動了那行,但是它沒有工作,它進入foreach兩次,但最後datatable是空的。 –

+0

我看到的一個問題是,您在每次導入之前清空表格,所以我最多隻希望看到一行。然而,也許關於'_uc090_WingsIntegrationDataSet.WingsBookingInterface'和'UC090_WingsIntegrationDataSet.WingsBookingInterfaceRow'是什麼,以及爲什麼你在第一步中使用'DataRow'和第二步中的自定義類型可能會給我們更多的解釋。 – Tim

+0

的確,我期望有一行,但我解決了這個問題,而且我仍然看不到任何行。這些是有類型的數據集,我只需要過濾數據表,然後遍歷結果。 –

回答

4

只是一種預感,但也許你正在清理從導致DataRow中收集到處於分離狀態你有行的集合表中的事實呢?如果是這樣的情況下,嘗試Clone作出新的DataTable具有相同的架構,但沒有行,然後做Import

foreach (int refmDossierId in distinctREFMDossierIds) 
{ 
    DataTable tempTable = _uc090_WingsIntegrationDataSet.WingsBookingInterface.Clone(); 

    DataRows[] datarows = _uc090_WingsIntegrationDataSet.WingsBookingInterface.Select("REFMDossierID = " + refmDossierId); 

    if (datarows.Length > 0) 
    { 
     foreach(DataRow dr in datarows) 
     { 
      tempTable.ImportRow(dr); 
     } 
    } 

    foreach (UC090_WingsIntegrationDataSet.WingsBookingInterfaceRow row in tempTable.Rows) 
    { 
     // Do your processing here 
    } 
} 

你當前的代碼會擦桌子的第一個ID後,由方式 - 如果您有多個ID處理,可能會出現問題。

0

Tim是正確的。例。

Private Sub SortAddress(ds As DataSet) 

     Dim sourceTable As DataTable = ds.Tables(0) 


     Dim targetTable As DataTable = ds.Tables(0).Clone() 

     Dim sorted = sourceTable.Rows.Cast(Of DataRow)().OrderBy(Function(row) _ 
                    _matchFirstNumberRegEx.Match(row("Col1")).Value) _ 
                 .ThenBy(Function(row) _ 
                    _matchFirstNumberRegEx.Replace(row("Col1"), "")).ToList() 

     ds.Tables.Remove(ds.Tables(0)) 

     For Each row In sorted 

      targetTable.ImportRow(row) 
     Next 

     ds.Tables.Add(targetTable) 

    End Sub 
0

在一個簡單的形式(C#編碼)以上回答可以被看作是:

string dt1Query = "SELECT ColumnName FROM tableName"; 
DataTable dt1 = new DataTable(); 
using (SqlCommand cmd = new SqlCommand(dt1Query, objConn)) 
{ 
    dt1.Load(cmd.ExecuteReader()); 
} 

DataTable dt2 = dt1.Clone(); 

foreach (DataRow dt1Row in dt1.Rows) 
{ 
    // some code here if need to work anything out before writing to dt2 
    dt2.ImportRow(dt1Row); 
} 

只是爲了幫助別人..