Index: System.Data.SQLite/SQLiteDataReader.cs ================================================================== --- System.Data.SQLite/SQLiteDataReader.cs +++ System.Data.SQLite/SQLiteDataReader.cs @@ -77,11 +77,11 @@ /// internal void Initialize() { _activeStatementIndex = -1; _activeStatement = null; - _rowsAffected = 0; + _rowsAffected = -1; _fieldCount = -1; } /// /// Closes the datareader, potentially closing the connection as well if CommandBehavior.CloseConnection was specified. @@ -663,34 +663,33 @@ while (true) { if (_activeStatement != null && stmt == null) { + // Reset the previously-executed statement + _activeStatement._sql.Reset(_activeStatement); + // If we're only supposed to return a single rowset, step through all remaining statements once until // they are all done and return false to indicate no more resultsets exist. if ((_commandBehavior & CommandBehavior.SingleResult) != 0) { - // Reset the previously-executed command - _activeStatement._sql.Reset(_activeStatement); - for (; ; ) { - stmt = _command.GetStatement(_activeStatementIndex); - _activeStatementIndex++; + stmt = _command.GetStatement(_activeStatementIndex + 1); if (stmt == null) break; + _activeStatementIndex++; stmt._sql.Step(stmt); - _rowsAffected += stmt._sql.Changes; + if (stmt._sql.ColumnCount(stmt) == 0) + { + if (_rowsAffected == -1) _rowsAffected = 0; + _rowsAffected += stmt._sql.Changes; + } stmt._sql.Reset(stmt); // Gotta reset after every step to release any locks and such! } return false; } - else - { - // Reset the previously-executed command - _activeStatement._sql.Reset(_activeStatement); - } } // Get the next statement to execute stmt = _command.GetStatement(_activeStatementIndex + 1); @@ -704,19 +703,20 @@ _activeStatementIndex++; fieldCount = stmt._sql.ColumnCount(stmt); - // If we're told to get schema information only, then don't perform an initial step() through the resultset + // If the statement is not a select statement or we're not retrieving schema only, then perform the initial step if ((_commandBehavior & CommandBehavior.SchemaOnly) == 0 || fieldCount == 0) { if (stmt._sql.Step(stmt)) { _readingState = -1; } else if (fieldCount == 0) // No rows returned, if fieldCount is zero, skip to the next statement { + if (_rowsAffected == -1) _rowsAffected = 0; _rowsAffected += stmt._sql.Changes; stmt._sql.Reset(stmt); continue; // Skip this command and move to the next, it was not a row-returning resultset } else // No rows, fieldCount is non-zero so stop here @@ -776,11 +776,11 @@ /// /// Retrieve the count of records affected by an update/insert command. Only valid once the data reader is closed! /// public override int RecordsAffected { - get { return (IsClosed) ? _rowsAffected : -1; } + get { return _rowsAffected; } } /// /// Indexer to retrieve data from a column given its name ///