简体   繁体   English

C# MySql Class //连接打开和关闭

[英]C# MySql Class // Connection Open and Close

I got a problem.我有问题。 I stack.我叠。 Don't know if i need a new class for it!不知道我是否需要一个新的 class ! 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:在这里,我调用 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.如果我调用方法 ClConnect() 而不是我必须为参数提供 arguments,但我已经这样做了,所以它不起作用。

Any idea how to do it?知道怎么做吗?

You are storing your dbconnection as a field in your class.您将 dbconnection 作为字段存储在 class 中。 When you want to close it you don't want to assign a new connection object to it with conn = new MySqlConnection();当您想关闭它时,您不想使用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.而只是想删除该行并将其替换为检查 conn 是否为 null 。 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.如果它的 null 那么不需要做任何工作(或者它可能是一个错误),如果它不是 null 那么你可以检查它是否打开并在适当的时候关闭。

You probably also want to be careful of where you are creating new objects in the connect method.您可能还需要注意在 connect 方法中创建新对象的位置。 If conn already exists you probably don't want to (or need to) create a new connection object.如果 conn 已经存在,您可能不想(或不需要)创建新连接 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.但是,我显然不知道你在做什么,所以我不能说这绝对是错误的,只是感觉有点不对。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM