Home > Net >  Small white consult a logical problem!
Small white consult a logical problem!

Time:11-06




Try
{
String connetStr="server=" + textBox1. Text + ";" +
"Port=" + textBox2. Text + ";" +
"User=" + textBox4. Text + ";" +
"Password=" + textBox5. Text + ";" +
"Database=" + textBox3. Text + "; ";
MySqlConnection conn=new MySqlConnection (connetStr);
Conn. The Open ();
If (ConnectionState. Open==conn. State)
{
Label16. Text="SQL connection status: successful connection";//set the Text property
TextBox1. ReadOnly=true;
TextBox2. ReadOnly=true;
TextBox3. ReadOnly=true;
TextBox4. ReadOnly=true;
TextBox5. ReadOnly=true;
Button4. Enabled=false;
}
The else
{
MessageBox. Show (" database has been shut down ");
}
}
The catch (Exception ex)
{
MessageBox. Show (ex. Message);
}


This is a piece of code I write directly in the connection that the button

My train of thought is
1. The connection after several input box and have become an input connection button and click on the
2. When click the disconnect button will become can be input and the connection button click
Write here I found a problem, I in the connection button directly join conn. Inside the Close (); It will show the conn't definition,
Personal idea is according to the principle that is now on the database connection, in the disconnect button to join conn. Close (); Should go!

Then you don't know how to proceed to the next step! Wish you a great god gives directions!

CodePudding user response:

You are defined in connection in the event the conn, it is a local variable, the scope only connection button events, in the event of an off button is not available, you can defines the conn, outside these two buttons can use this conn, for example:

Private MySqlConnection conn=null;
The button connect _Click ()
{
Conn=new...
Conn. The open ();
.
}

Button to disconnect _Click ()
{
Conn. Close ();
}

CodePudding user response:

MySqlConnection conn written on the outside of the method, as members of the class,


In addition, it is recommended to use WPF, these button state binding of a variable,

CodePudding user response:

reference 1st floor datafansbj response:
you are defined in connection in the event the conn, it is a local variable, the scope only connection button events, in the event of an off button is not available, you can defines the conn, outside these two buttons can use this conn, for example:

Private MySqlConnection conn=null;
The button connect _Click ()
{
Conn=new...
Conn. The open ();
.
}

Button to disconnect _Click ()
{
Conn. Close ();
}



Thank you very much, just play a few words to solve the problems.
  •  Tags:  
  • C#
  • Related