请教一个关于DATAREADER的小问题?
下面的代码运行的时候为什么老是提示我:DataReader未关闭?可我已经关闭了呀!
==============================================
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["sqlConn"]);
myConnection.Open();
// 启动一个事务
SqlTransaction myTrans = myConnection.BeginTransaction();
SqlDataReader dr;
// 为事务创建一个命令
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.Transaction = myTrans;
try
{
//获取题型名称
myCommand.CommandText = string.Format("select C_TypeClassName from tbl_TypeName where I_TypeNameID = {0}",typeNameID);
dr = myCommand.ExecuteReader();
if (dr.Read())
{
strTypeName = dr.GetString(0);
}
if (!dr.IsClosed)
{
dr.Close();
}
//修改科目相应统计信息
myCommand.CommandText = string.Format("select I_TKKMID,C_Statistic from tbl_TKKMBase where I_TKKMID = {0} or C_ChildIDs like '%' + {0} + '%'", tkkmID);
dr = myCommand.ExecuteReader();
while (dr.Read())
{
strTKKMID += dr.GetInt32(0).ToString() + ",";
strStatistic += dr.GetString(1) + ",";
}
if (!dr.IsClosed)
{
dr.Close();
}
myTrans.Commit();
flag = 1;
}
catch (Exception e)
{
myTrans.Rollback();
throw (e);
}
finally
{
myConnection.Close();
}
------解决方案--------------------建議樓主在對數據庫操作的地方使用useing語句,另:打開數據庫鏈接的時候首先判斷其打開狀態再操作.
------解决方案--------------------在finally里面关闭
------解决方案--------------------up
--
------解决方案--------------------C# code
SqlDataReader dr = null; //声明的时候=null
catch (Exception er)
{
if ( dr!=null && !dr.IsClosed)
{
dr.Close();
}
myTrans.Rollback();
throw (er);
}