使用 SMO 和任务并行库在同一服务器上的数据库上同时执行脚本?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是让每个线程创建自己的 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.