/// <summary> /// Executes a SQL Script and retrieves all data obteined /// </summary> /// <remarks> /// You must explicity close the returned DataReader once it's not used, /// otherwise the assigned connection would stay opened and cause /// a bad use of resources /// </remarks> /// <param name="command"> /// Sql Sentence to exectute for retrieve data /// </param> /// <returns> /// A System.Data.DbDataReader with all data obtained /// </returns> public override IDataReader GetDataReader(Command command) { //Local Vars MySqlPCL.MySqlDataReader reader = null; CommandEventArgs e = new CommandEventArgs(command); lock (Locker) { try { //raising events OnBeforeGetDataReader(e); //running the script only if e.Cancel is false if (!e.Cancel) { //Initializing command and connection MySqlPCL.MySqlCommand dbCommand = new MySqlPCL.MySqlCommand(); dbCommand.Connection = Connection; dbCommand.CommandText = command.Script; dbCommand.Transaction = Transaction; foreach (CommandParameter param in command.Parameters) { dbCommand.Parameters.Add(Parse(param)); } OpenConnection(); //Validating if there is a current transaction if (Transaction == null) { //Loading the data reader indicating that the close of the reader //must close the connection too reader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection); } else { //Loading the data reader reader = dbCommand.ExecuteReader(); } } //raising events e.Result = reader; OnAfterGetDataReader(e); } catch (System.Exception ex) { //Closing the reader if apply CloseConnection(); //Re - throw the excepción to the caller throw new SqlException(command, "Error on creating DataReader (" + ex.Message + ")", ex); } } //Returning the DataReader return new DataReader(this, (MySqlPCL.MySqlDataReader) e.Result); }
public DataReader(DataBase dataBase, MySqlPCL.MySqlDataReader nativeReader) { if (dataBase == null) { throw new ArgumentNullException("dataBase"); } if (nativeReader == null) { throw new ArgumentNullException("nativeReader"); } DataBase = dataBase; NativeReader = nativeReader; }