2014-09-10 205 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