简体   繁体   English

如何在Gridview上合并/加入两个不同的数据源?

[英]How can I merge/join two different datasources on Gridview?

I have two databases and tables. 我有两个数据库和表。 I need to join two tables on one datasource for Gridview. 我需要在Gridview的一个数据源上连接两个表。 (I can't use DBLINK) (我不能使用DBLINK)

try
{
OleDbConnection Connection1;
    using (Connection1 = new OleDbConnection("Provider=MSDAORA.1;Data Source=DATABASE1:1521/orcl;Persist Security Info=True;Password=PASSWORD;User ID=USERNAME;"))
    {
        string sqlQuery = "select * from DB1_TABLE";

        using (OleDbDataAdapter cmd = new OleDbDataAdapter(sqlQuery, Connection1))
        {
                Connection1.Open();
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                Connection1.Close();
        }
    }
}
catch (Exception)
{
}


try
{
OleDbConnection Connection2;
    using (Connection2 = new OleDbConnection("Provider=MSDAORA.1;Data Source=DATABASE2:1521/orcl;Persist Security Info=True;Password=PASSWORD;User ID=USERNAME;"))
    {
        string sqlQuery = "select * from DB2_TABLE";

        using (OleDbDataAdapter cmd = new OleDbDataAdapter(sqlQuery, Connection2))
        {
                Connection2.Open();
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                Connection2.Close();
        }
    }
}
catch (Exception)
{
}

Like this: 像这样:

在此输入图像描述

And sometimes db1 or db2 getting error. 有时db1或db2会出错。 I need to see other db values if getting one of them error like this: 如果得到其中一个错误,我需要查看其他数据库值,如下所示:

在此输入图像描述

or 要么

在此输入图像描述

How can I do this? 我怎样才能做到这一点? Could you show me some example on my sample code? 你能告诉我一些关于我的示例代码的例子吗? Thank you. 谢谢。

- -

I tried dataset.merge like this: 我像这样尝试了dataset.merge:

    dt1.Merge(dt2, true, MissingSchemaAction.Add);
    GridView1.DataSource = dt1;
    GridView1.DataBind();

But getting output like this: 但得到这样的输出:

在此输入图像描述

How can I fix that? 我该如何解决这个问题?

Something along these lines: 这些方面的东西:

        var data1 = new List<Data1>
        {
            new Data1 { Name = "A", Date1 = DateTime.Now },
            new Data1 { Name = "B", Date1 = DateTime.Now }
        };

        var data2 = new List<Data2>
        {
            new Data2 { Name = "C", Date3 = DateTime.Now },
            new Data2 { Name = "D", Date3 = DateTime.Now },
        };


        var query = from key in data1.Select(item => item.Name)
                        .Union(data2.Select(item => item.Name))

        join datum1 in data1 on key equals datum1.Name into kd1
        from sub1 in kd1.DefaultIfEmpty()

        join datum2 in data2 on key equals datum2.Name into kd2
        from sub2 in kd2.DefaultIfEmpty()

        select new
        {
            Name = key,
            Date1 = sub1?.Date1,
            Date2 = sub1?.Date2,
            Date3 = sub2?.Date3,
            Date4 = sub2?.Date4
        };

I'm first creating a list of all the Names that you have recieved and then left joining each data source to that names list. 我首先创建了一个已收到的所有名称的列表,然后将每个数据源连接到该名称列表。

You can do it as follow : 你可以这样做:

  • retrieve the first query result which is form certain DB in dataset1 检索第一个查询结果,该结果在dataset1中形成某个DB

  • retrieve the first query result which is form certain DB in dataset2 检索在dataset2中形成某个DB的第一个查询结果

  • use DataSet.Merge Method to merge the two data set in say datasetMaster 使用DataSet.Merge方法合并say datasetMaster中的两个数据集

  • bind datasetMaster to the grid 将datasetMaster绑定到网格

Update : 更新:

Also a easiest way around this would be a SP where you have a linked database server between DB1 and DB2 and you can then reference that table from DB1 in DB2 . 另外一个最简单的方法是在SP中,您在DB1DB2之间有一个链接数据库服务器,然后您可以从DB1 in DB2引用该表。

So it will have some code like: 所以它会有一些代码:

SELECT d.ColA, d.ColB, d.ColC
FROM dbo.MyTable d
UNION ALL
SELECT b.ColA, b.ColB,b.ColC
FROM DB1.dbo.MyOtherTable b

More Info Linked Servers 更多信息链接服务器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM