In a previous post (LINQ to the rescue!) I used linq to merge multiple DataTables, sort them and exclude duplicates.
But, what about filling those DataTables in the first place. If, like I, you have multiple databases in production at work and need to show to the users a “database agnostic” view of their data, you need to query all those databases and merge the data.
Querying a large number of databases in serial is a slow process, but playing with threads seems … dangerous.
Well, with the new Parallel libraries, this becomes very simple to do, even with Oracle ( ;-) )
- var dbs = new List<string>() { "Database1", "Database2", "Database3", "Database4" };
- var dts = new DataTable[dbs.Count];
- Parallel.For(0, dbs.Count, i => LoadData(dbs[i], dts, i));
- var myTable = dts[0];
- for (int i = 1; i < dbs.Count; i++) myTable.Merge(dts[i]);
- var dtLinqData = (from MyRow in myTable.AsEnumerable()
- orderby MyRow.Field<string>("Name") ascending
- select MyRow).Distinct().CopyToDataTable();
- static void LoadData(string db, DataTable[] dts, int index)
- {
- DataTable returnValue = null;
- using (var connection = new OracleConnection(GenerateConnectionString(db, "MyUserId", "MyPassword")))
- {
- using (var dataAdapter = new OracleDataAdapter("SELECT * FROM CUSTOMER", connection))
- {
- returnValue = new DataTable();
- dataAdapter.Fill(returnValue);
- }
- }
- dts[index] = returnValue;
- }
- private static string GenerateConnectionString(string instance, string userId, string password)
- {
- return string.Format("data source = {0}; user id = {1}; password={2}; pooling=true; Connection Lifetime=60; Max Pool Size=50", instance, userId, password);
- }