Good afternoon, I have a problem executing a query in C # using NPGSQL.
try
{
bdConn.Open();
NpgsqlCommand sql = new NpgsqlCommand();
sql.Connection = bdConn;
sql.CommandText = query;
sql.CommandTimeout = 600000;
NpgsqlDataReader dr = sql.ExecuteReader();
while (dr.Read())
{
string xml = dr[1].ToString();
xml = xml.Replace("<![CDATA[", "").Replace("]]>", "");
switch (cbbDocumento.Text)
{
case "NFe": System.IO.File.WriteAllText(tbPasta.Text + "\" + dr[0].ToString() + "-nfe.xml", xml); break;
case "NFCe": System.IO.File.WriteAllText(tbPasta.Text + "\" + dr[0].ToString() + "-nfce.xml", xml); break;
case "CTe": System.IO.File.WriteAllText(tbPasta.Text + "\" + dr[0].ToString() + "-cte.xml", xml); break;
case "MDFe": System.IO.File.WriteAllText(tbPasta.Text + "\" + dr[0].ToString() + "-mdfe.xml", xml); break;
}
}
}
finally
{
bdConn.Close();
}
In this section, after the ExecuteReader the connection is still open, and as long as there are records to be read the query continues to be executed in the database.
This ends up consuming a lot of processing from my database server.
Is there any way to execute a query and close this connection before starting to work with the returned data?
Thank you.
Update: The result of this query has about 80,000 records and I save the result in a physical location. The problem is that it takes about 30 minutes to save this amount of records and this is "killing" the database server.