2017-07-07 21 views
0

好的,所以,这是我从互联网上下载的一些代码,因为我对SQL很陌生,无意直到遇到我想要添加到我的桌面应用程序中的登录功能。这是代码。(已解决!)使用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; 
} 

}

编辑2:问题已经解决了!点击检测代码丢失了!非常感谢你的帮助。

+0

在您的按钮单击事件中放置一个断点代码,并确保它是否真的被执行或没有。如果没有,那么你可能需要把你的表单设计器代码放在这里。 – Nirman

+0

它没有执行。让我寻找表单设计器代码。 – JamesPlays

+0

添加了表单设计器代码。 – JamesPlays

回答

1

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

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

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

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

我刚才没有显示private代码中无效的部分,它非常的存在。 – JamesPlays

0

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

以下是一些注意事项: 使用预准备语句和参数化查询。永远不要根据用户输入创建动态SQL,因为应用程序易受SQL injection的影响。

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

SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT COUNT(*) FROM login WHERE [email protected] AND [email protected]", connection); 
dataAdapter.SelectCommand.Parameters.AddWithValue("@Username", flatTextBox1.Text); 
dataAdapter.SelectCommand.Parameters.AddWithValue("@Password", flatTextBox2.Text); 
相关问题