using Microsoft.Plugin.Indexer.Interface; using System; using System.Collections.Generic; using System.Data.OleDb; namespace Microsoft.Plugin.Indexer.SearchHelper { public class OleDBSearch : ISearch { private OleDbCommand command; private OleDbConnection conn; private OleDbDataReader WDSResults; public List Query(string connectionString, string sqlQuery) { List result = new List(); using (conn = new OleDbConnection(connectionString)) { // open the connection conn.Open(); // now create an OleDB command object with the query we built above and the connection we just opened. using (command = new OleDbCommand(sqlQuery, conn)) { using (WDSResults = command.ExecuteReader()) { if (WDSResults.HasRows) { while (WDSResults.Read()) { List fieldData = new List(); for (int i = 0; i < WDSResults.FieldCount; i++) { fieldData.Add(WDSResults.GetValue(i)); } result.Add(new OleDBResult(fieldData)); } } } } } return result; } /// Checks if all the variables related to database connection have been properly disposed public bool HaveAllDisposableItemsBeenDisposed() { bool commandDisposed = false; bool connDisposed = false; bool resultDisposed = false; try { command.ExecuteReader(); } catch (InvalidOperationException) { commandDisposed = true; } try { WDSResults.Read(); } catch (InvalidOperationException) { resultDisposed = true; } if(conn.State == System.Data.ConnectionState.Closed) { connDisposed = true; } return commandDisposed && resultDisposed && connDisposed; } } }