C# - How To Create Two Function To Open And Close Connection With Database

C# - How To Create Two Function To Open And Close Connection With MySQL Database In C#

___________________________________________________________________________

In This C# Code We Will See How To  Make 2 Function To Open And Close Connection With MySQL Database In CSharp Programming Language .

Project Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
 
namespace Project.classes
{
    class DataBaseClass
    {
        MySqlConnection mysqlcon;
        public DataBaseClass()
        {
            mysqlcon = new MySqlConnection("datasource = localhost; port = 3306; username = root; password =");
        }
        // function to open the connection with database
        public void OpenConnection()
        {
            if (mysqlcon.State == ConnectionState.Closed)
            {
                mysqlcon.Open();
            }
        }
 
        // function to Close the connection with database
        public void CloseConnection()
        {
            if (mysqlcon.State == ConnectionState.Open)
            {
                mysqlcon.Close();
            }
        }
    }
}

Share this

Related Posts

Previous
Next Post »