上次的谢谢各位!  是代码错了。
private void button1_Click(object sender, EventArgs e)
        {            string name = textBox1.Text;
            string pwd = textBox2.Text;
      
            string name1= "select count(*) from admine where admin="+textBox1 .Text ;
            string pwd1 = "select count(*) from admine where password="+textBox2 .Text ;
            if (name == "" && pwd == "")
            {
                MessageBox.Show("用户名和密码不能为空!");
            }
            else if(name .Equals (name1 ) && pwd.Equals (pwd1))  
            { 
                Form AdminForm = new Admin();
                AdminForm.Show();
                
            }
            else
            {
             MessageBox.Show("用户名或密码错误!请重新输入!");
                textBox1.Text = "";
                textBox2.Text = "";
            }
        }
大家帮忙看看这段代码,只要输入用户名和密码直接执行
else
            {
             MessageBox.Show("用户名或密码错误!请重新输入!");
                textBox1.Text = "";
                textBox2.Text = "";
            }
这段代码,帮忙看看,先谢谢大家了!

解决方案 »

  1.   

    不要使用MessageBox.不友好.
    如果需要實現用戶一離開TextBox就觸發事件,就在TextChanged或者LostFocus事件里面做
      

  2.   


    string name1= "select count(*) from admine where admin="+textBox1 .Text ; 
    string pwd1 = "select count(*) from admine where password="+textBox2 .Text ;   else if(name .Equals (name1 ) && pwd.Equals (pwd1))  
     { 
             Form AdminForm = new Admin(); 
             AdminForm.Show();      
     } 用户名和一个sql语句进行比较??
    即便是你做了查询,
    用户名和用户名的数量比较,密码和密码的数量比较。
    有可比性吗?
    这样能判断用户能否登录吗?
    每次都是判断失败当然直接执行else了。
      

  3.   

    sql语句得出的是什么..?数量...???
      

  4.   


    你用户名和密码在数据库中是字符串类型的吗?
    string name1= "select count(*) from admine where admin=‘"+textBox1 .Text +“’”; 
    string pwd1 = "select count(*) from admine where password='"+textBox2 .Text+"'" ; 
      

  5.   

    我对这段代码无语了,SQL语句都还没有执行得到返回结果,就直接赋值比较了,而且你要比较的是SQL语句还是用户名和密码,只要把以上的问题理清了你说的问题就不是问题了
    只要输入用户名和密码直接执行 
    else 
                { 
                MessageBox.Show("用户名或密码错误!请重新输入!"); 
                    textBox1.Text = ""; 
                    textBox2.Text = ""; 
                } 

      

  6.   

    嗯嗯 就是这个问题
    string name1= "select count(*) from admine where admin="+textBox1 .Text ; name .Equals (name1 ) 都没执行查询 name1值还是 "select count(*) from admine where admin="+textBox1 .Text 
      

  7.   

    sql语句没执行,你这是直接拿
    string name1= "select count(*) from admine where admin=‘"+textBox1 .Text +“’”; 
    string pwd1 = "select count(*) from admine where password='"+textBox2 .Text+"'" ; 
    这两条sql字符串和textbox中的内容比较
    结果肯定是
    else 
                { 
                MessageBox.Show("用户名或密码错误!请重新输入!"); 
                    textBox1.Text = ""; 
                    textBox2.Text = ""; 
                } 
      

  8.   

    你想干嘛?
    还不如第一次登陆后,记住Cookie呢。下次UName一出现自个儿跟上密码!
      

  9.   

    大哥,你得执行你的SQL语句呀。看执行结果判断是否登录成功。。怎么能直接拿用户名密码跟SQL语句相比较??
      

  10.   

    这代码...真无语....不知道比较了个啥if (name == "" && pwd == "") 
                { 
                    MessageBox.Show("用户名和密码不能为空!"); 
                }
     
    首先第一个判断就有问题,应该是
    if (name == "" || pwd == "") 
    吧...随便一个空都不能过哇
      

  11.   

    using(SqlConnection conn=new SqlConnection("") )
    {
    string strSql="select * from Tb where admin='"+textBox1.Text+"'";
    SqlCommand cmd=new SqlCommand(strSql,conn) ;
    conn.Open();
    SqlDataReader reader=cmd.ExecuteReader();
    if(reader.Read())
    {
                                 if(dr["password"].ToString().Equals(""))   
                                {}
    }
    conn.Close();
    }
      

  12.   

    name和name1比较...根据name查询数据库是否存在与之匹配的name,存在返回true,不存在则返回false
    //连接数据库略 private void button1_Click(object sender, EventArgs e)
            {//登录
                loginTime++;            string userName = this.textBox1.Text.Trim();
                string passWord = this.textBox2.Text.Trim();
                StringBuilder sql = new StringBuilder();
                sql.Append("select count(*) from [User] where Username='"+userName+"' and Password='"+passWord+"'");
                bool checkUser=DBUtility.DbHelperOleDb.CheckUser(sql.ToString());
                if (checkUser == true)
                {//检验合法用户成功,进入主界面
                    Close();
                    //MessageBox.Show(this, "存在该用户", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    if (loginTime == 3)
                    {
                        MessageBox.Show("登陆失败,超过三次,退出");
                        Application.Exit();
                    }
                    else
                    {
                        MessageBox.Show("登陆失败");
                        this.textBox1.Clear();
                        this.textBox2.Clear();
                    }
                }
            }
    public static bool CheckUser(String sql)
            {//查询用户是否存在
                Object obj = GetScalar(sql.ToString());
                int cmdResult;
                if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                {
                    cmdResult = 0;
                }
                else
                {
                    cmdResult = int.Parse(obj.ToString());
                }
                if (cmdResult == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            public static Object GetScalar(String sql)
            {//执行查询语句,返回查询结果(object)
                if (connection != null)
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        using (OleDbCommand cmd = new OleDbCommand(sql, connection))
                        {
                            try
                            {                            object obj = cmd.ExecuteScalar();
            
                                if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                                {
                                    return null;
                                }
                                else
                                {
                                    return obj;
                                }
                            }
                            catch (System.Data.OleDb.OleDbException e)
                            {
                                connection.Close();
                                throw new Exception(e.Message);
                            }
                        }
                    }
                }
                throw new Exception("未连接数据库");
            }
      

  13.   

    这里的代码有问题哦
    string name = textBox1.Text; 
    string pwd = textBox2.Text; 
          
    string name1= "select count(*) from admine where admin="+textBox1 .Text ; 
    string pwd1 = "select count(*) from admine where password="+textBox2 .Text ;string name1= "select count(*) from admine where admin="+textBox1 .Text ; 
    string pwd1 = "select count(*) from admine where password="+textBox2 .Text ;
    上述两条SQL语句赋值的结果是为空还是不为空
    这样赋值,name和name1肯定不相等的嘛
      

  14.   

    你的代码有多个问题啊!
    1.sql代码没有使用数据控件去查询(使用SqlCommand.ExecuteScalar)
    2.两个sql换成  where 中用 and 连接的两个条件的一个sql
    3.使用参数或把用户名和密码 ,替换一个单引号'为两个单引号''
    http://www.mybuffet.cn
      

  15.   

    private void button1_Click(object sender, EventArgs e) 
            {             string name = textBox1.Text; 
                string pwd = textBox2.Text; 
          
                int name1= "select count(*) from admine where admin= '"+name +"'" and password = '" +pwd  ; 
                 
                if (name1>0 && name1!=null) 
                { 
                  Form AdminForm = new Admin(); 
                    AdminForm.Show();                 
                } 
                else if(name  = ""||
                    pwd  = "")  
                { 
                  MessageBox.Show("用户名和密码不能为空!"); 
                    
                } 
                else 
                { 
                MessageBox.Show("用户名或密码错误!请重新输入!"); 
                    textBox1.Text = ""; 
                    textBox2.Text = ""; 
                } 
    }
    这样试试
      

  16.   

    private void button1_Click(object sender, EventArgs e) 
            {             string name = textBox1.Text; 
                string pwd = textBox2.Text; 
          
              int name1= "select count(*) from admine where admin= '"+name +"'" and password = '" +pwd  ; 
                 
                if (name1>0 && name1!=null) 
                { 
                  Form AdminForm = new Admin(); 
                    AdminForm.Show();                 
                } 
                else if(name  == ""||pwd  == "")  
                { 
                  MessageBox.Show("用户名和密码不能为空!"); 
                    
                } 
                else 
                { 
                MessageBox.Show("用户名或密码错误!请重新输入!"); 
                    textBox1.Text = ""; 
                    textBox2.Text = ""; 
                } 
    }
    试试这个怎么样?
      

  17.   

    你这语句怎么执行?
    string name1= "select count(*) from admine where admin="+textBox1 .Text ; 
    string pwd1 = "select count(*) from admine where password="+textBox2 .Text ; 你这比较又是什么就算你上面执行了  你返回的结果是字符串行的数字(count(*));
    你用返回的行数和你输入的用户名比?
              else if(name .Equals (name1 ) && pwd.Equals (pwd1))  
      
      

  18.   

    建议楼主先看下SQL语句和ADO.net相关的东西!不能为解决问题而解决问题。!仅个人建议!
      

  19.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void btnLogin_Click(object sender, EventArgs e)
            {
                string str = "server=liu;database=myTest;uid=sa;pwd=123456";
                SqlConnection sqlcon = new SqlConnection(str);            string uname=this.txtName.Text.Trim();//用户名
                string pwd=this.txtPwd.Text.Trim();//密码
                string sql=string.Format("select count(*) from login where name='{0}' and pwd='{1}'",
                    uname,pwd);            SqlCommand sqlcmd = new SqlCommand();
                sqlcmd.CommandText = sql;
                sqlcmd.CommandType = CommandType.Text;
                sqlcmd.Connection = sqlcon;            try
                {
                    sqlcon.Open();
                    object obj= sqlcmd.ExecuteScalar();
                    if (Convert.ToInt32(obj) > 0)
                    {
                        MessageBox.Show("恭喜,登录成功!");
                    }
                    else
                    {
                        MessageBox.Show("失败");
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    sqlcon.Close();
                }
            }
        }
    }
    自己改下数据库连接就行了