0

I got a problem. I stack! Don't know if i need a new class for it! i want a methode for closing the connection via a Button click.

I already created the constructor:

   public string Server;
   public string Username;
   public string Pwd;
   public string DB;


   MySqlConnection conn;
   string ConnString;

   public DBVerb(string eServer, string eUsername, string ePwd, string eDB)
   {
       this.Server = eServer;
       this.Username = eUsername;
       this.Pwd = ePwd;
       this.DB = eDB;

   }

And this two methods:

        public void Connect(System.Windows.Forms.Label lblStatus)
    {
        try
        {                 
            ConnString = String.Format("server={0};user id={1}; password={2}; database={3}; pooling=false",
                                               this.Server, this.Username, this.Pwd, this.DB);
            conn = new MySqlConnection();
            conn.ConnectionString = ConnString;

            if (conn != null)
                conn.Close();


            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                lblStatus.Text = String.Format("Verbindung zu {0} user: {1} Zeit: {2}", this.Server, this.Username, DateTime.Now.ToString());
            }
            else
            {
                MessageBox.Show("Felher");
            }


        }
        catch (Exception Ex)
        {
            MessageBox.Show(Ex.Message, "Fehler:", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }
    public void ClConnect()
    {
        conn = new MySqlConnection();
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
        }
    }

Here I'm calling the Methode:

       private void cmdHerstellen_Click(object sender, EventArgs e)
    {
        string lServer = txtBServ.Text;
        string lUID = txtBUid.Text;
        string lPawd = txtBPass.Text;
        string lDB = txtBDat.Text;


        DBVerb VerbindungHerstellen = new DBVerb(lServer, lUID, lPawd, lDB);
        VerbindungHerstellen.Err();
        VerbindungHerstellen.Connect(lblStatus);



    }

    private void cmdAbbr_Click(object sender, EventArgs e)
    {


    }

If i call the Method ClConnect() than I have to give the arguments for the parameter, but I already did, so it don't work.

Any idea how to do it?

1 Answer 1

1

You are storing your dbconnection as a field in your class. When you want to close it you don't want to assign a new connection object to it with conn = new MySqlConnection(); and instead just want to remove that line and replace it with a check to see if conn is null or not. If its null then no work needs to be done (or maybe its an error) and if its not null then you can check if it is open and close if appropriate.

You probably also want to be careful of where you are creating new objects in the connect method. If conn already exists you probably don't want to (or need to) create a new connection object.

My last comment is that there is something wrong sounding about the user needing to click a button to close your connection. That should be soemthign the code worries about and not the user. However, I obviously don't know what you are doing with this so I can't say it is definitely wrong, just that it feels a bit wrong. :)

Sign up to request clarification or add additional context in comments.

1 Comment

You probably then want to either mark my answer as correct (if it described how you fixed it) or if not then add an answer of your own describing what you needed to do to fix it and then mark the answer as the correct answer (usign the little tick mark next to the answer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.