使用 Visual Studio C# 连接 MySQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6419997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting MySQL with Visual Studio C#
提问by sean
I'm new to MySQL Workbench and I'm trying to make a Timekeeping system. I'm wondering how to connect MySQL with Visual Studio C#?
我是 MySQL Workbench 的新手,我正在尝试制作一个计时系统。我想知道如何将 MySQL 与 Visual Studio C# 连接起来?
回答by Jaime
you'll need a "connector/driver" to connect from .net to mysql, you can find the official .net connector from mysql here:
您需要一个“连接器/驱动程序”来从 .net 连接到 mysql,您可以在此处找到来自 mysql 的官方 .net 连接器:
http://dev.mysql.com/downloads/connector/net/
http://dev.mysql.com/downloads/connector/net/
the connector will install the MySql.Data library that you has classes to communicate to MySql (MySqlConnection, MySqlCommand, MySqlDataAdapter, etc)
连接器将安装 MySql.Data 库,您可以使用该库与 MySql 进行通信(MySqlConnection、MySqlCommand、MySqlDataAdapter 等)
回答by naveen
If you are working with MySQL for the first time in your PC, do these things.
如果您是第一次在 PC 上使用 MySQL,请执行以下操作。
- Install MySQL Server (Link here) - 28 MB
- Install MySQL ODBC Connector (Link here)- 3 MB
Now install SqlYog Community Edition. ( Link here). You can manipulate your MySQL Databases using this.
现在安装 SqlYog 社区版。(链接在这里)。您可以使用它来操作您的 MySQL 数据库。
Now in AppSettings of web.config, set two entries like this.
现在在 web.config 的 AppSettings 中,像这样设置两个条目。
<configuration>
<appSettings>
<add key="ODBCDriver" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;"/>
<add key="DataBaseDetails" value="Database=mydatabase;uid=root;pwd=;Option=3;"/>
</appSettings>
</configuration>
And call it like in your MySQL class like this.
并像这样在您的 MySQL 类中调用它。
public string MyConnectionString
{
get
{
//return {MySQL ODBC 5.1 Driver};Server=localhost;Database=mydatabase;uid=root;pwd=;Option=3;
return ConfigurationManager.AppSettings["ODBCDriver"]
+ ConfigurationManager.AppSettings["DataBaseDetails"];
}
}
Now you can initialize your connection like this.
现在您可以像这样初始化您的连接。
OdbcConnection connection = new OdbcConnection(MyConnectionString);
Namespace imported
导入的命名空间
using System.Data.Odbc;
Hope you get the idea.
希望你明白。
回答by Johnny Oshika
The easiest way is to use NuGet to get the .Net connector for MySQL:
最简单的方法是使用 NuGet 获取 MySQL 的 .Net 连接器:
Once you install the MySql.Data package, you can do something like this:
安装 MySql.Data 包后,您可以执行以下操作:
using (var connection = new MySqlConnection("Server=localhost;Database=MyDatabaseName;Uid=root;Pwd=;"))
using (var command = connection.CreateCommand()) {
connection.Open();
command.CommandText = "select id, name from widgets";
using (var reader = command.ExecuteReader())
while (reader.Read())
Console.WriteLine(reader.GetString(0) + ": " + reader.GetString(1));
}
回答by cabanaboy
Try this website:
试试这个网站:
http://www.connectionstrings.com/mysql#p34
http://www.connectionstrings.com/mysql#p34
Set up your connection string and then the rest should just work as if you were calling a SQLServer database.
设置您的连接字符串,然后其余部分应该像调用 SQLServer 数据库一样工作。
Good luck.
祝你好运。
回答by Devart
You can connect to MySQL using dotConnect for MySQL.
您可以使用 dotConnect for MySQL 连接到 MySQL。