使用 SMO 和任务并行库在同一服务器上的数据库上同时执行脚本?

发布于 2024-10-10 12:51:20 字数 543 浏览 0 评论 0原文

我正在尝试使用 SQL Server 管理对象在同一服务器中的所有数据库上同时执行更新脚本。我目前的实验实现是这样的:

var server = new Server(new ServerConnection(new SqlConnection(connString)));
server.ConnectionContext.InfoMessage += new SqlInfoMessageEventHandler(DBOutputReceived);
            
var databases = server.Databases.Cast<Database>().ToList();          
Parallel.ForEach(databases, db => { db.ExecuteNonQuery(script); });
    

我什至不确定使用 SMO 是否可行,但这基本上就是我想要做的。我可以看到 ConnectionContext.InfoMessage 事件可能会出现问题,但我不确定如何解决。我应该为每个并行任务创建一个单独的服务器实例吗?

I am trying to concurrently execute update scripts on all Databases in the same Server using SQL Server Management Objects. My current experiment implementation is like this:

var server = new Server(new ServerConnection(new SqlConnection(connString)));
server.ConnectionContext.InfoMessage += new SqlInfoMessageEventHandler(DBOutputReceived);
            
var databases = server.Databases.Cast<Database>().ToList();          
Parallel.ForEach(databases, db => { db.ExecuteNonQuery(script); });
    

I'm not even sure this is possible using SMO, but this is basically what I want to do. I can see that the ConnectionContext.InfoMessage event will likely have issues, but I'm not sure how to resolve. Should I instead create a separate Server instance for each parallel task?

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

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

发布评论

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

评论(1

假情假意假温柔 2024-10-17 12:51:20

解决方案是让每个线程创建自己的 Server/ServerConnection 对象,这样线程之间就没有共享内存。

The solution is to have each thread create it's own Server/ServerConnection object, that way there is no shared memory between the threads.

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