请教,错误:并非所有的代码路径都返回值
错误1“UserDao.GetLogin()”: 并非所有的代码路径都返回值
请问如何修改?
public bool GetLogin()
{
SqlConnection con = null;
try
{
con = new SqlConnection(cs);
SqlCommand cmd = con.CreateCommand();
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "WEB_Login ";
cmd.Parameters.Add( "@userName ", SqlDbType.VarChar, 50).Value = user.UserName;
cmd.Parameters.Add( "@userPwd ", SqlDbType.VarChar, 50).Value = user.UserPwd;
object o = cmd.ExecuteScalar();
if (o != null)
{
int iCount = Int32.Parse(o.ToString());
if (iCount == 0)
{
return true;
}
else
{
return false;
}
}
}
finally
{
if (con != null)
{
con.Close();
}
}
}
[解决办法]
finally
{
if (con != null)
{
con.Close();
}
}
return false;
}
[解决办法]
public bool GetLogin()
{
SqlConnection con = null;
bool bol = false;
try
{
con = new SqlConnection(cs);
SqlCommand cmd = con.CreateCommand();
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "WEB_Login ";
cmd.Parameters.Add( "@userName ", SqlDbType.VarChar, 50).Value = user.UserName;
cmd.Parameters.Add( "@userPwd ", SqlDbType.VarChar, 50).Value = user.UserPwd;
object o = cmd.ExecuteScalar();
if (o != null)
{
int iCount = Int32.Parse(o.ToString());
if (iCount == 0)
{
bol = true;
}
}
}
finally
{
if (con != null)
{
con.Close();
}
}
return bol ;
}
[解决办法]
最好在return之前把con给close();
[解决办法]
if (o != null) 当if (o == null)的时候还需要返回值,finally也要
[解决办法]
return 前也会执行finally的```
finally
{
if (con != null)
{
con.Close();
}
}
return false;
}
[解决办法]
public bool GetLogin()
{
SqlConnection con = null;
try
{
con = new SqlConnection(cs);
SqlCommand cmd = con.CreateCommand();
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "WEB_Login ";
cmd.Parameters.Add( "@userName ", SqlDbType.VarChar, 50).Value = user.UserName;
cmd.Parameters.Add( "@userPwd ", SqlDbType.VarChar, 50).Value = user.UserPwd;
object o = cmd.ExecuteScalar();
if (o != null)
{
int iCount = Int32.Parse(o.ToString());
if (iCount == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
finally
{
if (con != null)
{
con.Close();
}
}
}