Can I stop a working reader?
Scenario: I have a table with 100,000 data sets
CREATE TABLE stock ( uid bigint NOT NULL, name text, quantity integer, x bytea, y bytea );
and a console application (.NET 4.0, Npgsql 2.0.11.0/2.0.11.92) for reading data
conn = new NpgsqlConnection("Server=localhost;Database=postgres;User id=postgres;password=postgres;Timeout=600;CommandTimeout=600;ConnectionLifeTime=600;"); using (new ConnectionOpen(conn)) using (var ta = conn.BeginTransaction(IsolationLevel.Snapshot)) { IDbCommand command = conn.CreateCommand("SELECT * from stock;"); command.SetTransaction(ta); IDataReader reader = command.ExecuteReader(); int n = 0; while (!reader.IsClosed && reader.Read()) { n++; if (n > 5000) { if (reader != null) { ((NpgsqlDataReader)reader).Close(); } } } ((NpgsqlDataReader)reader).Dispose(); reader = null; }
I observed that the data reader cannot really be stopped. It seems the data reader first reads all the rows and returns after that.
This example is an abstraction of a larger application in which the user stops the data reader by pressing a button because reading takes too much time.
Joerg source share