C # use a DSN to connect to MYSQL

0

how is it going? Recently I am learning on my own to program from 0, following tutorials, reading forums, etc.

So far every time I want to perform, for example from a Windows Forms application, some operation to add, read or modify records in a MYSQL database, I am using a connection class specified in the code of the following way:

     class DBConexion
{
    public MySqlConnection conexion = new MySqlConnection("server = localhost; database=base1;port=3306;UID=root;password=root;sslmode=none");

    public void Abrir()
    {
        conexion.Open();
    }

    public void Cerrar()
    {
        conexion.Close();
    }

}

Then for example, if I want to consult the database when clicking a button of a form, then I do:

    private void button1_Click(object sender, EventArgs e)
    {
        DBConexion cc = new DBConexion();
        cc.Abrir();

        string cadena = "select nombre from clientes where numero = 1";

        MySqlCommand comando = new MySqlCommand(cadena,cc.conexion);
        MySqlDataReader registro = comando.ExecuteReader();

        if(registro.Read())
        {
            textBox1.Text = registro["nombre"].ToString();
        }

        cc.Cerrar();
    }

So far, what I've been learning, is the best way I came to perform operations with the database in MYSQL. But it turns out that if I wanted to change the connection data I should modify them directly from the code in the class, which clearly would not be practical.

I work in a company where the system we use for internal management uses an ODBC connection configured from windows to the database, and through an .ini file where you specify the name of the DSN that you have to use that connection , previously configured in windows from Tools > Adminitratives > ODBC Data Origins.

I would like to be able to do this myself, but I have been searching for days and I have not been able to find information that explains all the steps well or that I should use to do it this way. If someone can guide me or pass some link to be able to move forward with this, I would really appreciate it !!

    
asked by Guardia 16.08.2018 в 20:41
source

0 answers