LINQ to SQL:从不同服务器上的两个数据库获取记录
我需要从两个不同的表中获取记录。数据库位于两个不同的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 DB1 中创建引用 DB2 上的表 2 的视图。
You can create a View in DB1 that references table2 on DB2.
对此(至少)有两种可能的解决方案。
您可以定义一个存储过程来执行您已经熟悉的跨数据库查询。将存储过程添加为数据上下文中的方法。
或者,您可以为每个数据库定义一个数据上下文,然后在 Linq 中选择组合。像这样的东西:
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: