如何将特定行从一个数据集存储到另一个数据集。 [英] How to store specific rows from one dataset to another.

查看:127
本文介绍了如何将特定行从一个数据集存储到另一个数据集。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataSet ds 有200行。



我创建了2个新数据集 ds1,ds2



现在单击Windows窗体应用程序中的按钮,



我需要先检索100来自数据集 ds 的行并存储在新数据集 ds1 中,

然后在数据集 ds2 中接下来的100行。



我尝试过的事情:



任何人都可以给我任何建议关于怎么做..



谢谢..

I Have a DataSet ds which has 200 rows.

I created 2 new Datasets ds1, ds2.

Now On clicking a button in Windows forms application,

I need to retrieve first 100 rows from Dataset ds and store in the new dataset ds1,
then next 100 rows in dataset ds2.

What I have tried:

Can anyone please give me any suggestion on how to do this..

Thanks..

推荐答案

试试这个



try this

DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            for (int i = 1; i <= 200; i++)
                dt.Rows.Add(i, "Name - " + i);
            ds.Tables.Add(dt);

            DataTable dt1 = new DataTable();
            DataTable dt2 = new DataTable();

            dt1 = ds.Tables[0].Clone();
            for (int i = 0; i < 100; i++)
                dt1.Rows.Add(dt.Rows[i].ItemArray);

            dt2 = ds.Tables[0].Clone();
            for (int i = 100; i < 200; i++)
                dt2.Rows.Add(dt.Rows[i].ItemArray);

            DataSet ds1 = new DataSet();
            DataSet ds2 = new DataSet();
            ds1.Tables.Add(dt1); 
            ds2.Tables.Add(dt2);


1。在你的代码中保留变量startIndex,pageSize和totalRows。

2.在Init上,设置startIndex = 0.

3.将pageSize设置为默认值,例如pageSize = 10或任何你想要/用户想要的东西。

4.将currentPage设置为1.

5.将totalRows设置为从查询结果中记录计数。

6.获取源行数并复制到目标表

请参阅示例代码(对不起,但我使用了dotNetFiddle网站为您试用)。

7.您需要做的就是指定当前页面,它将获得该范围的行。



注意:这不会是非常好的表现大集合。使用LINQ或使用参数和临时表将此处显示的分页逻辑移动到数据库端以获取Reference id列。



1. In your code keep variables startIndex, pageSize and totalRows.
2. On Init, set startIndex = 0.
3. Set pageSize to default e.g. pageSize = 10 or whatever you want/user wants.
4. Set currentPage to 1.
5. Set totalRows to record count from query result.
6. Take the number of source rows and copy to destination table
See example code (sorry for the verbosity but I used dotNetFiddle web site to try it for you).
7. All you need to do is specify the current page and it will get the rows for that range.

NOTE: this will not be very good performance on big sets. Use LINQ or move the paging logic shown here to Database side using parameters and temp table to get the Reference id columns.

using System;
using System.Data;
using System.Collections.Generic;
using System.Xml;
					
public class Program
{
	private static DataTable dt;
	private static DataTable refDt;
	private static int startIndex;
	private static int pageSize = 5;
	private static int currentPage = 3;
	private static int totalRows;
	private static int totalPages;
	
	
	public static void Main()
	{
		// Simulating a query result data bind.
		dt = new DataTable("MyDataTable");
		dt.Columns.AddRange(new DataColumn[]
							{
								new DataColumn("col1"),
								new DataColumn("col2"),
								new DataColumn("col3")
							});
		List<object[]> data = new List<object[]>
		{
			new object[] { "1.1", "1.2", "1.3" },
			new object[] { "2.1", "2.2", "2.3" },
			new object[] { "3.1", "3.2", "3.3" },
			new object[] { "4.1", "4.2", "4.3" },
			new object[] { "5.1", "5.2", "5.3" },
			new object[] { "6.1", "6.2", "6.3" },
			new object[] { "7.1", "7.2", "7.3" },
			new object[] { "8.1", "8.2", "8.3" },
			new object[] { "9.1", "9.2", "9.3" },
			new object[] { "10.1", "10.2", "10.3" },
			new object[] { "11.1", "11.2", "11.3" },
			new object[] { "12.1", "12.2", "12.3" },
		};
		
		foreach(object[] dataItems in data)
		{
			dt.Rows.Add(dataItems);
		}
		
		// Now do the paging:		
		totalRows = dt.Rows.Count;
		
		// Set up a reference table to track ids as the source DT may be sorted etc and spoil the tracker field.
		refDt = new DataTable("RefDt");
		refDt.Columns.AddRange(new DataColumn[]
							   {
								   new DataColumn("Index"),
								   new DataColumn("Id")
							   });
		for(int i=0;i<dt.Rows.Count;i++)
		{
			// Here, the refDt data table is built up to store a reference to each ID field in source datatable
			// along with a row number
			refDt.Rows.Add(new object[] {Convert.ToString(i), dt.Rows[i]["col1"]});
			//Console.WriteLine("Index:" + refDt.Rows[i].ItemArray[0] + ", Id:" + refDt.Rows[i].ItemArray[1]);
		}
		
		
		// Now lets work out how many to take and copy to destination data table
		DataTable dest = new DataTable("Destination");
		dest = dt.Clone(); // <- copys columns/schema but not data.
		
		
		startIndex = (currentPage * pageSize) - pageSize;
		Console.WriteLine("Reading " + pageSize + " rows starting at row index " + startIndex);
		
		
		for(int i = startIndex; (pageSize * currentPage) > totalRows ? i < totalRows : i < pageSize * currentPage; i++)
		{
			string lookupFilter = "Index = '" + i.ToString() + "'";
			DataRow dr = (DataRow)refDt.Select(lookupFilter)[0];	
			
			string id = Convert.ToString(dr[1]);
			string selectFilter = "col1 = '" + id + "'";
						
			DataRow source = dt.Select(selectFilter)[0];
			
			dest.Rows.Add(source.ItemArray);
			
			// Just to prove it only copies the required range
			string output= "";
			for(int c = 0; c < dest.Columns.Count; c++)
			{
				output += Convert.ToString(dest.Rows[dest.Rows.Count-1][c]) + ", ";
			}
			Console.WriteLine(output.Remove(output.Length-2,2));
		}
		
		int remainderRows = dt.Rows.Count % pageSize;
		totalPages = remainderRows > 0 ? totalRows/pageSize + 1: totalRows/pageSize;
		Console.WriteLine("Last page has " + remainderRows + " rows");
		Console.WriteLine("Page " + currentPage + " of " + totalPages  + " (Total " + dt.Rows.Count + " rows)");
	}
}


检查过去的答案:如何将数据从一个Datatable划分为两个独立的Datatable? [ ^ ]



另一种方法是使用 Linq to DataSet [ ^ ]:

Check past answer: How to divide the data from one Datatable to two separate Datatable?[^]

Another way is to use Linq to DataSet[^]:
int j =0;
for(int i==0;i+=100;i<200)
{
    //get 100 records
    var copy = ds.Tables[0].AsEnumerable().Skip(i).Take(100);
    //CopyToDataTable method is used to copy data into another datatable
    ds.Table[j] = copy.CopyToDataTable();
    j+=1;
}





有关详细信息,请查看MSDN文档:从查询创建DataTable(LINQ到DataSet) [ ^ ]



注意:

1)对于非常大量的数据,linq解决方案可能会导致性能问题

2)目标数据表结构必须与源相同



For further details, please check MSDN documentation: Creating a DataTable From a Query (LINQ to DataSet)[^]

Note:
1) for very large amount of data, linq solution may cause performance issues
2) destination datatable structure must be the same as source


这篇关于如何将特定行从一个数据集存储到另一个数据集。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆