是否可以通过数据集中的相关数据表对数据表中的数据进行排序?

发布于 2024-12-09 01:16:41 字数 981 浏览 0 评论 0原文

我已经使用 ORM 这么久了,我似乎已经忘记了 dotnet 中的大部分基本数据处理技能:(

是否可以做这样的事情?

        DataSet ds = new DataSet();
        var compiledConnection = new SqlConnection(cDbConnectionString);
        SqlDataAdapter daChart = new SqlDataAdapter("select * from Chart", compiledConnection);
        daChart.Fill(ds, "chart");

        if (ds.Tables["chart"].Rows.Count > 0)
        {
            var sourceConnection = new SqlConnection(sourceDbConnectionString);
            SqlDataAdapter daSource = new SqlDataAdapter("select * from source", sourceConnection);
            daSource.Fill(ds, "source");

            DataRelation chart_source = new DataRelation("dr", ds.Tables["chart"].Columns["intItemId"],
                   ds.Tables["source"].Columns["intRowId"], false);
            ds.Relations.Add(chart_source);
        }

然后使用表“图表”中的一列进行排序表“源”中的数据跨数据关系?

(在有人问之前,这两个表位于不同站点上的 SqlServer 的单独实例中,因此将数据作为一个表拉取并不是一项简单的任务。因此这种方法)

干杯, 马特

I've been using ORM's for so long, I appear to have forgotten most of my basic data handling skills in dotnet :(

Is it possibly to do something like this?

        DataSet ds = new DataSet();
        var compiledConnection = new SqlConnection(cDbConnectionString);
        SqlDataAdapter daChart = new SqlDataAdapter("select * from Chart", compiledConnection);
        daChart.Fill(ds, "chart");

        if (ds.Tables["chart"].Rows.Count > 0)
        {
            var sourceConnection = new SqlConnection(sourceDbConnectionString);
            SqlDataAdapter daSource = new SqlDataAdapter("select * from source", sourceConnection);
            daSource.Fill(ds, "source");

            DataRelation chart_source = new DataRelation("dr", ds.Tables["chart"].Columns["intItemId"],
                   ds.Tables["source"].Columns["intRowId"], false);
            ds.Relations.Add(chart_source);
        }

And then use one of the columns in the table "chart" to order the data in the table "source" across the datarelation?

(Before anyone asks, these two tables are in separare instances of SqlServer on separate sites, so just pulling the data as one table is not a straightforward task. Hence this approach)

Cheers,
Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清旖 2024-12-16 01:16:41

这只是创建了一个外键的等价物。您似乎想要相当于 INNER JOIN 的东西。

除了创建关系之外,还需要将一个的所有列添加到另一个中,循环填充行和 GetParentRows。 MS 有一些很好的起点代码:

http://support.microsoft.com/kb/326080

编辑。您还可以通过创建链接服务器并使用 4 个部分名称 [服务器].[数据库].[所有者].[表] 来创建 SQL 版本

That just creates the equivalent of a foreign key. You seem to want the equivalent of an INNER JOIN.

In addition to creating the relationship, it requires adding all the columns of one to the other, loops to fill in the rows and GetParentRows. MS has some good starting point code:

http://support.microsoft.com/kb/326080

EDIT. You could also do a SQL version, by creating a linked server and using 4 part names [server].[database].[owner].[table]

八巷 2024-12-16 01:16:41

感谢您的建议,但我发现您可以使用 LINQ 更轻松地做到这一点:

            DataTable source = ds.Tables["source"];
            DataTable chart = ds.Tables["chart"];

            var joinedTable =
                from s in source.AsEnumerable()
                join c in chart.AsEnumerable()
                on s.Field<Int64>("intRowId") equals
                    c.Field<Int64>("intItemId")
                select new
                {
                    intRowId = s.Field<Int64>("intRowID"),
                    strTitle = s.Field<string>("strTitle"),
                    intWeight = c.Field<Int64>("intWeight")
                };

            var sortedTable = from j in joinedTable
                              orderby j.intWeight descending
                              select j;

Thanks for the suggestion, but I discovered you can do it with LINQ rather more easily:

            DataTable source = ds.Tables["source"];
            DataTable chart = ds.Tables["chart"];

            var joinedTable =
                from s in source.AsEnumerable()
                join c in chart.AsEnumerable()
                on s.Field<Int64>("intRowId") equals
                    c.Field<Int64>("intItemId")
                select new
                {
                    intRowId = s.Field<Int64>("intRowID"),
                    strTitle = s.Field<string>("strTitle"),
                    intWeight = c.Field<Int64>("intWeight")
                };

            var sortedTable = from j in joinedTable
                              orderby j.intWeight descending
                              select j;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文