。。 
            string user = this.USERtB1.Text; 
            string pwd = this.PWDtB2.Text; 
            SqlConnection conn = new SqlConnection(@"server=(Local)\sqlexpress;Integrated Security=True;" + "Database=examination");//LOCAL 
            conn.Open(); 
            SqlCommand cmd = new SqlCommand("select count(*) as flag from login where user=" + "'" + user 
                                   + "'" + "and pwd =" + "'" + pwd + "'", conn); 
           int flag = Convert.ToInt32(cmd.ExecuteScalar().ToString());             if (flag > 0)  
            { 
                main mmm = new main();   
                mmm.Show(); 
                this.Visible = false; 
            } 
            else  
            { 
                MessageBox .Show ("出错","出错"); 
            } 
总是直接执行 MessageBox,数据库我已经由ncher改成int的了还是不行,求助大家帮忙。谢谢

解决方案 »

  1.   

     SqlCommand cmd = new SqlCommand("select count(*) as flag from login where user='"+user+"'  
     and pwd ='" + pwd + "'", conn);  
      

  2.   

           
    加上try catch看看抛出的是什么错误
          string user = this.USERtB1.Text; 
                string pwd = this.PWDtB2.Text; 
         try{
                SqlConnection conn = new SqlConnection(@"server=(Local)\sqlexpress;Integrated Security=True;" + "Database=examination");//LOCAL 
                conn.Open(); 
                 SqlCommand cmd = new SqlCommand("select count(*) as flag from login where user='"+user+"'  
     and pwd ='" + pwd + "'", conn); 
               int flag = Convert.ToInt32(cmd.ExecuteScalar().ToString()); 
    }catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

                if (flag > 0)  
                { 
                    main mmm = new main();   
                    mmm.Show(); 
                    this.Visible = false; 
                } 
                else  
                { 
                    MessageBox .Show ("出错","出错"); 
                }  
      

  3.   

    兄弟愚笨,在沒加注释之前提示flag错误,加了注释后程序运行不报错,如下;
      string user = this.USERtB1.Text;  
                     string pwd = this.PWDtB2.Text;  
                try
                { 
                  SqlConnection conn = new SqlConnection(@"server=(Local)\sqlexpress;Integrated Security=True;" + "Database=examination");//LOCAL  
                  conn.Open();  
                  SqlCommand cmd = new SqlCommand("select count(*) as flag from login where user='"+user+"'and pwd ='" + pwd + "'", conn);  
                  int flag = Convert.ToInt32(cmd.ExecuteScalar().ToString());  
                }catch(Exception ex) 
               { 
                 MessageBox.Show(ex.Message); 
               }        /*    if ( flag > 0)   
                {  
                    main mmm = new main();    
                    mmm.Show();  
                    this.Visible = false;  
                }  
                else   
                {  
                    MessageBox .Show ("出错","出错");  
                }  
             */
            }
      

  4.   

     int flag =0;放在Try前面
      

  5.   

    原因很多,调试一下看看。既然没有抛出错误,看来不是数据库登录问题。很大可能是因为sql语句不对或数据库内没有该用户或密码不对。从你的语句看是sql语句有问题。
    "select count(*) as flag from login where user=" + "'" + user  
                                       + "'" + "and pwd =" + "'" + pwd + "'"最终得到的是 Select Count(*) as flag from login where user='u'and pwd='p'
    也就是说 and 之前少了个空格.
      

  6.   

    SqlCommand cmd = new SqlCommand("select count(*) as [flag] from login where user=" + "'" + user 
                                       + "'" + "and pwd =" + "'" + pwd + "'", conn);  flag是关键字,改为[flag]
      

  7.   

    flag=0
    所以不可能运行if语句中的内容,怎么没检索出来东西呢?
      

  8.   

    string user = this.textBox1.Text; 
                string pwd = this.textBox2.Text;
             
                    SqlConnection conn = new SqlConnection(@"Data Source=(local);Initial Catalog=master;Trusted_Connection=Yes;");   
                  conn.Open();   
                  SqlCommand cmd = new SqlCommand("select count(*) as flag from test where uid='"+user+"'and pwd ='" + pwd + "'", conn);   
                  int flag = Convert.ToInt32(cmd.ExecuteScalar().ToString());   
                
               if (flag > 0)
               {
                   Form2 frm = new Form2();
                   frm.Show();
                   this.Visible = false;
               }
               else
               {
                   MessageBox.Show("出错", "出错");
               }   
    可能连接字符串有问题!
      

  9.   

    SqlCommand cmd = new SqlCommand("select count(*) as flag from login where [user]=" + "'" + user   
                                       + "'" + "and pwd =" + "'" + pwd + "'", conn);    
      

  10.   

    SQL语句有问题啊,
    SqlCommand cmd = new SqlCommand("select count(*) as [flag] from login where user='" + user   + "' and pwd ='" + pwd + "'", conn); 
    这样就应该对了
      

  11.   

    建议先将SQL语句在数据库上直接运行一下看没有错的话就是程序问题了.