简体   繁体   English

与C#的MySQL连接

[英]MySQL Connection with C#

I have a code written in JAVA: 我有一个用JAVA编写的代码:

String host = "jdbc:mysql://online/find";
String username = "test";
String password = "test";

And its working fine. 而且工作正常。 But I want to use the same database MySQL with C#. 但我想将同一数据库MySQL与C#一起使用。 And I am doing this: 我正在这样做:

try
{

string myConnStr = "Server=//online/find; " +
                  " Port = 3306; "+
                  " DATABASE=finder; " + 
                  " UID=test;Password=test;";

MySqlConnection MySqlConn = new MySqlConnection(myConnStr);

MySqlDataAdapter MySqlAdapter = new MySqlDataAdapter();

MySqlAdapter.SelectCommand = new MySqlCommand("Select * from finder.Customer", MySqlConn);

MySqlCommandBuilder cb = new MySqlCommandBuilder(MySqlAdapter);

MySqlConn.Open();

DataSet ds = new DataSet();

MessageBox.Show("Connected");

MySqlConn.Close();

}

But I am getting Error: " Unable to connect to any of the specified mysql hosts " 但是我收到错误消息:“ 无法连接到任何指定的mysql主机

I even tried with IP address in connection string but still its not working. 我什至尝试在连接字符串中使用IP地址,但仍然无法正常工作。

I have checked these posts already: 我已经检查了这些帖子:

Unable to connect to any of the specified mysql hosts. 无法连接到任何指定的mysql主机。 C# MySQL C#MySQL

unable to connect to any of the specified mysql hosts. 无法连接到任何指定的mysql主机。 c# C#

According to the documentation should be: 根据文档应为:

string myConnStr = 
"Database=finder;Data Source=//online/find;Port=3306;User Id=test;Password=test";

However for me Connection strings could be hard to remember. 但是对我来说,连接字符串可能很难记住。 Its very easy to make a mistake when you write it manually. 手动编写时很容易出错。 One advice is to use the server explorer to connect to your database. 一种建议是使用服务器资源管理器连接到数据库。 Then right click on your database icon > select properties ... you will see the connection string copy and paste . 然后右键单击数据库图标>选择属性...,您将看到连接字符串的复制和粘贴。 Voilà! 瞧!

Server Explorer: 服务器资源管理器:

在此处输入图片说明

Properties: 属性:

在此处输入图片说明

尝试通过IDE服务器资源管理器->数据连接连接到服务器,并观察生成的连接字符串。

My first idea about the issue is "this has nothing to do with param names for username and password". 关于这个问题,我的第一个想法是“这与用户名和密码的参数名称无关”。 You are not getting an error like: 您没有收到类似以下的错误:

user "null" cant login 用户“空”无法登录

Java and C# work on totally different foundation (Oracle vs. Microsoft) I don't know Java but I think the libraries for connecting to a remote location must be different. Java和C#在完全不同的基础上工作(Oracle与Microsoft)我不知道Java,但我认为用于连接到远程位置的库必须不同。

I think the host URL you used is the problem: 我认为您使用的主机URL是个问题:

Server=//online/find; 服务器= //在线/查找;

I use MySQL with my C# projects and I define the host value as: 我在C#项目中使用MySQL,并将主机值定义为:

  • localhost or 127.0.0.1 本地主机或127.0.0.1
  • an IP address (xxx.xxx.xxx.xxx) IP地址(xxx.xxx.xxx.xxx)
  • a host url (my.dbserver.com) 主机URL(my.dbserver.com)

here is a working MySQL connection string: 这是一个有效的MySQL连接字符串:

<add name="dbConnNews"
connectionString="server=xx.xx.xx.xx;database=yyyyy;User
Id=zzzzzz;Password=**********;"
providerName="MySql.Data.MySqlClient" />

Did you install the MySQl Connector for Microsoft Application. 您是否安装了Microsoft应用程序MySQl连接器。 If yes then add a reference to MySql.dll from your C# application, then use the below connection string 如果是,则从您的C#应用​​程序中添加对MySql.dll的引用,然后使用以下连接字符串

string myConnStr = "server=yourMySqlServerHostorIP; port=MySqlPort;uid=username;pwd=password;initial catalog=dbname"; 字符串myConnStr =“ server = yourMySqlServerHostorIP; port = MySqlPort; uid =用户名; pwd =密码;初始目录= dbname”;

To download mysql connector go to http://dev.mysql.com/downloads/connector/net/ . 要下载mysql连接器,请访问http://dev.mysql.com/downloads/connector/net/

Let me know if it works. 让我知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM