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?
it is best to let the connection pooling system manage all connections. Do not create a globally accessible instance of MySqlConnection and then manually open and close it.Methinks the operative phrase is globally accessible. That said, if you create a 'global' DataAdapter and configure it, it will retain the MySqlCommand object to INSERT, DELETE and UPDATE as well as manage the connection.