Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;“登录”;查询用户表时未成功_C#_Mysql_Login - Fatal编程技术网

C# &引用;“登录”;查询用户表时未成功

C# &引用;“登录”;查询用户表时未成功,c#,mysql,login,C#,Mysql,Login,这是我的密码。我正在尝试创建一个登录页面。代码的问题是“count”值没有增加。 是的,我的表格里有足够的数据。 每次它都转到if条件的else部分,表示登录凭据无效。 我的连接建立在另一个类中,所以这里我只调用对象。 这到底是怎么回事? 请帮忙 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Sy

这是我的密码。我正在尝试创建一个登录页面。代码的问题是“count”值没有增加。 是的,我的表格里有足够的数据。 每次它都转到if条件的else部分,表示登录凭据无效。 我的连接建立在另一个类中,所以这里我只调用对象。 这到底是怎么回事? 请帮忙

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{
public partial class login : Form
{
    public login()
    {
        InitializeComponent();
    }

    public void login_Load(object sender, EventArgs e)
    {

        //to show tip on mouseover on text fields
        System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
        ToolTip1.SetToolTip(this.username_combobox, "username cannot be more that 20 characters          long");
        ToolTip1.SetToolTip(this.password_txt, "Not more than 10 characters");

        //mysql connection
        Connection con = new Connection();
        MySqlConnection conn = con.GetConnection();

        //filling the combo box with data from project.userdetails.username
        string query = "select username from project.user_details;";
        MySqlDataAdapter da = new MySqlDataAdapter(query, conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        username_combobox.DataSource = dt;
        username_combobox.DisplayMember = "username";
        conn.Close();
    }

    private void forgotpassword_label_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        forgot_password fp = new forgot_password();
        fp.Show();
    }

    private void newuser_linklable_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        newuser nu = new newuser();
        nu.Show();

    }

    private void submit_btn_Click(object sender, EventArgs e)
    {
        try
        {
            Connection con = new Connection();
            MySqlConnection conn = con.GetConnection();
            MySqlCommand selectCommand = new MySqlCommand("select * from project.user_details     where username =' " + this.username_combobox.Text + " ' and password = ' " + this.password_txt.Text + " '; ", conn);


            MySqlDataReader myreader;

            myreader = selectCommand.ExecuteReader();
            int counter = 1;

            while (myreader.Read())
            {
                counter = counter + 1;
            }
            if (counter == 2)
            {
                MessageBox.Show("Login successfull");
                Dashboard_form db = new Dashboard_form();
                db.Show(); //open dashboard
                Hide();
            }
            else if (counter > 1)
            {
                MessageBox.Show("Access denied. more than 1 account present");
            }
            else
            {
                MessageBox.Show("Invalid login credentials");
            }
            conn.Close();
        }
        catch(Exception x)
        { MessageBox.Show(x.Message); }
    }

    private void username_combobox_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {


    }
}
}
一个问题是select语句试图匹配由空格包围的用户名,例如
'Jimbox'
'Susanblinkz'
。虽然额外的空白可以简单地删除,但花点时间修复损坏的SQL(以及潜在的注入),并使用占位符,这也会使问题更加明显

MySqlCommand selectCommand = new MySqlCommand(
    @"select * from project.user_details
      where username=@username and password=@password",
    conn);

selectCommand.Parameters
             .Add("@username", SqlDbType.NVarChar)
             .Value = username_combobox.Text;
selectCommand.Parameters
             .Add("@password", SqlDbType.NVarChar)
             .Value = password_txt.Text;
免责声明:我不使用MySqlCommand,因此上述内容可能需要对SQL文本或参数进行一些修改

当然,存储纯文本密码的整个方法都是废话,但是。。很晚了,我要去睡觉了。

更换线路

MySqlCommand selectCommand = new MySqlCommand("select * from project.user_details     where username =' " + this.username_combobox.Text + " ' and password = ' " + this.password_txt.Text + " '; ", conn);

你应该养成调试代码的习惯。 查看strQuery在该点返回的内容,并尝试在sqlserver中执行查询。
我们可以简单地跟踪问题。

在查询文本中连接字符串(非常糟糕的移动),但这样做会在用户名前后添加一个空格。我敢打赌,这个查询不会返回任何东西(密码也是如此),问题是您在用户名和密码的开头和结尾添加了空格。如@user2864740所述。
var strQuery = "select * from project.user_details where username ='" + this.username_combobox.Text + "' and password = '" + this.password_txt.Text + "'; ";
MySqlCommand selectCommand = new MySqlCommand(strQuery, conn);