-1

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?

2
  • 1
    if using a DataAdapter, make your changes to the dataset and then call adapter.update(dataset) to update database with any changes Commented Feb 17, 2020 at 17:46
  • 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. Commented Feb 17, 2020 at 18:33

1 Answer 1

0

This is how to do it.

MySqlHelper.ExecuteNonQuery(_mySqlConnectionString, sqlStatement);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.