Parallel.Foreach SQL查询有时会导致Connection

我需要加快在我的应用程序中执行12个查询。 我从常规foreach切换到Parallel.ForEach。 但有时我会收到错误消息“ExecuteReader需要一个开放且可用的连接。连接的当前状态正在连接。” 我的理解是,由于12个查询中的许多查询都使用相同的InitialCatalog,因此12中没有真正的新连接,这可能是问题所在? 我怎样才能解决这个问题? “sql”是类型“Sql”的列表 – 类只是字符串名称,字符串连接和查询列表。 这是代码:

///  /// Connects to SQL, performs all queries and stores results in a list of DataTables ///  /// List of data tables for each query in the config file public List GetAllData() { Stopwatch sw = new Stopwatch(); sw.Start(); List data = new List(); List sql=new List(); Sql one = new Sql(); one.connection = "Data Source=XXX-SQL1;Initial Catalog=XXXDB;Integrated Security=True"; one.name = "Col1"; one.queries.Add("SELECT Name FROM [Reports]"); one.queries.Add("SELECT Other FROM [Reports2]"); sql.Add(one); Sql two = new Sql(); two.connection = "Data Source=XXX-SQL1;Initial Catalog=XXXDB;Integrated Security=True"; two.name = "Col2"; two.queries.Add("SELECT AlternateName FROM [Reports1]"); sql.Add(two); Sql three = new Sql(); three.connection = "Data Source=YYY-SQL2;Initial Catalog=YYYDB;Integrated Security=True"; three.name = "Col3"; three.queries.Add("SELECT Frequency FROM Times"); sql.Add(three); try { // ParallelOptions options = new ParallelOptions(); //options.MaxDegreeOfParallelism = 3; // Parallel.ForEach(sql, options, s => Parallel.ForEach(sql, s => //foreach (Sql s in sql) { foreach (string q in s.queries) { using (connection = new SqlConnection(s.connection)) { connection.Open(); DataTable dt = new DataTable(); dt.TableName = s.name; command = new SqlCommand(q, connection); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; adapter.Fill(dt); //adapter.Dispose(); lock (data) { data.Add(dt); } } } } ); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "GetAllData error"); } sw.Stop(); MessageBox.Show(sw.Elapsed.ToString()); return data; } 

这是我制作的你需要的Sql类:

 ///  /// Class defines a SQL connection and its respective queries ///  public class Sql { ///  /// Name of the connection/query ///  public string name { get; set; } ///  /// SQL Connection string ///  public string connection { get; set; } ///  /// List of SQL queries for a connection ///  public List queries = new List(); } 

我会重构你的业务逻辑(连接到数据库)。

 public class SqlOperation { public SqlOperation() { Queries = new List(); } public string TableName { get; set; } public string ConnectionString { get; set; } public List Queries { get; set; } } public static List GetAllData(IEnumerable sql) { var taskArray = sql.SelectMany(s => s.Queries .Select(query => Task.Run(() => //Task.Factory.StartNew for .NET 4.0 ExecuteQuery(s.ConnectionString, s.TableName, query)))) .ToArray(); try { Task.WaitAll(taskArray); } catch(AggregateException e) { MessageBox.Show(e.ToString(), "GetAllData error"); } return taskArray.Where(t => !t.IsFaulted).Select(t => t.Result).ToList(); } public static DataTable ExecuteQuery(string connectionString, string tableName, string query) { DataTable dataTable = null; using (var connection = new SqlConnection(connectionString)) { dataTable = new DataTable(); dataTable.TableName = tableName; using(var command = new SqlCommand(query, connection)) { connection.Open(); using(var adapter = new SqlDataAdapter()) { adapter.SelectCommand = command; adapter.Fill(dataTable); } } } return dataTable; } 

Ado.Net有一个非常聪明的连接池,所以一般来说你应该只打开连接并关闭每个命令的连接,让池处理它们是真的被打开还是关闭。

所以每个命令一个连接:

  Parallel.ForEach(sql, s=> //foreach (Sql s in sql) { foreach (string q in s.queries) { using (connection = new SqlConnection(s.connection)) { connection.Open(); DataTable dt = new DataTable(); dt.TableName = s.name; command = new SqlCommand(q, connection); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; adapter.Fill(dt); //adapter.Dispose(); lock(data){ data.Add(dt); } } } } 

您也可以使用MultipleActiveResultSets = true; 在连接字符串中,以支持多个读者