本文介绍了如何使用ADO.NET DbProviderFactory与MySQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何使用ADO.NET DbProviderFactory与MySQL?
How can I use ADO.NET DbProviderFactory with MySQL?
推荐答案
首先,您必须安装 MySQL的净连接器。
MySQL的供应商工厂有固定名称MySql.Data.MySqlClient。下面是一些例子C#code检索所有的表名在本地测试数据库,并坚持他们在一个列表框响应按钮的点击。
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)
{
DbProviderFactory dbf = DbProviderFactories.GetFactory("MySql.Data.MySqlClient");
using (DbConnection dbcn = dbf.CreateConnection())
{
dbcn.ConnectionString = "Server=localhost;Database=test;Uid=test;Pwd=test;";
dbcn.Open();
using (DbCommand dbcmd = dbcn.CreateCommand())
{
dbcmd.CommandType = CommandType.Text;
dbcmd.CommandText = "SHOW TABLES;";
using (DbDataReader dbrdr = dbcmd.ExecuteReader())
{
while (dbrdr.Read())
{
listBox1.Items.Add(dbrdr[0]);
}
}
}
}
}
这篇关于如何使用ADO.NET DbProviderFactory与MySQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!