如何在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