SqlDataReader类型的变量,会自动Close.
比如用下面函数传递SqlDataReader类型的变量,接收时,会得到空值,原因是SqlDataReader会自动关闭,即Close()
函数
如果做为参数和返回值传递是比较危险的,因为你要传出去就Close(),而究竟在哪里关闭容易被遗忘和忽略
所以我觉得还是用强类型的DataSet做为参数和返回值传递纪录集比较好。
使用SqlDataReader注意的几点:
比如用下面函数传递SqlDataReader类型的变量,接收时,会得到空值,原因是SqlDataReader会自动关闭,即Close()
SqlDataReader dataReader
=
ExecuteReader(sqlstring)
函数
public
static
SqlDataReader ExecuteReader(
string
strSQL)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(strSQL,connection);
try
{
connection.Open();
SqlDataReader myReader = cmd.ExecuteReader();
return myReader;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
connection.Dispose();
throw new Exception(e.Message);
}
}
}
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(strSQL,connection);
try
{
connection.Open();
SqlDataReader myReader = cmd.ExecuteReader();
return myReader;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
connection.Dispose();
throw new Exception(e.Message);
}
}
}
如果做为参数和返回值传递是比较危险的,因为你要传出去就Close(),而究竟在哪里关闭容易被遗忘和忽略
所以我觉得还是用强类型的DataSet做为参数和返回值传递纪录集比较好。
使用SqlDataReader注意的几点:
1、当SqlDataReader没有关闭之前,数据库连接会一直保持open状态,所以在使用SqlDataReader时,使用完毕应该马上调用SqlDataReader.Close()关闭它。
2、一个连接只能被一个SqlDataReader使用,这也是为什么要尽早关闭SqlDataReader的原因。
3、使用完SqlDataReader后,你可以在程序中显示的调用数据库连接对象的Close方法关闭连接,也可以在调用Command对象的ExecuteReader方法时传递CommandBehavior.CloseConnection 这个枚举变量,这样在调用SqlDataReader的Close方法时会自动关闭数据库连接。
4、使用SqlDataReader时尽量使用和数据库字段类型匹配的方法来取得相应的值,比如对于整形的字段使用GetInt32,对字符类型的字段使用GetString。这样会减少因为类型不一致而额外增加的类型转换操作。
5、使用SqlDataReader获取多条记录时,如果没有访问到取出记录的末尾时想要关闭SqlDataReader,应该先调用Command对象的Cancel方法,然后再调用SqlDataReader的Close方法。Command对象的Cancel方法使得数据库不再把SqlDataReader中未访问的数据发送到调用端,如果不调用此方法直接关闭SqlDataReader,数据库会发送和SqlDataReader未访问数据等长的空数据流到调用端。
6、如果想通过SqlCommand的ExecuteReader方法获取存储过程的返回值或者输出参数,必须先调用SqlDataReader的Close方法后,才能获取输出参数的值或者返回值。
7、如果使用SqlDataReader只返回一条记录,那么在调用Command的ExecuteReader方法时,指定
CommandBehavior.SingleRow参数,这个参数的是否使用对SQL Server .NET Data Provider没有什么影响,但是当你使用OLE DB .NET Data Provider时,指定这个参数后,DataPrivider内部将使用IRow接口,而不是使用相对来说耗费资源的IRowSet接口。
2、一个连接只能被一个SqlDataReader使用,这也是为什么要尽早关闭SqlDataReader的原因。
3、使用完SqlDataReader后,你可以在程序中显示的调用数据库连接对象的Close方法关闭连接,也可以在调用Command对象的ExecuteReader方法时传递CommandBehavior.CloseConnection 这个枚举变量,这样在调用SqlDataReader的Close方法时会自动关闭数据库连接。
4、使用SqlDataReader时尽量使用和数据库字段类型匹配的方法来取得相应的值,比如对于整形的字段使用GetInt32,对字符类型的字段使用GetString。这样会减少因为类型不一致而额外增加的类型转换操作。
5、使用SqlDataReader获取多条记录时,如果没有访问到取出记录的末尾时想要关闭SqlDataReader,应该先调用Command对象的Cancel方法,然后再调用SqlDataReader的Close方法。Command对象的Cancel方法使得数据库不再把SqlDataReader中未访问的数据发送到调用端,如果不调用此方法直接关闭SqlDataReader,数据库会发送和SqlDataReader未访问数据等长的空数据流到调用端。
6、如果想通过SqlCommand的ExecuteReader方法获取存储过程的返回值或者输出参数,必须先调用SqlDataReader的Close方法后,才能获取输出参数的值或者返回值。
7、如果使用SqlDataReader只返回一条记录,那么在调用Command的ExecuteReader方法时,指定
CommandBehavior.SingleRow参数,这个参数的是否使用对SQL Server .NET Data Provider没有什么影响,但是当你使用OLE DB .NET Data Provider时,指定这个参数后,DataPrivider内部将使用IRow接口,而不是使用相对来说耗费资源的IRowSet接口。