开发者

Open more than one DataReader on a single OleDbConnection

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());
      }
    }
    开发者_如何学Pythoncatch (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 DataReader on a single connection. You will get an exception if you attempt something like this.

From MSDN:

Note that while a DataReader is open, the Connection is in use exclusively by that DataReader. You cannot execute any commands for the Connection, including creating another DataReader, until the original DataReader is closed.

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 using statement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜