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;
        }这个方法是判断用户是否存在,好像方法不对

解决方案 »

  1.   

    你根本就没执行sql,当然选不出了,你好好看看msdn,看看怎么执行查询
      

  2.   

    使用:SqlCommand对象的ExecuteScalar方法返回第一行。
      

  3.   

    从来没看到你访问过数据库,并执行查询SqlConnection con = new SqlConnection ();//通过类调用连接上数据库            con.Open();//打开连接            SqlCommand com = new SqlCommand();
                com.CommandText = "Select * from Users where UserName='" + tbUserName.Text + "'";
                com.CommandType = CommandType.Text;
                com.Connection = con;
                bool flag = com.ExecuteScalar()==null?false:true;//ture 存在
      

  4.   

    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;
                    }
                }
      

  5.   

    if(b)
    {
        b = true;
    }
    else
    {
       return b;
    }
      

  6.   

    你可以改成string sql = "select count(*) from userinfo where user_id=@user_id";
    当返回结果不是0的时候(为1吧),就说明存在了
      

  7.   

    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的数,不然的话就此用户就不存在
      

  8.   


    public static int GetUserByLoginId(userinfomodel user)
            {
                string sql = "select count(*) from userinfo where user_id=@user_id";
                SqlConnection conn = new SqlConnection(DBHelper.connString);
                conn.Open();
                SqlCommand command = new SqlCommand(sql,conn);
                int result =Convert.ToInt32(command.ExecuteScalar());
                if (result > 0)
                {
                    return -1;
                }
                else 
                {
                    return 1;
                }
            }我改成这样了,但是提示“必须声明标量变量@user_id”
      

  9.   

    这个问题是正常的啊 、你判断是否存在这个ID 、但是你ID是什么都没有告诉人家 、能不报错么?
    cmd.Parameters.AddWithValue("@user_id",5);
    话说、我建议你多看点学习视频之类的好好补习一下吧...
      

  10.   

    这种sql一般用select null from userinfo where user_id=@user_id效率最高