mysql sqldatareader_using与SqlDataReader之嵌套使用范例

如果在程序中,大量使用SqlDataReader对象的话,会导致连接池耗尽的情况发生。仍然,有些时候,我们又不得不使用多个SqlDataReader。

因此,我们要解决的办法是,即要使用多个SqlDataReader,同时又不能让连接池耗尽,所以,我们得解决连接及时的关闭问题。

下面为大家提供范例,如何使用这两者对象。

using (SqlConnection Conn = new SqlConnection()) //第一个连接

{

Conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["CoachExamOnlineStr"].ToString();

Conn.Open();

using (SqlCommand Cmd = Conn.CreateCommand())

{

Cmd.CommandText = MySqlStr;

using (SqlDataReader MySqlReader = Cmd.ExecuteReader())  //

{

while (MySqlReader.Read())

{

using (SqlConnection Conn1 = new SqlConnection()) //第二个连接对象Conn1,因为如果沿用上面的Conn会导致SqlReader未关闭的错误提示

{

Conn1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["CoachExamOnlineStr"].ToString();

Conn1.Open();

SqlCommand Cmd2 = Conn1.CreateCommand();

Cmd2.CommandText = "select ID,ExamName from GetToExamBase where ID=1" ;

(CheckAddResult(Cmd2)) //调用函数,传递的参数是SqlCommand对象

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值