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

发布于 2024-11-24 10:02:40 字数 2219 浏览 0 评论 0原文

我遇到问题了。我叠! 不知道我是否需要新课程! 我想要一种通过单击按钮关闭连接的方法。

我已经创建了构造函数:

   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;

   }

这两个方法:

        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();
        }
    }

这里我调用 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)
    {


    }

如果我调用 Method ClConnect() ,那么我必须提供参数的参数,但我已经这样做了,所以它不起作用。

知道怎么做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菊凝晚露 2024-12-01 10:02:40

您将 dbconnection 作为字段存储在类中。当您想关闭它时,您不想使用 conn = new MySqlConnection(); 为其分配新的连接对象,而只想删除该行并将其替换为检查以查看conn 是否为空。如果它为空,则不需要执行任何工作(或者可能是一个错误),如果它不为空,那么您可以检查它是否打开并关闭(如果适用)。

您可能还需要注意在 connect 方法中创建新对象的位置。如果 conn 已经存在,您可能不想(或不需要)创建新的连接对象。

我的最后一条评论是,用户需要单击按钮来关闭连接,这听起来有些错误。这应该是代码担心的事情,而不是用户担心的事情。但是,我显然不知道你用这个做什么,所以我不能说它绝对是错误的,只是感觉有点错误。 :)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文