怎样在DataTable中取前几条数据
已经从数据库中取出了数据,现在想从datatable中只取出前7条数据,应该怎么写,请高手解答。谢谢
[解决办法]
dim i as integer
for i = 0 to 6
.... = dt.rows(i)("字段名")
next
[解决办法]
DataTable dt=new DataTable();
//然后通过sql语句绑定到dt
DataTable dt2=new DataTable();
dt2=dt.Clone();
for(int i=0;i<7;i++)
{
dt2.Rows.Add(dt.Rows[i]);
}
[解决办法]
你可以用DataTable.Clone()复制一个完全相同的DataTable结构,再对前面一个DataTable做循环,取出前7条数据,移到新的DataTable里面来。。。
[解决办法]
foreach(DataRow dr in dt.Rows)
{
if(i==7)break;
i+=1;
}
或for循环
[解决办法]
DataColumn myDataColumn;
DataTable myTable = new DataTable ();
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.Int32");
myDataColumn.ColumnName = "ID";
myDataColumn.AutoIncrement = true;
myDataColumn.AutoIncrementSeed = 0;
myDataColumn.AllowDBNull = false;
myDataColumn.Unique = true;
myTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "name";
myTable.Columns.Add(myDataColumn);
for(int i=0;i<10;i ++)
{
DataRow myDataRow = myTable.NewRow();
myDataRow["name"] = i.ToString();
myTable.Rows.Add(myDataRow);
}
DataRow[] foundRow = myTable.Select("ID<3");