LINQPad,使用多个数据上下文
我经常比较不同数据库中表中的数据。这些数据库不具有相同的架构。在 TSQL 中,我可以使用 DB>user>table
结构(DB1.dbo.Stores
、DB2.dbo.OtherPlaces
)来引用它们拉取数据进行比较。我非常喜欢 LINQPad 的想法,但我似乎无法轻松地从同一组语句中的两个不同数据上下文中提取数据。
我看到人们建议简单地更改连接字符串以将数据从其他源提取到当前模式中,但正如我所提到的,这是行不通的。我是否只是跳过了常见问题解答中的一页?这似乎是一个相当常规的程序,对我来说无法实现。
在“简单”的世界中,我希望能够简单地引用 LINQPad 创建的类型化数据上下文。然后我可以简单地:
DB1DataContext db1 = new DB1DataContext();
DB2DataContext db2 = new DB2DataContext();
然后从那里开始工作。
I am often comparing data in tables in different databases. These databases do not have the same schema. In TSQL, I can reference them with the DB>user>table
structure (DB1.dbo.Stores
, DB2.dbo.OtherPlaces
) to pull the data for comparison. I like the idea of LINQPad quite a bit, but I just can't seem to easily pull data from two different data contexts within the same set of statements.
I've seen people suggest simply changing the connection string to pull the data from the other source into the current schema but, as I mentioned, this will not do. Did I just skip a page in the FAQ? This seems a fairly routine procedure to be unavailable to me.
In the "easy" world, I'd love to be able to simply reference the typed datacontext that LINQPad creates. Then I could simply:
DB1DataContext db1 = new DB1DataContext();
DB2DataContext db2 = new DB2DataContext();
And work from there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
更新:现在可以在 LINQPad 中执行跨数据库 SQL Server 查询(从 LINQPad v4.31 开始,具有 LINQPad Premium 许可证)。要使用此功能,请按住 Control 键,同时将数据库从架构资源管理器拖动到查询窗口。
还可以查询链接服务器(您通过调用sp_add_linkedserver链接的服务器)。为此:
Update: it's now possible to do cross-database SQL Server queries in LINQPad (from LINQPad v4.31, with a LINQPad Premium license). To use this feature, hold down the Control key while dragging databases from the Schema Explorer to the query window.
It's also possible to query linked servers (that you've linked by calling sp_add_linkedserver). To do this:
请记住,您始终可以自己创建另一个上下文。
Keep in mind that you can always create another context on your own.
您可以实例化任意多个上下文来分散 SQL 实例并执行伪跨数据库联接、复制数据等。请注意,跨上下文的联接是在本地执行的,因此您必须调用 ToList()、ToArray() 等来执行查询在加入之前单独使用各自的数据源。换句话说,如果您“内部”连接 DB1.TABLE1 中的 10 行和 DB2.TABLE2 中的 20 行,则在 Linq 执行连接并返回相关/相交之前,必须将这两个集合(所有 30 行)拉入本地计算机上的内存中。设置(每个示例最多 20 行)。
You can instantiate as many contexts as you like to disparate SQL instances and execute pseudo cross database joins, copy data, etc. Note, joins across contexts are performed locally so you must call ToList(), ToArray(), etc to execute the queries using their respective data sources individually before joining. In other words if you "inner" join 10 rows from DB1.TABLE1 with 20 rows from DB2.TABLE2, both sets (all 30 rows) must be pulled into memory on your local machine before Linq performs the join and returns the related/intersecting set (20 rows max per example).
我认为你无法做到这一点。请参阅此 LinqPad 请求。< /a>
但是,您可以在单独的 dll 中构建多个 dbml 文件并在 LinqPad 中引用它们。
I do not think you are able to do this. See this LinqPad request.
However, you could build multiple dbml files in a separate dll and reference them in LinqPad.
拖放方法:按住 Ctrl 键拖动其他数据库
从架构资源管理器到查询编辑器。
使用案例:
Drag-and-drop approach: hold down the Ctrl key while dragging additional databases
from the Schema Explorer to the query editor.
Use case:
据我所知,多个数据库仅在 LinqPad 的“付费”版本中可用(我编写的内容适用于 LinqPad 6 Premium)。
有关更多详细信息,请参阅StackOverflow 中的此答案(“多数据库支持”部分)。
Multiple databases are as far as I know only available in the "paid" version of LinqPad (what I wrote applies to LinqPad 6 Premium).
For more details, see this answer in StackOverflow (section "Multiple database support").