SQL 用于从 table 获取数据时按钮不起作用的问题

Issues with a button not working when SQL is being used to get data from a table

这是我从 Internet 上获得的一些代码,因为我对 SQL 还很陌生,在我遇到这个我想添加到我的桌面应用程序中的登录功能之前,我无意使用它。这是代码。

    SqlConnection connection = new SqlConnection(@"Data Source=JOASH;Initial Catalog=Logins;Integrated Security=True");
        SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT COUNT(*) FROM login WHERE Username='" + flatTextBox1.Text + "' AND Password='" + flatTextBox2 + "'", connection);
        DataTable dataTable = new DataTable();
        dataAdapter.Fill(dataTable);
        if (dataTable.Rows[0][0].ToString() == "1")
        {
            this.Hide();
            Dashboard dashboard_form = new Dashboard();
            dashboard_form.Show();
        }
        else
        {
            MessageBox.Show("Authentication failed!");
        }

那是按钮发生 SQL 事情的部分。基本上,点击一个按钮,代码就会被执行。但是,出于某种原因,当我单击该按钮时,代码未执行或无法正常工作。 (我猜它没有执行。)我真的希望有人能帮我解决这个问题。该按钮将不起作用。

编辑:根据 Nirman 的要求,这是表单设计器代码。

    namespace subTerraApplication
    {
partial class WelcomePage
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.formSkin1 = new FlatUI.FormSkin();
        this.flatTextBox2 = new FlatUI.FlatTextBox();
        this.flatLabel2 = new FlatUI.FlatLabel();
        this.flatTextBox1 = new FlatUI.FlatTextBox();
        this.flatLabel1 = new FlatUI.FlatLabel();
        this.flatMini1 = new FlatUI.FlatMini();
        this.flatMax1 = new FlatUI.FlatMax();
        this.flatClose1 = new FlatUI.FlatClose();
        this.flatButton1 = new FlatUI.FlatButton();
        this.formSkin1.SuspendLayout();
        this.SuspendLayout();
        // 
        // formSkin1
        // 
        this.formSkin1.BackColor = System.Drawing.Color.White;
        this.formSkin1.BaseColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(70)))), ((int)(((byte)(73)))));
        this.formSkin1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(53)))), ((int)(((byte)(58)))), ((int)(((byte)(60)))));
        this.formSkin1.Controls.Add(this.flatButton1);
        this.formSkin1.Controls.Add(this.flatTextBox2);
        this.formSkin1.Controls.Add(this.flatLabel2);
        this.formSkin1.Controls.Add(this.flatTextBox1);
        this.formSkin1.Controls.Add(this.flatLabel1);
        this.formSkin1.Controls.Add(this.flatMini1);
        this.formSkin1.Controls.Add(this.flatMax1);
        this.formSkin1.Controls.Add(this.flatClose1);
        this.formSkin1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.formSkin1.FlatColor = System.Drawing.Color.FromArgb(((int)(((byte)(52)))), ((int)(((byte)(152)))), ((int)(((byte)(219)))));
        this.formSkin1.Font = new System.Drawing.Font("Segoe UI", 12F);
        this.formSkin1.HeaderColor = System.Drawing.Color.FromArgb(((int)(((byte)(45)))), ((int)(((byte)(47)))), ((int)(((byte)(49)))));
        this.formSkin1.HeaderMaximize = false;
        this.formSkin1.Location = new System.Drawing.Point(0, 0);
        this.formSkin1.Name = "formSkin1";
        this.formSkin1.Size = new System.Drawing.Size(256, 349);
        this.formSkin1.TabIndex = 0;
        this.formSkin1.Text = "subTerra Build 1";
        // 
        // flatTextBox2
        // 
        this.flatTextBox2.BackColor = System.Drawing.Color.Transparent;
        this.flatTextBox2.FocusOnHover = false;
        this.flatTextBox2.Location = new System.Drawing.Point(125, 135);
        this.flatTextBox2.MaxLength = 32767;
        this.flatTextBox2.Multiline = false;
        this.flatTextBox2.Name = "flatTextBox2";
        this.flatTextBox2.ReadOnly = false;
        this.flatTextBox2.Size = new System.Drawing.Size(109, 29);
        this.flatTextBox2.TabIndex = 10;
        this.flatTextBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
        this.flatTextBox2.TextColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
        this.flatTextBox2.UseSystemPasswordChar = true;
        // 
        // flatLabel2
        // 
        this.flatLabel2.AutoSize = true;
        this.flatLabel2.BackColor = System.Drawing.Color.Transparent;
        this.flatLabel2.Font = new System.Drawing.Font("Segoe UI", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.flatLabel2.ForeColor = System.Drawing.Color.White;
        this.flatLabel2.Location = new System.Drawing.Point(13, 135);
        this.flatLabel2.Name = "flatLabel2";
        this.flatLabel2.Size = new System.Drawing.Size(99, 30);
        this.flatLabel2.TabIndex = 9;
        this.flatLabel2.Text = "Password";
        // 
        // flatTextBox1
        // 
        this.flatTextBox1.BackColor = System.Drawing.Color.Transparent;
        this.flatTextBox1.FocusOnHover = false;
        this.flatTextBox1.Location = new System.Drawing.Point(125, 81);
        this.flatTextBox1.MaxLength = 32767;
        this.flatTextBox1.Multiline = false;
        this.flatTextBox1.Name = "flatTextBox1";
        this.flatTextBox1.ReadOnly = false;
        this.flatTextBox1.Size = new System.Drawing.Size(109, 29);
        this.flatTextBox1.TabIndex = 8;
        this.flatTextBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
        this.flatTextBox1.TextColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
        this.flatTextBox1.UseSystemPasswordChar = false;
        // 
        // flatLabel1
        // 
        this.flatLabel1.AutoSize = true;
        this.flatLabel1.BackColor = System.Drawing.Color.Transparent;
        this.flatLabel1.Font = new System.Drawing.Font("Segoe UI", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.flatLabel1.ForeColor = System.Drawing.Color.White;
        this.flatLabel1.Location = new System.Drawing.Point(13, 81);
        this.flatLabel1.Name = "flatLabel1";
        this.flatLabel1.Size = new System.Drawing.Size(106, 30);
        this.flatLabel1.TabIndex = 7;
        this.flatLabel1.Text = "Username";
        // 
        // flatMini1
        // 
        this.flatMini1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.flatMini1.BackColor = System.Drawing.Color.White;
        this.flatMini1.BaseColor = System.Drawing.Color.FromArgb(((int)(((byte)(45)))), ((int)(((byte)(47)))), ((int)(((byte)(49)))));
        this.flatMini1.Font = new System.Drawing.Font("Marlett", 12F);
        this.flatMini1.Location = new System.Drawing.Point(189, -1);
        this.flatMini1.Name = "flatMini1";
        this.flatMini1.Size = new System.Drawing.Size(18, 18);
        this.flatMini1.TabIndex = 6;
        this.flatMini1.Text = "flatMini1";
        this.flatMini1.TextColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(243)))), ((int)(((byte)(243)))));
        // 
        // flatMax1
        // 
        this.flatMax1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.flatMax1.BackColor = System.Drawing.Color.White;
        this.flatMax1.BaseColor = System.Drawing.Color.FromArgb(((int)(((byte)(45)))), ((int)(((byte)(47)))), ((int)(((byte)(49)))));
        this.flatMax1.Font = new System.Drawing.Font("Marlett", 12F);
        this.flatMax1.Location = new System.Drawing.Point(213, -1);
        this.flatMax1.Name = "flatMax1";
        this.flatMax1.Size = new System.Drawing.Size(18, 18);
        this.flatMax1.TabIndex = 5;
        this.flatMax1.Text = "flatMax1";
        this.flatMax1.TextColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(243)))), ((int)(((byte)(243)))));
        // 
        // flatClose1
        // 
        this.flatClose1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.flatClose1.BackColor = System.Drawing.Color.White;
        this.flatClose1.BaseColor = System.Drawing.Color.FromArgb(((int)(((byte)(168)))), ((int)(((byte)(35)))), ((int)(((byte)(35)))));
        this.flatClose1.Font = new System.Drawing.Font("Marlett", 10F);
        this.flatClose1.Location = new System.Drawing.Point(237, 0);
        this.flatClose1.Name = "flatClose1";
        this.flatClose1.Size = new System.Drawing.Size(18, 18);
        this.flatClose1.TabIndex = 4;
        this.flatClose1.Text = "flatClose1";
        this.flatClose1.TextColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(243)))), ((int)(((byte)(243)))));
        // 
        // flatButton1
        // 
        this.flatButton1.BackColor = System.Drawing.Color.Transparent;
        this.flatButton1.BaseColor = System.Drawing.Color.FromArgb(((int)(((byte)(52)))), ((int)(((byte)(152)))), ((int)(((byte)(219)))));
        this.flatButton1.Cursor = System.Windows.Forms.Cursors.Hand;
        this.flatButton1.Font = new System.Drawing.Font("Segoe UI", 12F);
        this.flatButton1.Location = new System.Drawing.Point(74, 209);
        this.flatButton1.Name = "flatButton1";
        this.flatButton1.Rounded = false;
        this.flatButton1.Size = new System.Drawing.Size(106, 32);
        this.flatButton1.TabIndex = 11;
        this.flatButton1.Text = "Login";
        this.flatButton1.TextColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(243)))), ((int)(((byte)(243)))));
        // 
        // WelcomePage
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(256, 349);
        this.Controls.Add(this.formSkin1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "WelcomePage";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Form1";
        this.TransparencyKey = System.Drawing.Color.Fuchsia;
        this.formSkin1.ResumeLayout(false);
        this.formSkin1.PerformLayout();
        this.ResumeLayout(false);

    }

    #endregion

    private FlatUI.FormSkin formSkin1;
    private FlatUI.FlatMini flatMini1;
    private FlatUI.FlatMax flatMax1;
    private FlatUI.FlatClose flatClose1;
    private FlatUI.FlatTextBox flatTextBox2;
    private FlatUI.FlatLabel flatLabel2;
    private FlatUI.FlatTextBox flatTextBox1;
    private FlatUI.FlatLabel flatLabel1;
    private FlatUI.FlatButton flatButton1;
}

}


问题已解决!缺少点击检测代码!

您是否尝试在 if/else 块中设置断点?

这里有一些注意事项: 使用准备好的语句和参数化查询。永远不要根据用户输入创建动态 SQL,因为应用程序容易受到 SQL injection.

的攻击

这是您使用参数化参数重写的代码:

SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT COUNT(*) FROM login WHERE Username=@Username AND Password=@Password", connection);
dataAdapter.SelectCommand.Parameters.AddWithValue("@Username", flatTextBox1.Text);
dataAdapter.SelectCommand.Parameters.AddWithValue("@Password", flatTextBox2.Text);

我没有看到任何事件处理程序附加到按钮的设计器代码应该是:

this.flatButton1.Click += new System.EventHandler(this.flatButton1_Click);

您必须定义一个事件处理程序并将代码放入其中

    private void flatButton1_Click(object sender, EventArgs e)
    {
        //Your code here
    }