ASP.NET新手问题?

来源:百度知道 编辑:UC知道 时间:2024/05/09 13:25:54
在一个类一个写了一个方法,方法如下
/// <summary>
/// 判断用户登陆
/// </summary>
public bool ExecuteReaders(string strSql)
{
OleDbConnection con = new OleDbConnection(strCon);
OleDbCommand cmd = new OleDbCommand(strSql,con);
OleDbDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
return true;
}
else
{
return false;
}
dr.Close();
con.Close();
}

然后调用这个方法实现用户登陆,虽然没有错误,但是编译时会出现警告,检测到无法访问的代码dr.Close();,请问各位朋友知道为什么吗?怎么写才能解决这个问题?

if(dr.Read())
{
return true;
}
else
{
return false;
}
dr.Close();
con.Close();
}

你的代码写的有问题.因为不管dr.Read()为真还是为假,你的代码都是直接返回(return true或return false),既然都退出这个函数了,那下面代码当然不要执行了.代码应该这样写:
if(dr.Read())
{
dr.Close();
con.Close();
return true;
}
else
{
dr.Close();
con.Close();
return false;
}

}

/// <summary>
/// 判断用户登陆
/// </summary>
public bool ExecuteReaders(string strSql)
{
OleDbConnection con = new OleDbConnection(strCon);

con.Open();

OleDbCommand cmd = new OleDbCommand(strSql,con);
OleDbDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
return true;
}
else
{
return false;
}
dr.Close();
con.Close();
}

我也是新手。。不过我猜。。DR是不是应该定义一下?我猜的~~说错了别砸我~