C#中登录功能的实现

具体代码

//登录功能
        private void button1_Click(object sender, EventArgs e)
        {

            if (userID.Text == "" || userPassword.Text == "")
            {
                MessageBox.Show("用户名或者密码为空~");
            }
            else//二者不为空时,判断与数据库中是否相同
            {
                DataBase dataBase = new DataBase();
                SqlConnection sqlConnection = dataBase.getConn();

                sqlConnection.Open();

                //定义sql语句
                string sql = "select count(1) from test1007.guest.its where id='"+userID.Text+"' and password='"+userPassword.Text+"'";

                MessageBox.Show("id:" + userID.Text);
                MessageBox.Show("password:" + userPassword.Text);
                //执行sqlCommand对象
                SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);

                if ((int)sqlCommand.ExecuteScalar() >= 1)
                {
                    //找得到~登录成功~
                    upload up = new upload();
                    this.Hide();
                    up.ShowDialog();
                    Application.ExitThread();
                }
                else//不存在该用户,登录失败~
                {
                    MessageBox.Show("未找到该用户~请仔细检查相关信息");
                }

                sqlConnection.Close();
            }
            
        }
posted @ 2023-11-06 10:57  yesyes1  阅读(43)  评论(0编辑  收藏  举报