Error connecting running Query in Mysql + C# - Visual Studio

Asked

Viewed 83 times

1

I’m trying to make a connection to the Mysql database through Visual Studio and I’ve already added the database in the Server manager, tested the connections and it all worked out.

The problem is in the part of executing the code.

"ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"

I searched and saw that I had to install the ODBC Mysql driver in order to work. I did and it didn’t work either. Then I searched and saw that I had to add the ODBC connection to the "ODBC Data Sources" part of the Control Panel / Administrative Tools. I added and it hasn’t worked yet.

I believe the error may be in the same code.

Below is what I did:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Odbc;

namespace Banco_Dados
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void BtnConectar_Click(object sender, EventArgs e)
        {

            OdbcConnection con = new OdbcConnection();
            con.ConnectionString = Properties.Settings.Default.CST;

            try
            {
                con.Open();
                OdbcCommand Cmm = new OdbcCommand();
                Cmm.CommandText = "SELECT * FROM cargos";
                Cmm.CommandType = CommandType.Text;
                Cmm.Connection = con;
                OdbcDataReader rd;
                rd = Cmm.ExecuteReader();            

                DataTable dt = new DataTable();
                dt.Load(rd);
                dataGridView1.DataSource = dt;
                dataGridView1.Refresh();
                con.Close();                

                MessageBox.Show("Conexão bem sucedida!");
            }
            catch (OdbcException errConn) {// pode ser usado tambem apenas Exception
                MessageBox.Show(errConn.Message);
                Console.WriteLine(errConn.Message);
            }


        }

    }
}

Where can I be missing?

  • 7

    Use the correct driver that is https://www.nuget.org/packages/MySql.Data/ install via nuget and be happy

  • 3

    I looked for a thousand and one solutions, and this was the only one that managed to solve. Thank you very much!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.