我从SQL SERVER上获取DATASET,
因为表数据可能非常多.所以用SELECT * FROM TABLE肯定不现实,
如果我想用分批获取,怎么做? 在不知道表里哪个是主键的情况下. 只知道一个表名,其它什么也不知道,怎么分批取出?
因为表数据可能非常多.所以用SELECT * FROM TABLE肯定不现实,
如果我想用分批获取,怎么做? 在不知道表里哪个是主键的情况下. 只知道一个表名,其它什么也不知道,怎么分批取出?
将指定的 DataSet、DataTable 或 DataRow 对象的数组合并到当前的 DataSet 或 DataTable 中。private void DemonstrateMergeTableAddSchema(){
// Create a DataSet with one table, two columns, and ten rows.
DataSet ds = new DataSet("myDataSet");
DataTable t = new DataTable("Items"); // Add table to the DataSet
ds.Tables.Add(t); // Create and add two columns to the DataTable
DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"),"");
c1.AutoIncrement=true;
DataColumn c2 = new DataColumn("Item", Type.GetType("System.Int32"),"");
t.Columns.Add(c1);
t.Columns.Add(c2); // Set the primary key to the first column.
t.PrimaryKey = new DataColumn[1]{ c1 }; // Add RowChanged event handler for the table.
t.RowChanged+= new DataRowChangeEventHandler(Row_Changed); // Add ten rows.
for(int i = 0; i <10;i++){
DataRow r=t.NewRow();
r["Item"]= i;
t.Rows.Add(r);
} // Accept changes.
ds.AcceptChanges();
PrintValues(ds, "Original values"); // Create a second DataTable identical to the first, with
// one extra column using the Clone method.
DataTable t2 = t.Clone();
t2.Columns.Add("extra", typeof(string)); // Add two rows. Note that the id column can't be the
// same as existing rows in the DataSet table.
DataRow newRow;
newRow=t2.NewRow();
newRow["id"]= 12;
newRow["Item"]=555;
newRow["extra"]= "extra Column 1";
t2.Rows.Add(newRow); newRow=t2.NewRow();
newRow["id"]= 13;
newRow["Item"]=665;
newRow["extra"]= "extra Column 2";
t2.Rows.Add(newRow); // Merge the table into the DataSet.
Console.WriteLine("Merging");
ds.Merge(t2,false,MissingSchemaAction.Add);
PrintValues(ds, "Merged With Table, Schema Added");
}private void Row_Changed(object sender, DataRowChangeEventArgs e){
Console.WriteLine("Row Changed " + e.Action.ToString() + "\t" + e.Row.ItemArray[0]);
}private void PrintValues(DataSet ds, string label){
Console.WriteLine("\n" + label);
foreach(DataTable t in ds.Tables){
Console.WriteLine("TableName: " + t.TableName);
foreach(DataRow r in t.Rows){
foreach(DataColumn c in t.Columns){
Console.Write("\t " + r[c] );
}
Console.WriteLine();
}
}
}
我是想从数据库表里取出数据填充到DATASET时,怎么分批取....
**:返回的行数,不过还是不能满足你的要求。如果表的主关键字有规律可循比如1~1000,那个select * from table where pk_col > 101 and pk_col < 200然后再Fill到DataSet同一个表里,虽然没试过,不过应该是在后面累加的吧。
如果select语句所返回结果集合中的行数太多时,可以使用TOP n [PERCENT]选项限制返回的数据行数。Top n说明返回查询结果集合中的前n(n取值范围为0到4294967295)行数,使用PERCENT选项时,说明Top n PERCENT中的n值为百分数,这时n取值范围为0到100,所返回的行数等于CEILING(ROWS*n%),其中rows为符合检索条件的数据行数。
例如:下面语句分别要求返回结果集合中的前2行和前CEILING(3*20/100)行:
select top 2 * from table
select top 20 PERCENT * FROM table
如果表里有递增的关键字(或递减),就可以用TOP加上关键字条件。
如第一次查找时可以用
IndexStr = 0
Select top 50 from TableName where IndexName >= IndexStr
然后记录下最后一条记录的关键字,下次再用。