首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

请问,异常:并非所有的代码路径都返回值

2011-12-31 
请教,错误:并非所有的代码路径都返回值错误1“UserDao.GetLogin()”:并非所有的代码路径都返回值请问如何修

请教,错误:并非所有的代码路径都返回值
错误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();
}
}
}

热点排行