如何用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();
        }

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

        求高手指点!

转载于:https://www.cnblogs.com/StrongZhang/archive/2012/09/17/2688981.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值