简体   繁体   English

与数据库连接时显示进度条

[英]showing a progressbar while connection to database

I've created windows application which uses an remote online MYSQL database.For connection i've created a DataConnector() Class. 我创建了使用远程在线MYSQL数据库的Windows应用程序。对于连接,我创建了一个DataConnector()类。 Everytime when i want to connect I create an object of DataConnector() class. 每当我想连接时,我都会创建一个DataConnector()类的对象。

Actually I want to show the progressbar during the connection takes place to database, i mean the progressbar should be on the top of application, after connection successful the progressbar should close automatically. 实际上,我想在数据库连接期间显示进度条,这意味着进度条应位于应用程序的顶部,连接成功后,进度条应自动关闭。

need some idea how to do it...I've tried with "backgroundworker" but facing prob as the function inside the class returns "MySqlConnection" type. 需要一些想法怎么做...我已经尝试过“ backgroundworker”,但由于类中的函数返回“ MySqlConnection”类型而面临问题。

Here is my DataConnector() Class.. 这是我的DataConnector()类。

 namespace omg
{
    class DataConnector
    {
        bool connected = false;
        MySqlConnection connection = null;
        MySqlCommand command;
        string connectionstring = "server=127.0.0.1;database=online_trading_system;UserId=root;Password=phanny";
        public MySqlConnection connect()
        {
            try
            {
                connection = new MySqlConnection(connectionstring);
                connection.Open();
                connected = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return connection;
            }

            MessageBox.Show("connected with the databse");
            // connection.Close();
            //connected = false;
            return connection;
        }
        public void close()
        {
            connection.Close();
            connected = false;
            MessageBox.Show("connection Closed");
        }
    }
}

Your BackgroundWorker approach was correct. 您的BackgroundWorker方法是正确的。 Here's a short sample: 这是一个简短的示例:

private void OpenConnectionButton_Click() {
    var bw = new BackgroundWorker();

    bw.DoWork += (sender, e) => {
        // this will happen in a separate thread
        var connector = new DataConnector();
        connector.connect(); // this might take a while
        e.Result = connector;
    }

    bw.RunWorkerCompleted += (sender, e) => {
        // We are back in the UI thread here.

        // close the progress bar
        ...

        if (e.Error != null)  // if an exception occurred during DoWork,
            MessageBox.Show(e.Error.ToString());  // do your error handling here
        else {
            var connector = (DataConnector)e.Result;
            // do something with your connector
        }
    };

    // show the progress bar
    ...

    bw.RunWorkerAsync(); // start the background worker
}

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

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