比较datagrid 1.1框架中的2个数据集 [英] comparing 2 datasets in datagrid 1.1 framework

查看:105
本文介绍了比较datagrid 1.1框架中的2个数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数据集,我想比较两列,如果列数据不同

我想更新其他数据集的差异,请指导我

I have 2 datasets which i want to compare both columns and if the column data is different

i want to update the difference in other dataset, please guide me

推荐答案

我不确定你要做什么,但似乎你要找的是DataSet.Merge。
.NET Framework类库 - DataSet.Merge方法
http://msdn.microsoft.com /en-us/library/system.data.dataset.merge(VS.71).aspx

我写了一个简单的例子。
第一个数据集包含伦敦客户的CustomerID和City列。
第二个数据集包含马德里客户的所有CustomerID,City和Address列。
合并后,第一个数据集包含伦敦客户的CustomerID,City和Address列和马德里。

I wrote a quick example.
The first dataset set contains the CustomerID and City columns for customers in London.
The second dataset contains all CustomerID, City and Address columns for customers in Madrid.
After the Merge, the first dataset contains CustomerID, City and Address columns for customers in London and Madrid.

(如果你想看到它,你需要对Northwind数据库运行它个体经营)点击--------------------------------------------- -----------------------------------

(you need to run this against the Northwind database if you want to see it yourself)
--------------------------------------------------------------------------------

class Program
{
static void Main(string [] args)
{
string cs = @" Data Source =< your server> ;; Integrated Security = SSPI; Initial Catalog = Northwind" ;;
using(SqlConnection con = new SqlConnection(cs))
{
//创建两个数据集
DataSet dsOne = new DataSet(" DsOne");
DataSet dsTwo = new DataSet(" DsTwo") ;);
//用于填充第一个数据集的命令和适配器

SqlCommand cmd = new SqlCommand(" SELECT CustomerID,City FROM Customers WHERE City ='London',con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//填充第一个数据集
sda.Fill(dsOne);
PrintDataSet(dsOne);
//填充第二个数据集的命令和适配器
cmd = new SqlCommand(" SELECT CustomerID,City ,FROM FROM Customers WHERE City ='Madrid'",con);
sda = new SqlDataAdapter(cmd);
//填充第二个数据集
sda.Fill(dsTwo);
PrintDataSet( dsTwo);
//将第二个数据集合并到第一个数据集中。
//第二个数据集中的"地址"列现在也位于第一个数据集中。
dsOne.Merge(dsTwo); < br> //打印合并数据集(dsOne)
PrintDataSet(dsOne);
}

    class Program
    {
        static void Main(string[] args)
        {
            string cs = @"Data Source=<your server>;Integrated Security=SSPI;Initial Catalog=Northwind";
            using (SqlConnection con = new SqlConnection(cs))
            {
                // Create two datasets
                DataSet dsOne = new DataSet("DsOne");
                DataSet dsTwo = new DataSet("DsTwo");
                // Command and adapter to fill the first dataset
                SqlCommand cmd = new SqlCommand("SELECT CustomerID, City FROM Customers WHERE City = 'London'", con);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                // Fill first dataset
                sda.Fill(dsOne);
                PrintDataSet(dsOne);
                // Command and adapter to fill the second dataset
                cmd = new SqlCommand("SELECT CustomerID, City, Address FROM Customers WHERE City = 'Madrid'", con);
                sda = new SqlDataAdapter(cmd);
                // Fill second dataset
                sda.Fill(dsTwo);
                PrintDataSet(dsTwo);
                // Merge the second dataset into the first dataset.
                // The 'Address' column in the second dataset is now also in the first dataset.
                dsOne.Merge(dsTwo);
                // Print the merged dataset (dsOne)
                PrintDataSet(dsOne);
            }
        }

private static void PrintDataSet(DataSet dsIn)
{
Console.WriteLine(" Rows in dataset {0}:",dsIn.DataSetName );
StringBuilder sb = new StringBuilder();
foreach(dsIn.Tables [0] .Rows中的DataRow dr)
{
foreach(dsIn.Tables [0] .Columns中的DataColumn dc) )



sb.Append(dr [dc] +" \t");
}

sb.AppendLine();
}
Console.WriteLine(某人) .ToString());
}



--------------------------------- -----------------------------------------------结果输出:

        private static void PrintDataSet(DataSet dsIn)
        {
            Console.WriteLine("Rows in dataset {0}:", dsIn.DataSetName);
            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dsIn.Tables[0].Rows)
            {
                foreach (DataColumn dc in dsIn.Tables[0].Columns)
                {
                    sb.Append(dr[dc] + "\t");
                }
                sb.AppendLine();
            }
            Console.WriteLine(sb.ToString());
        }
    }
--------------------------------------------------------------------------------
Output:

数据集DsOne中的行:
AROUT伦敦
BSBEV伦敦
CONSH伦敦
EASTC L ondon
NORTS伦敦
SEVES伦敦

Rows in dataset DsOne:
AROUT   London
BSBEV   London
CONSH   London
EASTC   London
NORTS   London
SEVES   London

数据集DsTwo中的行:
BOLID Madrid C / Araquil,67
FISSA Madrid C / Moralzarzal,86
ROMEY MadridGranVía,1

Rows in dataset DsTwo:
BOLID   Madrid  C/ Araquil, 67
FISSA   Madrid  C/ Moralzarzal, 86
ROMEY   Madrid  Gran Vía, 1

数据集DsOne中的行:
AROUT伦敦
BSBEV伦敦
CONSH伦敦
EASTC伦敦
NORTS伦敦
SEVES伦敦
BOLID Madrid C / Araquil,67
FISSA Madrid C / Moralzarzal,86
ROMEY MadridGranVía,1

Rows in dataset DsOne:
AROUT   London
BSBEV   London
CONSH   London
EASTC   London
NORTS   London
SEVES   London
BOLID   Madrid  C/ Araquil, 67
FISSA   Madrid  C/ Moralzarzal, 86
ROMEY   Madrid  Gran Vía, 1

希望这会有所帮助。

------------------------------------------- -------------------------------------
此帖子是"按现状"提供的。没有保证,也没有权利。

--------------------------------------------------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.


这篇关于比较datagrid 1.1框架中的2个数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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