Search code examples
c#mysql.data

Using MySql.Data with C# while not making a MySqlConnection


The reccomended way to use MySql.Data is to NOT use a MySqlConnection object; as explained in the documentation. This allows for the MySQl.Data API code to handle connection pooling correctly. See: mySQL documentation

So, for example, this code Selects data with the connection string passed in as a parameter.

The MySqlConnection object is created in the background:

        DataSet dataset = new DataSet();
        MySqlDataAdapter adapter = new MySqlDataAdapter("select * from cfx_jobs", _mySqlConnectionString);
        adapter.Fill(dataset);
        return dataset;

I have looked around and I cannot find an example of how to Insert into the database without explicitly creating a MySqlConnection object.

Which method should I use?


Solution

  • This is how to do it.

    MySqlHelper.ExecuteNonQuery(_mySqlConnectionString, sqlStatement);