protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string strsql = "select * from  用户编码表 where 用户编码='000' and 密码='111'";        LoginCheck p = new LoginCheck();
       
        DataTable dt = p.UserLoginCheck(strsql);
        if (dt == null)
        {
                Response.Write("<script>alert('无此用户');</script>");
                return;
        }
        else
            {
                if (dt .Rows.Count == 0)
                {
                    Response.Write("<script>alert('无记录');</script>");
                    return;
                }
                else
                {
                    Session["username"] = dt .Rows[0]["用户名称"];
                    Response.Redirect("frame.aspx");
                }
            }     
    }
  public DataTable UserLoginCheck(string strsql)
    {
        SqlConnection con = DB.Connection();
        SqlCommand cmd = null;
        SqlDataAdapter DA = null;
        DataSet DS = new DataSet();
        DataTable dt = new DataTable();        try
        {
            DA = new SqlDataAdapter(strsql,con);
            DA.Fill(DS);
            dt = DS.Tables[0];           
        }
        catch { }
        finally
        {
            if (con != null)
            {
                con.Close();
                con.Dispose();
            }
            if (cmd != null)
            {
                cmd.Dispose();
            }
        }
        return dt;    }
总是显示"无记录",可数据库里有值.

解决方案 »

  1.   

    把 SQL语句拿到查询分析器中去执行一下,看是否有纪录先。
      

  2.   

    你这样写的话
    查个用户吗
    不要这么复杂把
    command.executescalar()
    就可以了
    返回执行行数
    不要dataset,dataadapter,浪费了
    设断点看看的运行情况把
    另外看看你的sql语句是否可以执行呢,在查询分析器里面
      

  3.   

    单步跟踪就、下,看看返回的时候,dt是否为null
      

  4.   


    try 
            { 
                    con.Open()
                    cmd = new SqlCommand(strsql);
                    command.CommandType = CommandType.Text;
                    DA = new SqlDataAdapter(); 
                    DA.SelectCommand = cmd 
                    DataSet DS = new DataSet("Users");//可以不写
                      DA.Fill(DS); 
                   dt = DS.Tables[0]; 
                   con.Close();        } 
      

  5.   

    不好意思,上面写错个语句,重新发
    try 
            { 
                    con.Open()
                    cmd = new SqlCommand(strsql,con);
                    command.CommandType = CommandType.Text;
                    DA = new SqlDataAdapter(); 
                    DA.SelectCommand = cmd 
                    DataSet DS = new DataSet("Users");//可以不写
                      DA.Fill(DS); 
                   dt = DS.Tables[0]; 
                   con.Close();        } 
      

  6.   

    知道了,数据库没连接上,但是catch()处没输出错误,一直没注意,谢谢各位老大