0

我正在使用以下代码将 n 行从 DataTable 返回到另一个 DataTable 对象。当第一个数据表记录来自数据库时,它工作正常,但是当我在数据表中手动添加记录时,它会崩溃并且一条消息不显示任何行。我已经调试过,发现currentTable.DefaultView.ToTable().Rows.Cast<DataRow>()这将返回空值。我编写了以下代码来从 DataTable 对象中选择 n 条记录

currentTable = currentTable.DefaultView.ToTable().Rows.Cast<DataRow>().Take(numberOfRecord).CopyToDataTable();

我不知道为什么当我手动创建 DataTable 并在记录来自 DataReader 到 DataTable 时工作正常时它会崩溃。

我正在使用以下代码手动创建 DataTable

DataTable tblCurrent = new DataTable();
//For Adding columns
for (int i = 0; i < fieldCol.Length; i++)
   tblCurrent.Columns.Add(string.Format("Column{0}", i + 1));

//for adding rows.
for (int i = (count + globalClass.sourceConnectionInfo.RowsToSkip); i < lineRows.Length; i++)
{
   DataRow newRow = tblCurrent.NewRow();
   for (int j = 0; j < fieldCol.Length; j++)
      newRow[j] = fieldCol[j];
   tblCurrent.Rows.Add(newRow);
}


//Then for selecting n records
if (currentTable.Rows.Count > 0)
   tblCurrent = tblCurrent.DefaultView.ToTable().Rows.Cast<DataRow>().Take(numberOfRecord).CopyToDataTable();
4

1 回答 1

0

检查以下代码片段,它工作正常,没有任何错误。

 void Main()
    {
    DataTable dt = new DataTable();
        if (dt.Columns.Count == 0)
                {
                    dt.Columns.Clear();
                    dt.Columns.AddRange(
                        new DataColumn[] 
                        {
                            new DataColumn("TStamp", typeof(DateTime)),
                            new DataColumn("C1", typeof(double)),
                            new DataColumn("C2", typeof(double)),
                            new DataColumn("C3", typeof(double))
                            //new DataColumn("Unit", typeof(string))
                        }
                        );
                }

                  Random ovValue = new Random();
                for (int i = 0; i < 10; i++)
                {
                    DataRow dnew = dt.NewRow();
                    dnew["TStamp"] = DateTime.Now.AddMonths(i);
                    dnew["C1"] = ovValue.Next(1, 100);
                    dnew["C2"] = ovValue.Next(1, 100);
                    dnew["C3"] = ovValue.Next(1, 100);
                    dt.Rows.Add(dnew);
                }
                try
                {
                int numberOfRecord = 100;
                int count = dt.Rows.Count;  
                if(numberOfRecord <= count)
                {
                dt = dt.DefaultView.ToTable().Rows.Cast<DataRow>().Take(numberOfRecord).CopyToDataTable();
                }
                else
                {
                /// No Erroe cause if you select more tested at LinqPad
                dt = dt.DefaultView.ToTable().Rows.Cast<DataRow>().Take(100).CopyToDataTable();
                }
                }
                catch(Exception ex)
                {
                Console.WriteLine("Error");
                }

     foreach(DataRow r in dt.Rows)
     {
     Console.WriteLine(r["TStamp"].ToString());
     }

    }

检查您的以下代码块,可能是这使数据表中没有行。dt.Rows.Clear();在选择之前放入我的代码块会导致错误。

//for adding rows.
for (int i = (count + globalClass.sourceConnectionInfo.RowsToSkip); i < lineRows.Length; i++)
{
   DataRow newRow = tblCurrent.NewRow();
   for (int j = 0; j < fieldCol.Length; j++)
      newRow[j] = fieldCol[j];
   tblCurrent.Rows.Add(newRow);
}
于 2012-04-16T10:39:15.393 回答