[英]Returning n number of rows in datatable format from datatable object
I am using following code to return n number of rows from DataTable to another DataTable object. 我正在使用以下代码将n个行从DataTable返回到另一个DataTable对象。 It is working fine when in first datatable records comes from database but when I mannualy added records in datatable then it crash and a message displays no rows . 当第一个数据表中的记录来自数据库时,它工作正常,但是当我手动在数据表中添加记录时,它崩溃了,并且消息中没有显示任何行 。 I had debugeed and found that currentTable.DefaultView.ToTable().Rows.Cast<DataRow>()
this will returns null value. 我已经调试过,发现currentTable.DefaultView.ToTable().Rows.Cast<DataRow>()
会返回空值。 I had written following code to select n records from DataTable object 我写了以下代码从DataTable对象中选择n条记录
currentTable = currentTable.DefaultView.ToTable().Rows.Cast<DataRow>().Take(numberOfRecord).CopyToDataTable();
I dont know why it is crashed when I manualy creating DataTable and working fine when records comes from DataReader to DataTable. 我不知道为什么当我手动创建DataTable并当记录从DataReader到DataTable时工作正常时它会崩溃。
I am using following code to create DataTable manually 我正在使用以下代码手动创建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();
Check the following code snippet, it is working fine without any error. 检查以下代码段,它工作正常,没有任何错误。
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());
}
}
Check your following code block, may be this make the no rows in the datatable. 检查您的以下代码块,可能是这使数据表中没有行。 put dt.Rows.Clear();
把dt.Rows.Clear();
in my code block before the selection it will cause error. 在我的代码块中选择之前,它将导致错误。
//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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.