LINQ to SQL:从不同服务器上的两个数据库获取记录

发布于 2024-08-29 20:26:49 字数 282 浏览 2 评论 0原文

我需要从两个不同的表中获取记录。数据库位于两个不同的 SQL Server 中。

例如。销售数据库位于服务器 1 上,采购数据库位于服务器 2 上。销售和采购数据库都有一些表集,例如销售数据库中的 table1 和采购数据库中的 table2。 现在,我需要通过连接 table1 和 table2 来获取具有一些公共记录的记录。

使用 T-SQL,我可以通过链接服务器然后查询它们来做到这一点。

请建议,我如何使用 LINQ to SQL 来做到这一点,因为我不知道这一点。

谢谢。

I need to fetch records from tables, that are in two diff. databases in two different SQL Servers.

For Instance. Sales DB on server1 and Purchase DB on server2. Both Sales and Purchase DB's have some set of tables say table1 in Sales DB and table2 in Purchase DB.
Now, I need to get records from table1 and table2 that are having some common records by joining them.

Using T-SQL i can do it by linking the servers and then quering them.

Please suggest, how can i do it using LINQ to SQL as am'nt aware of it.

Thanks.

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

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

发布评论

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

评论(2

人间☆小暴躁 2024-09-05 20:26:50

您可以在 DB1 中创建引用 DB2 上的表 2 的视图。

You can create a View in DB1 that references table2 on DB2.

弱骨蛰伏 2024-09-05 20:26:49

对此(至少)有两种可能的解决方案。

您可以定义一个存储过程来执行您已经熟悉的跨数据库查询。将存储过程添加为数据上下文中的方法。

或者,您可以为每个数据库定义一个数据上下文,然后在 Linq 中选择组合。像这样的东西:

var table1records = from rec1 in context1.Table1
                    select rec1;

var table2records = from rec2 in context2.Table2
                    select rec2;

var combined = from rec1 in table1records
               join rec2 in table2records on rec1.idColumn equals rec2.idColumn
               select new
               {
                 Rec1 = rec1,
                 Rec2 = rec2
               };

There are (at least) two possible solutions for this.

You could define a stored procedure that does the cross-database query with which you are already familiar. Add the stored procedure as a method on your data context.

Or, you could define a data context for each database, then select the combination in Linq. Something like:

var table1records = from rec1 in context1.Table1
                    select rec1;

var table2records = from rec2 in context2.Table2
                    select rec2;

var combined = from rec1 in table1records
               join rec2 in table2records on rec1.idColumn equals rec2.idColumn
               select new
               {
                 Rec1 = rec1,
                 Rec2 = rec2
               };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文