如何在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)
{
}
}
}
此行爲應該對關閉SQL連接有*無關。數據庫連接只有在需要時才能在非常小的範圍內打開,使用和關閉。如果你的應用程序打開了連接,那麼這就是資源泄漏,需要修復。無論如何,數據庫連接是否打開都與用戶是否通過身份驗證無關。這聽起來像你的身份驗證機制(你沒有顯示)不起作用。 (另外,有一個靜態的'Connection'對象是一個*着名的壞主意。) – David 2014-09-10 18:22:57
現在沒有認證機制。如果輸入的用戶名和密碼可以訪問mssql表,則會顯示錯誤信息。 – user3888775 2014-09-10 18:42:00
如果沒有認證機制,那麼你期望什麼能夠阻止(錯誤命名的)登錄按鈕打開表單? – David 2014-09-10 18:43:49