Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use ADO.NET DbProviderFactory with MySQL?

How can I use ADO.NET DbProviderFactory with MySQL?

like image 212
user366312 Avatar asked Aug 01 '09 13:08

user366312


People also ask

Does ado net support MySQL?

Connector/NET is a fully-managed ADO.NET driver for MySQL. MySQL Connector/NET 8.0 is compatible with all MySQL versions starting with MySQL 5.6. Additionally, MySQL Connector/NET 8.0 supports the new X DevAPI for development with MySQL Server 8.0.

What is Dbproviderfactory C#?

Definition. Represents a set of methods for creating instances of a provider's implementation of the data source classes.


1 Answers

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]);
                }
            }
        }
    }
}
like image 92
Ken Keenan Avatar answered Sep 28 '22 04:09

Ken Keenan