小编Joe*_*erg的帖子

我可以在阅读时中断数据读取器吗?

可以阻止正在运行的读卡器吗?

场景:我有一个包含100000个数据集的表

CREATE TABLE stock (
uid bigint NOT NULL,
name text,
quantity integer,
x bytea,
y bytea
);
Run Code Online (Sandbox Code Playgroud)

和一个用于读取数据的控制台应用程序(.NET 4.0,Npgsql 2.0.11.0/2.0.11.92)

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;
}
Run Code Online (Sandbox Code Playgroud)

我观察到数据阅读器无法真正停止.似乎数据读取器首先读取所有行,然后正常返回.

此示例是较大应用程序的摘要,用户将通过按下按钮来停止数据读取器,因为读取时间过长.

c# ado.net npgsql

7
推荐指数
2
解决办法
3993
查看次数

标签 统计

ado.net ×1

c# ×1

npgsql ×1