如何用C#写检测当前用户输入的信息在数据表中是否存在?

    public SqlConnection Connection()    //连接并返回连接对象
    {
     
         SqlConnection conn = new SqlConnection(" Data Source=localhost;Initial Catalog=db_NetBar;User ID=sa;Password=123456");
         return conn;
           
    }
        public SqlCommand Command(string sql, SqlConnection conn)    //返回一个SqlCommand对象
        {

            SqlCommand cmd = new SqlCommand(sql,conn);
            return cmd;

        }

        public bool HasUser(string sql)   //判断输入的用户信息是否存在
        {
            SqlConnection conn = this.Connection();
            conn.Open();
            SqlCommand cmd;
            cmd = this.Command(sql, conn);
           
            if (cmd.ExecuteNonQuery() > 0)
            {
                return true;
            }
            else
            {
                MessageBox.Show("输入的用户名或密码不正确,请从新输入!");
                return false;
            }
            conn.Close();
          
        }

        private void btn_Denglu_Click(object sender, EventArgs e)
        {
           
           
            if ((txtb_UserID.Text == "") || (txtb_UserPwd.Text == ""))
            {
                MessageBox.Show("用户ID和用户密码不能为空");
            }
            else
            {
                string ID = txtb_UserID.Text.ToString();
                string Pwd = txtb_UserPwd.Text.ToString();
               

                if (cbo_select.Text == "超级管理员")
                {
                    string sql = "select * from Super_Administor where Sup_Adm_ID="+ID +" and Sup_Adm_Pwd="+Pwd;
                    if (this.HasUser(sql))
                    {
                        Sup_AdmLogin newFrm1 = new Sup_AdmLogin();
                        newFrm1.Show();
                    }
                }
                if (cbo_select.Text == "管理员")
                {
                    string sql = "select * from Administor where Adm_ID="+ID+" and Adm_Pwd="+Pwd;
                    if (this.HasUser(sql))
                    {
                        AdministorLogin newFrm2 = new AdministorLogin();
                        newFrm2.Show();
                    }
                }
                if ((cbo_select.Text == "普通用户") || (cbo_select.Text == "会员用户") || (cbo_select.Text == "VIP用户"))
                {
                    string sql = "Select * from Users where Users_ID='@Users_ID' and Users_Pwd='@Users_Pwd'";
                    if (this.HasUser(sql))
                    {
                        UserLogin newFrm3 = new UserLogin();
                        newFrm3.Show();
                    }
                }
            }
          
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

上面的代码无法访问数据表中的信息,不知道为什么!

        求高手指点!

posted @ 2012-09-17 16:51  华强国邦  阅读(469)  评论(0编辑  收藏  举报