用户名密码验证 [英] USername password verification

查看:89
本文介绍了用户名密码验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码.m试图用数据库验证用户名/密码.但是,当我调试它时,它不起作用,接下来是否要进行dr声明(带有粗体的后者),则表明在没有数据存在时无效的读取尝试为什么会这样?任何想法..

below is my code.m trying to verify username/password with database.but,its not working,when i debug it,next if to dr declaration(with bold latters) says that INVALID ATTEMPT TO READ WHEN NO DATA IS PRESENT.why this is so?any idea..

private void button1_Click(object sender, EventArgs e)
        {
            
            if (string.IsNullOrEmpty(this.textBox1.Text) | string.IsNullOrEmpty(this.textBox2.Text))
            {
                MessageBox.Show("Please Enter Username or Password");
                return;
            }
            SqlConnection conn=new SqlConnection("Data Source=PRANAV\\SQLEXPRESS;Initial Catalog=Try;Integrated Security=True");
            
            conn.Open();
            
            SqlCommand cmd = new SqlCommand("SELECT * FROM idpswd where id='"+textBox1.Text+"'and pswd='"+textBox2.Text+"'",conn);



            /*SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);*/
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();

            if(dr.Read())
            {
                SqlConnection con = new SqlConnection("Data Source=PRANAV\\SQLEXPRESS;Initial Catalog=Try;Integrated Security=True");

                con.Open();
                if (this.textBox1.Text == dr["id"].ToString() & this.textBox2.Text == dr["pswd"].ToString())
                {
                    MessageBox.Show("Login Successfull!");

                }
                else
                {
                    MessageBox.Show("Invalid username or password");
                    return;
                }

            }
            
        }

推荐答案

首先,如果您的用户名&密码匹配,如果是,则应该匹配.我猜您提供了正确的连接字符串,以便能够打开连接.在调试时检查打开后的连接,复制cmd文本查询&在SQL Server中执行.如果有数据,那么它也应该在您的应用程序中.可能是您提供了错误的数据,或者数据库中没有与您的用户名&密码.尝试dr [0]& dr [1]代替dr ["id"]& dr ["pswd"].

查看此链接以获取更多信息. [
First of all is there any data in the database provided your username & password matches, if yes it should come. I guess you providing the right connection string so its able to open the connection. while debugging check the connection after open, copy the cmd text query & execute in the SQL server. if the data comes then it should come in your application too. it might be you providing wrong data or there is no data in the database which matches with your username & password. try dr[0] & dr[1] instead of dr["id"] & dr["pswd"].

Check this link for more information.[^]

try this solution. Hope it helps.

private void button1_Click(object sender, EventArgs e)
        {
            
            if (string.IsNullOrEmpty(this.textBox1.Text) | string.IsNullOrEmpty(this.textBox2.Text))
            {
                MessageBox.Show("Please Enter Username or Password");
                return;
            }
            SqlConnection conn=new SqlConnection("Data Source=PRANAV\\SQLEXPRESS;Initial Catalog=Try;Integrated Security=True");
            
            conn.Open();
            
            SqlCommand cmd = new SqlCommand("SELECT * FROM idpswd where id='"+textBox1.Text+"'and pswd='"+textBox2.Text+"'",conn);
 

 
            /*SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);*/
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
 
            if(dr.Read())
            {
                if (this.textBox1.Text == dr["id"].ToString() & this.textBox2.Text == dr["pswd"].ToString())
                {
                    MessageBox.Show("Login Successfull!");
 
                }
                else
                {
                    MessageBox.Show("Invalid username or password");
                    return;
                }
 
            }
            
        }


这篇关于用户名密码验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆