1
Reply

ODBC - MySQL Connection error in c#

shahul mechery

shahul mechery

Oct 9 2013 2:24 AM
1.2k
I tried to access MySql Database using ODBC in C#
But i got an error as:

ERROR [HY000] [MySQL][ODBC 5.1 Driver]Can't connect to MySQL server on 'localhost' (10061)

Whats wrong here? What i have to recheck in my control panel settings?

Code is as follows:

 private void Form2_Load(object sender, EventArgs e)
        {
            gridfill();
        }
        string MyConnectionString = @"server=localhost;userid=admin;password=admin;database=blood"; 
        private void gridfill()
        {
            MySqlConnection con = null;
            try
            {
                con = new MySqlConnection(MyConnectionString);
                con.Open();

                MySqlCommand cmd = con.CreateCommand();
                //cmd.CommandText = "SELECT * FROM blood";
                MySqlDataAdapter da = new MySqlDataAdapter();
                da.SelectCommand = new MySqlCommand("SELECT * FROM blood", con);
                MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
                DataSet ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (MySqlException err) 
            {
                MessageBox.Show("Error: " + err.ToString());
            }
            
           
            finally 
            {
                con.Close();
            }
        }

Answers (1)