c#中“并非所有的代码路径都返回值”

呵呵112123 2009-07-22 01:28:00
public string sqlexe(string sql)
{
cmd = new SqlCommand(sql,con);
con.Open();
string bbn = cmd.ExecuteScalar().ToString();
con.Close();
return bbn;
}

public void sqlexe2(string sql)
{
cmd = new SqlCommand(sql, con);
con.Open();
string bbn = cmd.ExecuteNonQuery().ToString();
con.Close();
}

public int loginlog(string a, string b,string c)
{
bool IsOverMaxLoginCount = true;
int custid=0;
//取CustomerID
string sql = "SELECT CustomerID FROM Customer where (LoginName = " + a + ") AND (PassWord = " + b + ")";
string bdf = sqlexe(sql);
if (bdf != null)
{
custid = Convert.ToInt32(bdf);
}
else
{
return 0;//0 用户名或密码不存在
}
//用户过期判断
string sql1 = "SELECT COUNT(*) FROM Customer where CustomerID= "+custid +" and (ExpiryTime>getdate() or ExpiryTime is null)";
string jkh= sqlexe(sql1);
if (jkh == null)
{
return 0;//0 用户名或密码不存在
}
else
{
string sql2 = "SELECT LoginLog.* FROM LoginLog where CustomerId="+custid ;
SqlDataAdapter custlog = new SqlDataAdapter(sql2, con);
DataSet ds = new DataSet();
custlog.Fill(ds, "custlog");
DataTable tblcust;
tblcust = ds.Tables["custlog"];

if(tblcust.Rows.Count< 2)
{
bool boltemp = false;//表示已经有此条数据
foreach( DataRow dr in tblcust.Rows )
{
if (dr["Mac"].ToString().ToUpper().Trim() == c.ToString().ToUpper().Trim())
{
boltemp = true;
return 1;//用户成功登陆

}
}
if (boltemp == false)
{
//llAdp.Insert(CustomerId, strmac, true);//插入MAC地址
string sql3 = "INSERT INTO LoginLog (CustomerId ,Mac) VALUES ("+custid+","+c+")";
sqlexe2(sql3);
return 1;//用户登录成功,并写入MAC
}


}

else if (tblcust.Rows.Count == 2)
{
bool boltemp = false;//表示已经有此条数据
foreach (DataRow dr in tblcust.Rows)
{
if (dr["Mac"].ToString().ToUpper().Trim() == c.ToString().ToUpper().Trim())
{
boltemp = true;
return 1;//用户成功登陆
}
}

if (boltemp)
IsOverMaxLoginCount = false;
else
IsOverMaxLoginCount = true;
}
else
{
IsOverMaxLoginCount = true;

}
if (IsOverMaxLoginCount)
{
return 2;//您已经在2台机器上使用过该用户名登陆。登陆失败!
}
}

}
...全文
588 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhoujinfeng 2009-07-22
  • 打赏
  • 举报
回复
你看看你的判断
IF THEN 里面要有return
在ELSE 里面也要有return
你检查下判断
deyter 2009-07-22
  • 打赏
  • 举报
回复
这么多,同意楼上的说法。
lchy110 2009-07-22
  • 打赏
  • 举报
回复
自己看下 应该是哪个if的时候没返回 建议你还是 在方法开头string 一个变量 然后再中间通过不同的条件给变量赋值 在方法的最后来个总的ruturn 变量 这是个好的习惯
ff1222 2009-07-22
  • 打赏
  • 举报
回复
最后加一句

return 0;
zgke 2009-07-22
  • 打赏
  • 举报
回复
loginlog 方法需要返回值.....
这么多if else 看不下去 最后加个return 0; 把.

110,548

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧