我需要创建一个程序来并行处理包含在表格模型中的表的列表。
下面是我使用的代码:
Server[] svrList = new Server[tables.getTables.Count];
Parallel.For(0, tables.getTables.Count, i =>
{
svrList[i] = ServerConnect(connectionString);
Model m = svrList[i].Databases[database].Model;
log.Info("process table " + tables.getTables.ElementAt(i).Name);
Table t = m.Tables[tables.getTables.ElementAt(i).Name];
t.RequestRefresh(Microsoft.AnalysisServices.Tabular.RefreshType.Full);
m.SaveChanges();
log.Info("Finish " + tables.getTables.ElementAt(i).Name);
svrList[i].Disconnect();
}
);如果一个表失败,其他表必须正确加载。
在这段代码中,表被正确处理,但它们是按顺序处理的。
我对每个表使用不同的服务器连接,因为如果我使用相同的连接,我会出现以下错误:
the connection cannot be used while an xmlreader object is open如何解决此问题?
谢谢
发布于 2019-06-02 02:25:27
只需将Parallel.For更改为常规for循环,并将SaveChanges移出该循环即可。SaveChanges将执行您已排队的所有命令。默认情况下,它在事务内并行执行此操作。
var conn = ServerConnect(connectionString);
Model m = conn.Databases[database].Model;
for (int i=0; i<tables.getTables.Count; i++)
{
log.Info("process table " + tables.getTables.ElementAt(i).Name);
Table t = m.Tables[tables.getTables.ElementAt(i).Name];
t.RequestRefresh(Microsoft.AnalysisServices.Tabular.RefreshType.Full);
}
m.SaveChanges();
conn.Disconnect();https://stackoverflow.com/questions/56397152
复制相似问题