开发者

C# MySql Class // Connection Open and Close

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?


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. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜