使用 Visual C# 连接到远程 MySQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/402355/
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
Connect to remote MySQL database with Visual C#
提问by Elie
I am trying to connect to a remote MySQL database using Visual C# 2008 Express Edition. Is there a way to connect using the editor, or do I have to code the connection manually? The editor has a clear and easy-to-follow wizard for connecting to Microsoft SQL Server and Access databases, but I don't see an easy way to add a remote MySQL datasource. I tried searching the help, but couldn't find anything useful.
我正在尝试使用 Visual C# 2008 Express Edition 连接到远程 MySQL 数据库。有没有办法使用编辑器进行连接,还是必须手动编码连接?编辑器有一个清晰易懂的向导,用于连接 Microsoft SQL Server 和 Access 数据库,但我没有看到添加远程 MySQL 数据源的简单方法。我尝试搜索帮助,但找不到任何有用的信息。
Has anyone done this using the editor? Or can point me in a useful direction?
有没有人用编辑器做过这个?或者可以为我指出一个有用的方向?
采纳答案by Rishi Agarwal
You will have to code the connection manually to connect to a remote MySQL database using Visual C# 2008 Express Edition.
您必须手动编写连接代码才能使用 Visual C# 2008 Express Edition 连接到远程 MySQL 数据库。
VS 2008 Express (and VS 2005 Express too) doesn't allow you to use MySQL .Net Provider through the Data Source Dialog. The non-Express edition allow you to do the same.
VS 2008 Express(以及 VS 2005 Express)不允许您通过数据源对话框使用 MySQL .Net Provider。非 Express 版本允许您执行相同的操作。
To use MySQL in VS Express, you will have to include a reference to the MySQL DLLs. If you have installed the MySQL .Net Provider, the DLLs will be in C:\Program Files\MySQL\MySQL Connector Net x.x.x). Or copy the DLLs to the Bin folder of your project. After including the DLLs, you can make a ConnectionString to connect to the remote MySQL Database.
要在 VS Express 中使用 MySQL,您必须包含对 MySQL DLL 的引用。如果您安装了 MySQL .Net Provider,DLL 将位于 C:\Program Files\MySQL\MySQL Connector Net xxx)。或者将 DLL 复制到项目的 Bin 文件夹中。包含 DLL 后,您可以创建一个 ConnectionString 以连接到远程 MySQL 数据库。
The MySQL .Net Provider can be found here
MySQL .Net Provider 可以在这里找到
A similar question was asked in thread 396593 here
回答by Marc Gravell
The good thing about "express" (or even just "csc") is that even if it doesn't have a designer to help with some things (like setting up the connection string to most useful databases), the core framework is not limited. So you'll probably have to put in the connection string yourself and add a reference to the MySQL/.NET provider, but it should work at runtime, even in debug.
“express”(甚至只是“csc”)的好处在于,即使它没有设计人员帮助处理某些事情(例如设置到最有用的数据库的连接字符串),核心框架也不受限制. 因此,您可能必须自己放入连接字符串并添加对 MySQL/.NET 提供程序的引用,但它应该在运行时工作,即使在调试中也是如此。
Which is very nice ;-p
这是非常好的;-p
回答by Gant
EDIT:I didn't check Rishi Agarwal's answer before posting. I think his answer has more insight on the express edition
编辑:我在发布之前没有检查 Rishi Agarwal 的回答。我觉得他的回答对快递版有更深入的了解
I am not sure about this and express edition, but you should try MySQL Connector/Net. It works fine with my VS2008 Pro.
我不确定这个和 express 版本,但你应该尝试MySQL Connector/Net。它适用于我的 VS2008 Pro。
回答by Gant
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome ...!");
String conString = "SERVER = localhost; DATABASE = l2emu; User ID = root; PASSWORD = password;";
MySqlConnection connection = new MySqlConnection(conString);
String command = "SELECT * FROM characters";
MySqlCommand cmd = new MySqlCommand(command,connection);
MySqlDataReader reader;
try
{
connection.Open();
cmd.ExecuteNonQuery();
reader = cmd.ExecuteReader();
cmd.CommandType = System.Data.CommandType.Text;
while (reader.Read() != false)
{
Console.WriteLine(reader["char_name"]);
Console.WriteLine(reader["level"]);
}
Console.ReadLine();
}
catch (MySqlException MySqlError)
{
Console.WriteLine(MySqlError.Message);
}
}
}
}
here is the example but you must download the mysql connector,
这是示例,但您必须下载mysql 连接器,
回答by kosnkov
now u can use entity to mysql http://www.codeproject.com/Tips/426790/Using-MySQL-with-Entity-Framework
现在你可以使用实体到 mysql http://www.codeproject.com/Tips/426790/Using-MySQL-with-Entity-Framework