繁体   English   中英

如何在一个连接中添加多个查询(Visual Studio C# 到 PostgreSQL)

[英]How Can I add more then one query in one connection (Visual Studio C# to PostgreSQL)

我将数据添加到数据库

NpgsqlConnection conn1 = new NpgsqlConnection("Server=localhost;Port=5432;Database=test;User Id=postgres;Password=postgres;");
conn.Open();

NpgsqlCommand comm = new NpgsqlCommand();
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = "SELECT * FROM test";
NpgsqlDataReader dr = comm.ExecuteReader();

if (dr.HasRows)
{
    dt.Load(dr);
    dataGridView1.DataSource = dt;
}

comm.Dispose();
conn.Close();

到目前为止一切都很好,但我想在一个连接中添加一个以上的查询,例如

comm.CommandText = "SELECT * FROM test";
NpgsqlDataReader dr = comm.ExecuteReader();

// some code for view data in dataGridView1

comm.CommandText = "SELECT * FROM test1";
NpgsqlDataReader dr = comm.ExecuteReader();

// and some code for view data in dataGridView2

但我不能

或者也许使用事务,但我不知道如何使用它。

使用数据适配器让生活更轻松:

var conn = "Server=localhost;Port=5432;Database=test;User Id=postgres;Password=postgres;"

var da = new NpgsqlDataAdapter("SELECT * FROM one", conn);
var dt = new DataTable();
da.Fill(dt);
grid1.DataSource = dt;

da = new NpgsqlDataAdapter("SELECT * FROM two", conn);
dt = new DataTable();
da.Fill(dt);
grid2.DataSource = dt;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM