C# Connection to MySQL Database

Using visual studio C# you can connect to mysql in three simple steps. Here we go

  1. Before we start writing the code, we need to add the mysql Reference in our project. To do so, we right click our project name, and choose Add Reference:
  2. Then we choose MySql.Data from the list:
  3. Lets confirm it
    //Add MySql Library
    using MySql.Data.MySqlClient;
    class DBConnect
    {
        private MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;
    
        //Constructor
        public DBConnect()
        {
            Initialize();
        }
    
        //Initialize values
        private void Initialize()
        {
            server = "localhost";
            database = "connectcsharptomysql";
            uid = "username";
            password = "password";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" + 
    		database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
    
            connection = new MySqlConnection(connectionString);
        }
    
        //open connection to database
        private bool OpenConnection()
        {
        }
    
        //Close connection
        private bool CloseConnection()
        {
        }
    
        //Insert statement
        public void Insert()
        {
        }
    
        //Update statement
        public void Update()
        {
        }
    
        //Delete statement
        public void Delete()
        {
        }
    
        //Select statement
        public List <string> [] Select()
        {
        }
    
        //Count statement
        public int Count()
        {
        }
    
        //Backup
        public void Backup()
        {
        }
    
        //Restore
        public void Restore()
        {
        }
    }
    

     

    Enjoy!

Leave a Reply

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