How To Check Database Connection In MySql C# Visual Studio 2010 Xampp

How To Check Mysql Database Connection C# Visual Studio 2010

How To Check Database Connection In MySql C# Visual Studio 2010 Xampp

class Form1 : Form

        MySQLDatabaseConnection dbCONN = new MySQLDatabaseConnection();
        private void Form1_Load(object sender, EventArgs e)
        {
            // Check the connection when the form loads
            try
            {
                //Open Connection
                dbCONN.getDatabaseConnection().Open();
                //Check If Connection Is Open
                if (dbCONN.getDatabaseConnection().State == ConnectionState.Open)
                {
                    MessageBox.Show("Connected To The Database ............. ");
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("Error " + err.Message);
            }
            finally { 
                //Release Resources 
                dbCONN.getDatabaseConnection().Dispose();
                MessageBox.Show("Resources Released  ............. ");
                //Close Database Connection
                dbCONN.getDatabaseConnection().Close();
                MessageBox.Show("Database Connection Closed ............. ");
            }
        }

MySQLDatabaseConnection Class



    public class MySQLDatabaseConnection
    {
        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=csharpandmysqlcrudinsertupdatedeleteselect");

        //Function to get connection

        public MySqlConnection getDatabaseConnection()
        {
            return connection;
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *