How can I use ADO.NET DbProviderFactory with MySQL?

北慕城南 提交于 2019-11-29 01:13:17

问题


How can I use ADO.NET DbProviderFactory with MySQL?


回答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]);
                }
            }
        }
    }
}


来源:https://stackoverflow.com/questions/1216626/how-can-i-use-ado-net-dbproviderfactory-with-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!