How can I use ADO.NET DbProviderFactory with MySQL?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
First, you have to install the MySQL .Net Connector.
The MySQL Provider factory has the invariant name “MySql.Data.MySqlClient”. Below is some example C# code that retrieves all the table names in the local test database and sticks them in a listbox in response to a button click.
private void button1_Click(object sender, EventArgs e) { var dbf = DbProviderFactories.GetFactory("MySql.Data.MySqlClient"); using (var dbcn = dbf.CreateConnection()) { dbcn.ConnectionString = "Server=localhost;Database=test;Uid=test;Pwd=test;"; dbcn.Open(); using (var dbcmd = dbcn.CreateCommand()) { dbcmd.CommandType = CommandType.Text; dbcmd.CommandText = "SHOW TABLES;"; using (var dbrdr = dbcmd.ExecuteReader()) { while (dbrdr.Read()) { listBox1.Items.Add(dbrdr[0]); } } } } }
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0