asp.net为什么dataReader关闭了还提示未关闭
为什么写了DataReader.Close()还是说没关闭?我连续调用了二次有DataReader的函数,函数里写了Close()了,怎么还是说要先关闭?两个方法代码如下...
为什么写了DataReader.Close()还是说没关闭?
我连续调用了二次有DataReader的函数,函数里写了Close()了,怎么还是说要先关闭? 两个方法代码如下:
///<summary>
///获取一个用户类型
///</summary>
public static UsersType GetOneUsersType(int id)
{
string sql = "select * from UsersType where id="+id+"";
UsersType userType = new UsersType();
SqlDataReader dr = DBHelper.GetReader(sql);
userType.Id = dr.GetInt32(0);
userType.UserType = dr.GetInt32(1);
dr.Close();
return userType;
}
///<summary>
///检查用户名和密码
///</summary>
public static Users CheckUser(string username, string password)
{
Users user = new Users();
string sql = "select * from Users where username="+username+" and password="+password+"";
SqlDataReader dr = DBHelper.GetReader2(sql);
while (dr.Read())
{
user.Id = int.Parse(dr[0].ToString());
user.userName = dr[1].ToString();
user.Password = dr[2].ToString();
user.UserTypeId = int.Parse(((UsersType)UsersTypeSevice.GetOneUsersType(int.Parse(dr[3].ToString()))).UserType.ToString());
}
dr.Close();
return user;
}
为了方便 我在检查用户名密码的时候就没有用bool值了,方便接收,大家看看这两个方法上面的先执行,可是dataReader老是提示未关闭。
private static SqlConnection connection;
public static SqlConnection Connection{
get {
string connectionString = "server=.;database=WebSite;uid=sa;pwd=";
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
public static SqlDataReader GetReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
那个2打错了 嘿嘿
粘不完了 展开
我连续调用了二次有DataReader的函数,函数里写了Close()了,怎么还是说要先关闭? 两个方法代码如下:
///<summary>
///获取一个用户类型
///</summary>
public static UsersType GetOneUsersType(int id)
{
string sql = "select * from UsersType where id="+id+"";
UsersType userType = new UsersType();
SqlDataReader dr = DBHelper.GetReader(sql);
userType.Id = dr.GetInt32(0);
userType.UserType = dr.GetInt32(1);
dr.Close();
return userType;
}
///<summary>
///检查用户名和密码
///</summary>
public static Users CheckUser(string username, string password)
{
Users user = new Users();
string sql = "select * from Users where username="+username+" and password="+password+"";
SqlDataReader dr = DBHelper.GetReader2(sql);
while (dr.Read())
{
user.Id = int.Parse(dr[0].ToString());
user.userName = dr[1].ToString();
user.Password = dr[2].ToString();
user.UserTypeId = int.Parse(((UsersType)UsersTypeSevice.GetOneUsersType(int.Parse(dr[3].ToString()))).UserType.ToString());
}
dr.Close();
return user;
}
为了方便 我在检查用户名密码的时候就没有用bool值了,方便接收,大家看看这两个方法上面的先执行,可是dataReader老是提示未关闭。
private static SqlConnection connection;
public static SqlConnection Connection{
get {
string connectionString = "server=.;database=WebSite;uid=sa;pwd=";
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
public static SqlDataReader GetReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
那个2打错了 嘿嘿
粘不完了 展开
5个回答
展开全部
public static string connectionString = ConfigurationManager.ConnectionStrings["ABCConnectionString"].ConnectionString;
/// <summary>
/// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>SqlDataReader</returns>
public static SqlDataReader ExecuteReader(string strSQL)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(strSQL, connection);
try
{
connection.Open();
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.SqlClient.SqlException e)
{
throw e;
}
}
/// <summary>
/// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>SqlDataReader</returns>
public static SqlDataReader ExecuteReader(string strSQL)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(strSQL, connection);
try
{
connection.Open();
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.SqlClient.SqlException e)
{
throw e;
}
}
展开全部
SqlDataReader reader = cmd.ExecuteReader();
return reader;
你直接改为
return cmd.ExecuteReader();
return reader;
你直接改为
return cmd.ExecuteReader();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using(SqlDataReader dr = DBHelper.GetReader(sql)
)
{
//代码
}
你这样写试试
)
{
//代码
}
你这样写试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
怎么一会儿 SqlDataReader dr = DBHelper.GetReader(sql);
一会儿有 SqlDataReader dr = DBHelper.GetReader2(sql);
一会儿有 SqlDataReader dr = DBHelper.GetReader2(sql);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是不是你那个DBHelper有问题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询