在单个 OleDbConnection 上打开多个 DataReader
我正在使用 IBM OLE DB Provider 连接到 DB2。
我可以在单个 OleDbConnection 上打开多个 DataReader。此提供程序是否隐式为每个 DataReader 打开一个附加连接。
如果是这样,该连接会自动关闭还是保持打开状态直到连接超时。
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您以现在的方式使用 try/finally 阻塞,则连接将始终关闭。
不可以,您无法在单个连接上打开多个
DataReader
。如果你尝试这样的事情,你将会得到一个例外。来自 MSDN:
相反,打开 2 个连接(不要担心惩罚,因为很可能您的连接无论如何都会被池化)并确保在 finally 块中关闭连接,或者使用
using
语句。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:
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.