ImportRow无效

时间:2011-08-12 07:53:13

标签: .net asp.net ado.net

在下面的示例中,它返回了2行,并且importrow不会抛出任何异常,但是当我看到foreach之后的数据表为空时它应该还有2行。

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)
    {

3 个答案:

答案 0 :(得分:5)

只是一个预感,但也许你正在清理表中有行集合的事实导致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,这可能会有问题。

答案 1 :(得分:0)

蒂姆是对的。实施例。

    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

答案 2 :(得分: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);
}

只是为了帮助别人..