在下面的示例中,它返回了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)
{
答案 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);
}
只是为了帮助别人..