public override void Close(MySqlDataReader reader) { base.Close(reader); if (String.IsNullOrEmpty(outSelect)) return; if ((reader.CommandBehavior & CommandBehavior.SchemaOnly) != 0) return; MySqlCommand cmd = new MySqlCommand(outSelect, command.Connection); using (MySqlDataReader rdr = cmd.ExecuteReader(reader.CommandBehavior)) { ProcessOutputParameters(rdr); } }
/// <include file='docs/mysqlcommand.xml' path='docs/ExecuteReader1/*'/> public new MySqlDataReader ExecuteReader(CommandBehavior behavior) { bool success = false; CheckState(); Driver driver = connection.driver; lock (driver) { // We have to recheck that there is no reader, after we got the lock if (connection.Reader != null) { throw new MySqlException(Resources.DataReaderOpen); } System.Transactions.Transaction curTrans = System.Transactions.Transaction.Current; if (curTrans != null) { bool inRollback = false; if (driver.CurrentTransaction != null) { inRollback = driver.CurrentTransaction.InRollback; } if (!inRollback) { TransactionStatus status = TransactionStatus.InDoubt; try { // in some cases (during state transitions) this throws // an exception. Ignore exceptions, we're only interested // whether transaction was aborted or not. status = curTrans.TransactionInformation.Status; } catch (TransactionException) { } if (status == TransactionStatus.Aborted) { throw new TransactionAbortedException(); } } } commandTimer = new CommandTimer(connection, CommandTimeout); lastInsertedId = -1; cmdText = cmdText.Trim(); if (String.IsNullOrEmpty(cmdText)) { throw new InvalidOperationException(Resources.CommandTextNotInitialized); } string sql = cmdText.Trim(';'); if (CommandType == CommandType.TableDirect) { sql = "SELECT * FROM " + sql; } // if we are on a replicated connection, we are only allow readonly statements if (connection.Settings.Replication && !InternallyCreated) { EnsureCommandIsReadOnly(sql); } if (statement == null || !statement.IsPrepared) { if (CommandType == CommandType.StoredProcedure) { statement = new StoredProcedure(this, sql); } else { statement = new PreparableStatement(this, sql); } } // stored procs are the only statement type that need do anything during resolve statement.Resolve(false); // Now that we have completed our resolve step, we can handle our // command behaviors HandleCommandBehaviors(behavior); updatedRowCount = -1; try { MySqlDataReader reader = new MySqlDataReader(this, statement, behavior); connection.Reader = reader; canceled = false; // execute the statement statement.Execute(); // wait for data to return reader.NextResult(); success = true; return(reader); } catch (TimeoutException tex) { connection.HandleTimeoutOrThreadAbort(tex); throw; //unreached } catch (ThreadAbortException taex) { connection.HandleTimeoutOrThreadAbort(taex); throw; } catch (IOException ioex) { connection.Abort(); // Closes connection without returning it to the pool throw new MySqlException(Resources.FatalErrorDuringExecute, ioex); } catch (MySqlException ex) { if (ex.InnerException is TimeoutException) { throw; // already handled } try { ResetReader(); ResetSqlSelectLimit(); } catch (Exception) { // Reset SqlLimit did not work, connection is hosed. Connection.Abort(); throw new MySqlException(ex.Message, true, ex); } // if we caught an exception because of a cancel, then just return null if (ex.IsQueryAborted) { return(null); } if (ex.IsFatal) { Connection.Close(); } if (ex.Number == 0) { throw new MySqlException(Resources.FatalErrorDuringExecute, ex); } throw; } finally { if (connection != null) { if (connection.Reader == null) { // Something went seriously wrong, and reader would not // be able to clear timeout on closing. // So we clear timeout here. ClearCommandTimer(); } if (!success) { // ExecuteReader failed.Close Reader and set to null to // prevent subsequent errors with DataReaderOpen ResetReader(); } } } } }
public virtual void Close(MySqlDataReader reader) { }
internal void Close(MySqlDataReader reader) { if (statement != null) statement.Close(reader); ResetSqlSelectLimit(); if (statement != null && connection != null && connection.driver != null) connection.driver.CloseQuery(connection, statement.StatementId); ClearCommandTimer(); }
/// <include file='docs/mysqlcommand.xml' path='docs/ExecuteReader1/*'/> public new MySqlDataReader ExecuteReader(CommandBehavior behavior) { bool success = false; CheckState(); Driver driver = connection.driver; lock (driver) { // We have to recheck that there is no reader, after we got the lock if (connection.Reader != null) { throw new MySqlException(Resources.DataReaderOpen); } System.Transactions.Transaction curTrans = System.Transactions.Transaction.Current; if (curTrans != null) { bool inRollback = false; if (driver.CurrentTransaction != null) inRollback = driver.CurrentTransaction.InRollback; if (!inRollback) { TransactionStatus status = TransactionStatus.InDoubt; try { // in some cases (during state transitions) this throws // an exception. Ignore exceptions, we're only interested // whether transaction was aborted or not. status = curTrans.TransactionInformation.Status; } catch (TransactionException) { } if (status == TransactionStatus.Aborted) throw new TransactionAbortedException(); } } commandTimer = new CommandTimer(connection, CommandTimeout); lastInsertedId = -1; cmdText = cmdText.Trim(); if (String.IsNullOrEmpty(cmdText)) throw new InvalidOperationException(Resources.CommandTextNotInitialized); string sql = cmdText.Trim(';'); if (CommandType == CommandType.TableDirect) sql = "SELECT * FROM " + sql; // if we are on a replicated connection, we are only allow readonly statements if (connection.Settings.Replication && !InternallyCreated) EnsureCommandIsReadOnly(sql); if (statement == null || !statement.IsPrepared) { if (CommandType == CommandType.StoredProcedure) statement = new StoredProcedure(this, sql); else statement = new PreparableStatement(this, sql); } // stored procs are the only statement type that need do anything during resolve statement.Resolve(false); // Now that we have completed our resolve step, we can handle our // command behaviors HandleCommandBehaviors(behavior); updatedRowCount = -1; try { MySqlDataReader reader = new MySqlDataReader(this, statement, behavior); connection.Reader = reader; canceled = false; // execute the statement statement.Execute(); // wait for data to return reader.NextResult(); success = true; return reader; } catch (TimeoutException tex) { connection.HandleTimeoutOrThreadAbort(tex); throw; //unreached } catch (ThreadAbortException taex) { connection.HandleTimeoutOrThreadAbort(taex); throw; } catch (IOException ioex) { connection.Abort(); // Closes connection without returning it to the pool throw new MySqlException(Resources.FatalErrorDuringExecute, ioex); } catch (MySqlException ex) { if (ex.InnerException is TimeoutException) throw; // already handled try { ResetReader(); ResetSqlSelectLimit(); } catch (Exception) { // Reset SqlLimit did not work, connection is hosed. Connection.Abort(); throw new MySqlException(ex.Message, true, ex); } // if we caught an exception because of a cancel, then just return null if (ex.IsQueryAborted) return null; if (ex.IsFatal) Connection.Close(); if (ex.Number == 0) throw new MySqlException(Resources.FatalErrorDuringExecute, ex); throw; } finally { if (connection != null) { if (connection.Reader == null) { // Something went seriously wrong, and reader would not // be able to clear timeout on closing. // So we clear timeout here. ClearCommandTimer(); } if (!success) { // ExecuteReader failed.Close Reader and set to null to // prevent subsequent errors with DataReaderOpen ResetReader(); } } } } }
private MembershipUser GetUserFromReader(MySqlDataReader reader) { object providerUserKey = reader.GetInt32("userId"); string username = reader.GetString("name"); string email = null; if (!reader.IsDBNull(reader.GetOrdinal("Email"))) email = reader.GetString("Email"); string passwordQuestion = ""; if (!(reader.GetValue(reader.GetOrdinal("PasswordQuestion")) == DBNull.Value)) passwordQuestion = reader.GetString("PasswordQuestion"); string comment = ""; if (!(reader.GetValue(reader.GetOrdinal("Comment")) == DBNull.Value)) comment = reader.GetString("Comment"); bool isApproved = reader.GetBoolean("IsApproved"); bool isLockedOut = reader.GetBoolean("IsLockedOut"); DateTime creationDate = reader.GetDateTime("CreationDate"); DateTime lastLoginDate = new DateTime(); if (!(reader.GetValue(reader.GetOrdinal("LastLoginDate")) == DBNull.Value)) lastLoginDate = reader.GetDateTime("LastLoginDate"); DateTime lastActivityDate = reader.GetDateTime("LastActivityDate"); DateTime lastPasswordChangedDate = reader.GetDateTime("LastPasswordChangedDate"); DateTime lastLockedOutDate = new DateTime(); if (!(reader.GetValue(reader.GetOrdinal("LastLockedoutDate")) == DBNull.Value)) lastLockedOutDate = reader.GetDateTime("LastLockedoutDate"); MembershipUser u = new MembershipUser(Name, username, providerUserKey, email, passwordQuestion, comment, isApproved, isLockedOut, creationDate, lastLoginDate, lastActivityDate, lastPasswordChangedDate, lastLockedOutDate); return u; }
private void AdjustOutputTypes(MySqlDataReader reader) { // since MySQL likes to return user variables as strings // we reset the types of the readers internal value objects // this will allow those value objects to parse the string based // return values for (int i = 0; i < reader.FieldCount; i++) { string fieldName = reader.GetName(i); if (fieldName.IndexOf(StoredProcedure.ParameterPrefix) != -1) fieldName = fieldName.Remove(0, StoredProcedure.ParameterPrefix.Length + 1); MySqlParameter parameter = command.Parameters.GetParameterFlexible(fieldName, true); IMySqlValue v = MySqlField.GetIMySqlValue(parameter.MySqlDbType); if (v is MySqlBit) { MySqlBit bit = (MySqlBit)v; bit.ReadAsString = true; reader.ResultSet.SetValueObject(i, bit); } else reader.ResultSet.SetValueObject(i, v); } }
internal void ProcessOutputParameters(MySqlDataReader reader) { // We apparently need to always adjust our output types since the server // provided data types are not always right AdjustOutputTypes(reader); // now read the output parameters data row CommandBehavior behavior = reader.CommandBehavior; if ((behavior & CommandBehavior.SchemaOnly) != 0) return; if (!reader.Read()) return; //reader.ResultSet.NextRow(behavior); string prefix = "@" + StoredProcedure.ParameterPrefix; for (int i = 0; i < reader.FieldCount; i++) { string fieldName = reader.GetName(i); if (fieldName.StartsWith(prefix)) fieldName = fieldName.Remove(0, prefix.Length); MySqlParameter parameter = command.Parameters.GetParameterFlexible(fieldName, true); parameter.Value = reader.GetValue(i); } }
protected virtual void Dispose(bool disposing) { // Avoid cyclic calls to Dispose. if (disposeInProgress) return; disposeInProgress = true; try { ResetTimeout(1000); if (disposing) handler.Close(isOpen); // if we are pooling, then release ourselves if (connectionString.Pooling) MySqlPoolManager.RemoveConnection(this); } catch (Exception) { if (disposing) throw; } finally { reader = null; isOpen = false; disposeInProgress = false; } }
private static string GetString(MySqlDataReader reader, int index) { if (reader.IsDBNull(index)) return null; return reader.GetString(index); }