How to connect to MySQL Database

0 votes
I'm very new to C# programming but would like to be able to access MySQL Databases (do not have the money for MS SQL)

However I have one question; I know you are required to have "MySQL connector/NET" and "MySQL for Visual Studio" for developing the C# application, but does the person who installs the application also require them? (Is it possible I can just release the connector DLL with the program?)
Oct 12, 2018 in Database by Neha
• 6,300 points
1,138 views

1 answer to this question.

0 votes
using MySql.Data;
using MySql.Data.MySqlClient;

namespace Data
{
    public class DBConnection
    {
        private DBConnection()
        {
        }

        private string databaseName = string.Empty;
        public string DatabaseName
        {
            get { return databaseName; }
            set { databaseName = value; }
        }

        public string Password { get; set; }
        private MySqlConnection connection = null;
        public MySqlConnection Connection
        {
            get { return connection; }
        }

        private static DBConnection _instance = null;
        public static DBConnection Instance()
        {
            if (_instance == null)
                _instance = new DBConnection();
           return _instance;
        }
 public bool IsConnect()
        {
            if (Connection == null)
            {
                if (String.IsNullOrEmpty(databaseName))
                    return false;
                string connstring = string.Format("Server=localhost; database={0}; UID=UserName; password=your password", databaseName);
                connection = new MySqlConnection(connstring);
                connection.Open();
            }

            return true;
        }

        public void Close()
        {
            connection.Close();
        }        
    }
}

Example:

var dbCon = DBConnection.Instance();
dbCon.DatabaseName = "YourDatabase";
if (dbCon.IsConnect())
{
    //suppose col0 and col1 are defined as VARCHAR in the DB
    string query = "SELECT col0,col1 FROM YourTable";
    var cmd = new MySqlCommand(query, dbCon.Connection);
    var reader = cmd.ExecuteReader();
    while(reader.Read())
    {
        string someStringFromColumnZero = reader.GetString(0);
        string someStringFromColumnOne = reader.GetString(1);
        Console.WriteLine(someStringFromColumnZero + "," + someStringFromColumnOne);
    }
    dbCon.Close();
}
answered Oct 12, 2018 by Frankie
• 9,830 points

Related Questions In Database

0 votes
1 answer

How to connect to MySQL Database?

Firstly you must download the MySQLConnection NET ...READ MORE

answered Oct 11, 2019 in Database by Omaiz
• 560 points
830 views
0 votes
2 answers
0 votes
1 answer

How to load data of .csv file in MySQL Database Table?

At first, put the dataset in the ...READ MORE

answered Jul 5, 2019 in Database by Reshma
1,421 views
0 votes
1 answer

How to take MySQL database backup using MySQL Workbench?

Hii, You can export like this: Hope this is ...READ MORE

answered May 4, 2020 in Database by Niroj
• 82,840 points
1,299 views
0 votes
1 answer

Why is not preferable to use mysql_* functions in PHP?

The reasons are as follows: The MySQL extension: Does ...READ MORE

answered Sep 7, 2018 in Database by DataKing99
• 8,250 points
1,268 views
0 votes
1 answer

Find values in a comma separated string in a MySQL query

You can add commas to the left and ...READ MORE

answered Sep 10, 2018 in Database by Sahiti
• 6,370 points
12,897 views
0 votes
1 answer

How to do a batch insert in MySQL

You can try out the following query: INSERT ...READ MORE

answered Sep 10, 2018 in Database by Sahiti
• 6,370 points
2,317 views
0 votes
1 answer
0 votes
1 answer

How to schedule a job for SQL query to run daily?

down voteaccepted Expand the SQL Server Agent node ...READ MORE

answered Oct 25, 2018 in Database by Frankie
• 9,830 points
10,186 views
0 votes
1 answer

How to cache data in a MVC application?

Reference the System.Web dll in your model ...READ MORE

answered Oct 25, 2018 in Database by Frankie
• 9,830 points
1,833 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP