2014-09-10 215 views
0

如何在c#窗体中创建一个注销按钮来关闭一个mssql连接。c#窗体窗体注销

我有一个登录表单,可以将用户名和密码从一种表单发送到另一种表单。 Form 2上的连接字符串被放置在公共部分类,看起来像这样:

public static 
SqlConnection con = new SqlConnection(@"Data Source=" + globalvariables.hosttxt + "," + globalvariables.porttxt + "\\SQLEXPRESS;Database=ha;Persist Security Info=false; UID='" + globalvariables.user + "' ; PWD='" + globalvariables.psw + "'"); 
SqlCommand mySqlCmd = con.CreateCommand(); 

我创建的窗口2退出按钮可以追溯到第一次登录形式,但它似乎并没有关闭SqlConnection的。您可以再次按登录按钮,而无需输入用户名和密码,然后再次输入form2。

所以你第一次输入用户名和密码,第二次你不需要。

我使用注销按钮的代码是:

con.Close(); 
this.Close(); 
Form fmlogind = new logind(); 
fmlogind.Show(); 

这里是第二形式对整个代码:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 
using System.IO; 


namespace WindowsFormsApplication1 
{ 


    public partial class Form1 : Form 
    { 


      public static 
      SqlConnection con = new SqlConnection(@"Data Source=" + globalvariables.hosttxt + "," + globalvariables.porttxt + "\\SQLEXPRESS;Database=ha;Persist Security Info=false; UID='" + globalvariables.user + "' ; PWD='" + globalvariables.psw + "'"); 
      SqlCommand mySqlCmd = con.CreateCommand(); 





     public Form1() 
     { 
      InitializeComponent(); 
      this.Text ="EasyAud"; 

     } 


     public void Form1_Load(object sender, EventArgs e) 
     {  

      easyAudToolStripMenuItem.Text = globalvariables.user; 

      comboBox4.Items.Add("1"); 
      comboBox4.Items.Add("2"); 

      mySqlCmd.CommandText = "Select distinct firma from app"; 
      con.Open(); 
      SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

      while (firmaReader.Read()) 
      { 
       comboBox1.Items.Add(firmaReader["firma"]); 
      } 

      firmaReader.Close(); 
      con.Close(); 

      mySqlCmd.CommandText = "Select distinct type from app"; 
      con.Open(); 
      SqlDataReader typeReader = mySqlCmd.ExecuteReader(); 

      while (typeReader.Read()) 
      { 
       comboBox2.Items.Add(typeReader["type"]); 
      } 

      typeReader.Close(); 
      con.Close(); 


     } 



     private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e) 
     { 

       listBox1.Items.Clear(); 
       comboBox3.Items.Clear(); 



      if (comboBox1.Text != "" && comboBox2.Text == "" && comboBox3.Text == "") 
      { 

       string firma = comboBox1.SelectedItem.ToString(); 


       mySqlCmd.CommandText = "Select distinct ha from app where firma = '" + firma + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 


       mySqlCmd.CommandText = "Select distinct farve from app where firma = '" + firma + "'"; 
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 


      } 

      if (comboBox1.Text == "" && comboBox2.Text != "" && comboBox3.Text == "") 
      { 


       string type = comboBox2.SelectedItem.ToString(); 


       mySqlCmd.CommandText = "Select distinct ha from app where type = '" + type + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 

       mySqlCmd.CommandText = "Select distinct farve from app where type = '" + type + "'"; 
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 


      } 

      if (comboBox1.Text == "" && comboBox2.Text == "" && comboBox3.Text != "") 
      { 


       string farve = comboBox3.SelectedItem.ToString(); 

       mySqlCmd.CommandText = "Select distinct ha from app where farve = '" + farve + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 


       mySqlCmd.CommandText = "Select distinct farve from app where farve = '" + farve + "'"; 
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 

      } 

      if (comboBox1.Text != "" && comboBox2.Text != "" && comboBox3.Text != "") 
      { 

       string firma = comboBox1.SelectedItem.ToString(); 
       string type = comboBox2.SelectedItem.ToString(); 
       string farve = comboBox3.SelectedItem.ToString(); 

       mySqlCmd.CommandText = "Select distinct ha from app where firma = '" + firma + "' and type ='" + type + "' and farve ='" + farve + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 


       mySqlCmd.CommandText = "Select distinct farve from app where firma = '" + firma + "' and type ='" + type + "' and farve ='" + farve + "'"; 
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 

      } 

      if (comboBox1.Text != "" && comboBox2.Text != "" && comboBox3.Text == "") 
      { 

       string firma = comboBox1.SelectedItem.ToString(); 
       string type = comboBox2.SelectedItem.ToString(); 


       mySqlCmd.CommandText = "Select distinct ha from app where firma = '" + firma + "' and type ='" + type + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 

       mySqlCmd.CommandText = "Select distinct farve from app where firma = '" + firma + "' and type ='" + type + "'"; 
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 


      } 

      if (comboBox1.Text != "" && comboBox2.Text == "" && comboBox3.Text != "") 
      { 

       string firma = comboBox1.SelectedItem.ToString(); 

       string farve = comboBox3.SelectedItem.ToString(); 

       mySqlCmd.CommandText = "Select distinct ha from app where firma = '" + firma + "' and farve ='" + farve + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 

       mySqlCmd.CommandText = "Select distinct farve from app where firma = '" + firma + "' and farve ='" + farve + "'"; 
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 



      } 


      if (comboBox1.Text == "" && comboBox2.Text != "" && comboBox3.Text != "") 
      { 


       string type = comboBox2.SelectedItem.ToString(); 
       string farve = comboBox3.SelectedItem.ToString(); 

       mySqlCmd.CommandText = "Select distinct ha from app where type = '" + type + "' and farve ='" + farve + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 

       mySqlCmd.CommandText = "Select distinct farve from app where type = '" + type + "' and farve ='" + farve + "'"; 
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 



      } 
     } 

     private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
     { 

      listBox1.Items.Clear(); 
      comboBox3.Items.Clear(); 

      if (comboBox1.Text != "" && comboBox2.Text == "" && comboBox3.Text == "") 
      { 

       string firma = comboBox1.SelectedItem.ToString(); 


       mySqlCmd.CommandText = "Select distinct ha from app where firma = '" + firma + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 


       mySqlCmd.CommandText = "Select distinct farve from app where firma = '" + firma + "'"; 
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 



      } 

      if (comboBox1.Text == "" && comboBox2.Text != "" && comboBox3.Text == "") 
      { 


       string type = comboBox2.SelectedItem.ToString(); 


       mySqlCmd.CommandText = "Select distinct ha from app where type = '" + type + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 


       mySqlCmd.CommandText = "Select distinct farve from app where type = '" + type + "'"; 
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 


      } 

      if (comboBox1.Text == "" && comboBox2.Text == "" && comboBox3.Text != "") 
      { 


       string farve = comboBox3.SelectedItem.ToString(); 

       mySqlCmd.CommandText = "Select distinct ha from app where farve = '" + farve + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 


       mySqlCmd.CommandText = "Select distinct farve from app where farve = '" + farve + "'";    
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 

      } 

      if (comboBox1.Text != "" && comboBox2.Text != "" && comboBox3.Text != "") 
      { 

       string firma = comboBox1.SelectedItem.ToString(); 
       string type = comboBox2.SelectedItem.ToString(); 
       string farve = comboBox3.SelectedItem.ToString(); 

       mySqlCmd.CommandText = "Select distinct ha from app where firma = '" + firma + "' and type ='" + type + "' and farve ='" + farve + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 

       mySqlCmd.CommandText = "Select distinct farve from app where firma = '" + firma + "' and type ='" + type + "' and farve ='" + farve + "'"; 
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 


      } 

      if (comboBox1.Text != "" && comboBox2.Text != "" && comboBox3.Text == "") 
      { 

       string firma = comboBox1.SelectedItem.ToString(); 
       string type = comboBox2.SelectedItem.ToString(); 


       mySqlCmd.CommandText = "Select distinct ha from app where firma = '" + firma + "' and type ='" + type + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 

       mySqlCmd.CommandText = "Select distinct farve from app where firma = '" + firma + "' and type ='" + type + "'"; 
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 


      } 

      if (comboBox1.Text != "" && comboBox2.Text == "" && comboBox3.Text != "") 
      { 

       string firma = comboBox1.SelectedItem.ToString(); 

       string farve = comboBox3.SelectedItem.ToString(); 

       mySqlCmd.CommandText = "Select distinct ha from app where firma = '" + firma + "' and farve ='" + farve + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 


       mySqlCmd.CommandText = "Select distinct farve from app where firma = '" + firma + "' and farve ='" + farve + "'";    
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 


      } 


      if (comboBox1.Text == "" && comboBox2.Text != "" && comboBox3.Text != "") 
      { 


       string type = comboBox2.SelectedItem.ToString(); 
       string farve = comboBox3.SelectedItem.ToString(); 

       mySqlCmd.CommandText = "Select distinct ha from app where type = '" + type + "' and farve ='" + farve + "'"; 
       con.Open(); 
       SqlDataReader firmaReader = mySqlCmd.ExecuteReader(); 

       while (firmaReader.Read()) 
       { 
        listBox1.Items.Add(firmaReader["ha"]); 
       } 

       firmaReader.Close(); 
       con.Close(); 


       mySqlCmd.CommandText = "Select distinct farve from app where type = '" + type + "' and farve ='" + farve + "'"; 
       con.Open(); 
       SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

       while (farveReader.Read()) 
       { 
        comboBox3.Items.Add(farveReader["farve"]); 
       } 

       farveReader.Close(); 
       con.Close(); 

      } 

     } 

     private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) 
     { 

     } 

     private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      comboBox3.Items.Clear(); 
      string ha = listBox1.SelectedItem.ToString(); 

      mySqlCmd.CommandText = "Select distinct farve from app where ha = '" + ha + "'"; 
      con.Open(); 
      SqlDataReader farveReader = mySqlCmd.ExecuteReader(); 

      while (farveReader.Read()) 
      { 
       comboBox3.Items.Add(farveReader["farve"]); 
      } 

      farveReader.Close(); 
      con.Close(); 
     } 

     private void comboBox3_SelectedIndexChanged_1(object sender, EventArgs e) 
     { 

     } 

     private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      listBox1.Items.Clear(); 
      comboBox1.SelectedIndex = -1; 
      comboBox2.SelectedIndex = -1; 
      comboBox3.SelectedIndex = -1; 
      comboBox4.SelectedIndex = -1; 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 

      if (comboBox4.Text == "" || comboBox3.Text == "" || listBox1.Text == "") 
      { 

       MessageBox.Show("HA-Valg blev ikke gemt!" + "\n" + "Felterne: antal, ha og farve skal udfyldes", "Ikke gemt"); 
      } 


      if (comboBox4.Text != "" && listBox1.Text != "") 
      { 
       string ha = listBox1.SelectedItem.ToString(); 
       Clipboard.SetText(comboBox4.Text + "stk " + ha + " i farve " + comboBox3.Text); 
      } 
     } 

     private void logAfToolStripMenuItem_Click(object sender, EventArgs e) 
     { 

      this.Close(); 
      Form fmlogind = new logind(); 
      fmlogind.Show(); 


     } 

     private void easyAudToolStripMenuItem_Click(object sender, EventArgs e) 
     { 

     } 

     private void lukToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 

     private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) 
     { 

     } 









    } 
} 
+1

此行为应该对关闭SQL连接有*无关。数据库连接只有在需要时才能在非常小的范围内打开,使用和关闭。如果你的应用程序打开了连接,那么这就是资源泄漏,需要修复。无论如何,数据库连接是否打开都与用户是否通过身份验证无关。这听起来像你的身份验证机制(你没有显示)不起作用。 (另外,有一个静态的'Connection'对象是一个*着名的坏主意。) – David 2014-09-10 18:22:57

+0

现在没有认证机制。如果输入的用户名和密码可以访问mssql表,则会显示错误信息。 – user3888775 2014-09-10 18:42:00

+0

如果没有认证机制,那么你期望什么能够阻止(错误命名的)登录按钮打开表单? – David 2014-09-10 18:43:49

回答

1

这行代码执行一次只有一次不管你打开表格的次数多少次:

public static 
     SqlConnection con = new SqlConnection(@"Data Source=" + globalvariables.hosttxt + "," + globalvariables.porttxt + "\\SQLEXPRESS;Database=ha;Persist Security Info=false; UID='" + globalvariables.user + "' ; PWD='" + globalvariables.psw + "'"); 

除了使用全局变量(坏主意)和使用单个静态数据库连接(坏主意)之外,一旦创建了连接,第一次将总是作为该类型的static属性存在。

从这一点开始,您可以输入您喜欢的所有用户名和密码值。您可能正在更新与它们相关联的全局变量,但您是而不是更新con变量。该表达式的右侧不会将参考复制到那些要多次评估的全局变量,它只会评估它们一次并保留该评估的结果。

所以,你在这种情况下,事件的顺序是:

  1. 创建使用所提供的用户名和密码的连接。
  2. 打开连接。
  3. 使用窗体,连接等
  4. 关闭连接。
  5. 更改用户名和密码变量,但不更改连接。
  6. 重新打开原来的连接,使用原来的连接字符串。

解决此问题的最佳方法是从static范围中删除SqlConnection对象。如果您想在表单加载时尝试启动数据库连接,请在构造函数或事件处理函数中执行此操作。而不是在整个应用程序中重复使用相同的永远打开的连接,任何时候您需要访问数据库时,都应该创建一个本地连接到该范围的连接,并在完成后立即处理它。

+0

我刚才从所有的if语句中的公共静态移动sqlconnection,它的工作原理,谢谢! – user3888775 2014-09-10 19:00:16

+0

为什么不是全局变量这样一个好主意? – user3888775 2014-09-10 19:02:32

+0

@ user3888775:多年来,我已经多次详细解释了这个问题,并且我可以提供更多的细节和口才。例如:http://programmers.stackexchange.com/questions/148108/why-is-global-state-so-evil – David 2014-09-10 19:06:41