日期:2014-05-17  浏览次数:20524 次

判断用户是否存在
public static userinfomodel GetUserByLoginId(userinfomodel user)
  {
  string sql = "select * from userinfo where user_id=@user_id";
  SqlParameter[] values = 
  {
  new SqlParameter("@user_id",user.User_id)
  };
  List<userinfomodel> list = new List<userinfomodel>();
  if (list != null && list.Count > 0)
  {
  return list[0];
  }
  return null;
  }

这个方法是判断用户是否存在,好像方法不对

------解决方案--------------------
public static bool GetUserByLoginId(userinfomodel user)
{
bool b = false;
using (MySqlConnection con = new MySqlConnection(Connection))
{

MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = @"select * from userinfo where user_id=@user_id";
b= cmd.ExecuteScalar(CommandBehavior.CloseConnection);
if(b)
{
return b;

}else
{
return b;
}
}
------解决方案--------------------
SqlConnection con = new SqlConnection ();//通过类调用连接上数据库
con.Open();//打开连接
try
{
SqlCommand com = new SqlCommand();
com.CommandText = "Select count(*) from Users where UserName='" + tbUserName.Text + "'";
com.CommandType = CommandType.Text;
com.Connection = con;
object flag = com.ExecuteScalar();
if(flag!=null)
{
return Convert.toInt32(flag);
}
else
{
return -1;
}
}
catch()
{
}
finally
{
con.close();
}

你最后可以返回一个整型,要是有用户存在,就会返回>0的数,不然的话就此用户就不存在
------解决方案--------------------
这个问题是正常的啊 、你判断是否存在这个ID 、但是你ID是什么都没有告诉人家 、能不报错么?
cmd.Parameters.AddWithValue("@user_id",5);
话说、我建议你多看点学习视频之类的好好补习一下吧...