I am using IBM OLE DB Provider for connecting to DB2.
I can open more than one DataReader on a single OleDbConnection.Does this provider implicitly opens an additional connection for each DataReader.
If so,will this connections closed automatically or stay open until the connection get time-out.
OleDbConnection connection = new (connectionString);
OleDbDataReader reader = null;
try
{
connection.Open();
reader = OleDbHelpher.ExecuteNonQuery(connection, CommandType.Text,query1);
while (reader.Read())
{
Console.WriteLine(reader[0].ToString());
}
reader.Close();
reader = OleDbHelpher.ExecuteNonQuery(connection, CommandType.Text,query2);
while (reader.Read())
{
Console.WriteLine(reader[0].ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
connecton.Close();
}
If you have a try/finally block the way you have it now, the connection will always be closed.
No, you can’t open more than one
DataReaderon a single connection. You will get an exception if you attempt something like this.From MSDN:
Instead, open 2 connections (don’t worry about the penalty since most likely your connections are pooled anyway) and make sure that you close the connection in a finally block or instead use a
usingstatement.