获取一个SqlDataReader对象怎么利用using try/finally实现资源的释放啊?
下面的这样写是不是只能起到一个关闭连接的作用,达不到彻底释放资源的效果啊
SqlConnection sqlConn = null;
SqlCommand sqlComm = null;
SqlDataReader sqlDR = null;
sqlConn = new SqlConnection(_sqlConnectionString);
sqlComm = new SqlCommand(storedProcedureName, sqlConn);
sqlComm.CommandType = CommandType.StoredProcedure;
sqlConn.Open();
sqlDR = sqlComm.ExecuteReader(CommandBehavior.CloseConnection);
return sqlDR;
下面的这样写是不是只能起到一个关闭连接的作用,达不到彻底释放资源的效果啊
SqlConnection sqlConn = null;
SqlCommand sqlComm = null;
SqlDataReader sqlDR = null;
sqlConn = new SqlConnection(_sqlConnectionString);
sqlComm = new SqlCommand(storedProcedureName, sqlConn);
sqlComm.CommandType = CommandType.StoredProcedure;
sqlConn.Open();
sqlDR = sqlComm.ExecuteReader(CommandBehavior.CloseConnection);
return sqlDR;
解决方案 »
- WPF:为什么这个工程的代码,编译能通过,运行时会崩溃?错在哪里了?
- 如何用C#做一个这样的界面?
- C#中如何让Form逐渐消失
- 未将对象引用设置到对象的实例
- 各位大侠帮下忙,关于条码软件barTender的调用
- 自动生成TextBox控件的问题!求助!!
- 用c#实现,多线程程序,例如其中一个实现 i++ ;另一个实现 i-- ;该如何实现,最好的方法是什么?
- 关于C#进度条
- string的构造函数,怎么会是C/C++风格的char*呢?
- 请大家分析一下怎样写一个电力设备台帐管理系统?
- 怎样获取listbox数据源的隐藏信息?
- HTTP 错误 500.24 - Internal Server Error
/// <summary>
/// 取得返回的数据
/// </summary>
/// <returns></returns>
public DataTable GetResult()
{
DataTable dtResult = new DataTable();
using (SqlConnection sqlConn = new SqlConnection(_sqlConnectionString))
{
sqlConn.Open();
using (SqlCommand sqlComm = new SqlCommand(storedProcedureName, sqlConn))
{
sqlComm.CommandType = CommandType.StoredProcedure;
using (SqlDataReader sdr = sqlComm.ExecuteReader(CommandBehavior.CloseConnection))
{
if(null!=sdr&&sdr.HasRows&&sdr.Read())
dtResult.Load(sdr);
}
}
}
return dtResult;
}
/// <summary>
/// 取得返回的数据
/// </summary>
/// <returns></returns>
public DataTable GetResult()
{
DataTable dtResult = new DataTable();
using (SqlConnection sqlConn = new SqlConnection(_sqlConnectionString))
{
sqlConn.Open();
using (SqlCommand sqlComm = new SqlCommand(storedProcedureName, sqlConn))
{
sqlComm.CommandType = CommandType.StoredProcedure;
using (SqlDataReader sdr = sqlComm.ExecuteReader(CommandBehavior.CloseConnection))
{
if(null!=sdr&&sdr.HasRows&&sdr.Read())
dtResult.Load(sdr);
}
}
}
return dtResult;
}