c#数据库访问返回值类型为SqlDataReader时使用using时注意的问题
2014-04-18 13:42
806 查看
在封装通用 SQLSERVER 数据可访问方法时,如果返回值类型为 SqlDataReader ,那么在创建连接字符串的时候,我们不能写成如下
public static SqlDataReader ExecuteReader(string strSQL)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(strSQL, connection))
{
try
{
connection.Open();
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
}
}
}
你在使用using创建的时候,在SqlDataReader 赋值后return时,SqlConnection 就会被释放资源,连接就会被关闭。而我们传递过去的SqlDataReader 是引用类型,接收传递过去的SqlDataReader 的地方调用的时候,就会提示连接已经被关闭,无法调用,因为 using (SqlConnection connection = new SqlConnection(connectionString))在方法结束时,就把资源释放了,并关闭了连接,为了正常接收传递过去的SqlDataReader ,在创建连接的时候不能用using,正确的写法如下
public static SqlDataReader ExecuteReader(string strSQL)
{
SqlConnection connection = new SqlConnection(connectionString);
using (SqlCommand cmd = new SqlCommand(strSQL, connection))
{
try
{
connection.Open();
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
}
}
public static SqlDataReader ExecuteReader(string strSQL)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(strSQL, connection))
{
try
{
connection.Open();
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
}
}
}
你在使用using创建的时候,在SqlDataReader 赋值后return时,SqlConnection 就会被释放资源,连接就会被关闭。而我们传递过去的SqlDataReader 是引用类型,接收传递过去的SqlDataReader 的地方调用的时候,就会提示连接已经被关闭,无法调用,因为 using (SqlConnection connection = new SqlConnection(connectionString))在方法结束时,就把资源释放了,并关闭了连接,为了正常接收传递过去的SqlDataReader ,在创建连接的时候不能用using,正确的写法如下
public static SqlDataReader ExecuteReader(string strSQL)
{
SqlConnection connection = new SqlConnection(connectionString);
using (SqlCommand cmd = new SqlCommand(strSQL, connection))
{
try
{
connection.Open();
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
}
}
相关文章推荐
- asp.net中SqlDataReader使用时关闭数据库连接的问题(转)
- C# 使用SqlDataReader 注意问题
- ADO.NET访问数据库-SqlCommand的ExecuteReader方法一般配合sqldatareader使用
- 使用SqlDataReader应该注意的问题,使用datareader没有获取到分页的output值【来自Mist .NET blog】
- 使用SqlDataReader来操作数据库
- ASP.NET优化连载(三)只读数据访问用SqlDataReader,不要使用DataSet
- 使用Visual Basic访问数据库几个注意的问题
- 使用Visual Basic访问数据库几个注意的问题
- 数据库文件提取 dataset 和 SqlDataReader 的使用
- 使用SqlDataReader注意的几点
- system.Data.SqlClient.SqlError:因为数据库正在使用,所以无法获得对数据库的独占访问权
- 使用SqlDataReader和SqlDataAdapter的注意
- 使用SqlDataReader注意的几点
- system.Data.SqlClient.SqlError:因为数据库正在使用,所以无法获得对数据库的独占访问权
- sqldatareader:使用SqlDataReader注意的几点
- 使用OleDbDataReader 或sqldatareader 连数据库
- using语句下使用SqlDataReader异常:Invalid attempt to Read when reader is closed
- 微软企业库中数据库访问代码问题(System.InvalidOperationException: Parameter '@FileContent' exceeds the size limit for the sql_variant datatype.)
- 使用SqlDataReader注意的几点
- 使用SqlDataReader的注意点