寻找一个可以同时访问多个数据库的良好实现

发布于 2024-08-30 12:38:38 字数 225 浏览 5 评论 0原文

我只是想知道是否有一个很好的实现(在 C#/ASP.NET 中)如何同时从不同的数据库访问我的记录。我有这个简单的应用程序,它应该在这些数据库上搜索,然后,如果某个关键字匹配则显示记录。

我总共有大约 5 个数据库(当前使用 MySQL),并将在未来扩展它(可能会更改为 SQL Server)。我完全了解如何在单个数据库上进行搜索,同时我仍在学习如何在多个数据库中进行搜索。

有什么提示、意见或建议吗?

I just want to find out if there's a good implementation (in C#/ASP.NET) on how to access my records from my different databases simultaneously. I have this simple application which suppose to search on these databases, then, display the record if a certain keyword is matched.

I have like 5 databases (currently using MySQL) all in all and will be expanding it on the future (might be changing to SQL Server). I am fully aware on how to search on a single database, while I'm still learning how do it in multiple databases.

Any tips, comments or suggestions?

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

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

发布评论

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

评论(4

蓝眸 2024-09-06 12:38:38

对于 MySQL,我不能说,但在 SQL Server 中,您可以从多个数据库中进行选择(只要您连接的用户具有正确的权限)。如果所有数据库中的表都相似,那么您也许可以通过将结果联合在一起来在一个查询中完成此操作。因此,如果您有 3 个数据库(名为 DB1DB2DB3),并且每个数据库都有一个名为 Records 的表那么你可以这样做:

SELECT * FROM DB1.dbo.Records WHERE Name Like '%searchterm%'
UNION
SELECT * FROM DB2.dbo.Records WHERE Name Like '%searchterm%'
UNION
SELECT * FROM DB3.dbo.Records WHERE Name Like '%searchterm%'

I can't say for MySQL, but in SQL server you can select from multiple databases (so long as the user you are connecting to has the correct permissions). If the tables are similar across all databases then you could perhaps do this in one query by UNIONING the results together. So, if you had 3 databases (called DB1, DB2 and DB3) and each had a table called Records then you could do this:

SELECT * FROM DB1.dbo.Records WHERE Name Like '%searchterm%'
UNION
SELECT * FROM DB2.dbo.Records WHERE Name Like '%searchterm%'
UNION
SELECT * FROM DB3.dbo.Records WHERE Name Like '%searchterm%'
温柔女人霸气范 2024-09-06 12:38:38

拥有 XXXConnection 的多个实例(MysqlConnection 或其他)并迭代它们。

Have multiple instances of XXXConnection (MysqlConnection or whatever) and iterate through them.

谁人与我共长歌 2024-09-06 12:38:38

您将使用与使用不同的连接字符串针对每个其他数据库搜索单个数据库相同的方法。这对于 .NET 4.0 或不久前发布的 3.5 版本库中的并行扩展来说是一个很好的候选者。如果结果集有些相同,我还会研究 DataTable.Merge 方法。

You would use the same methodology used to search a single database against each of the other databases using a different connection string. This would be a good candidate for the parallel extensions in .NET 4.0 or the 3.5 version of the library that was put out some time ago. If the resultsets are going to be somewhat identical, I would also investigate the use of the DataTable.Merge method.

○闲身 2024-09-06 12:38:38

根据您是在应用程序的服务器端还是客户端执行查询,您可能会在服务器端采用以下方式:

[Server].[Database].[Schema].[Table]

在 SQL Server 中,表名称可能以服务器名称、数据库名称和架构名称为前缀。如果您不想使用架构名称,但需要指定位于同一服务器上的数据库,则可以编写以下内容:

[Database]..[Table]

如果您更愿意从客户端进行搜索,则可以 编写希望使用企业库来轻松配置命名连接字符串和它提供的抽象。

如果您愿意,您可以简单地使用 BackgroundWorker性能增益。再说一遍,这是在客户端。

Depending on whether you perform your queries on the server or client side of the application, you might go the following way on the server side:

[Server].[Database].[Schema].[Table]

In SQL Server, the table name may be prefixed by the server name, the database name and the schema name. If you'd rather not use the schema name, but you need to specify the database which is on the same server, you could write the following:

[Database]..[Table]

If you're more preferably want to do your search from the client-side, you might wish to use Enterprise Library for the ease of configurable named connection strings and the abstraction it offers.

You could simply use a BackgroundWorker if you wish to gain in performance. Than again, that is on the client-side.

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