简体   繁体   English

无法从ASP.NET连接到MySQL数据库

[英]Unable to connect to MySQL database from ASP.NET

I have got a problem with connection with MySQL Database. 我与MySQL数据库的连接有问题。 I got MySQLData.dll. 我得到了MySQLData.dll。 My connection string looks like that: 我的连接字符串如下所示:

String sCON = "Server=mysql51-105.perso; Database=adamlato;  UID=name; Password=pass";

I have got OVH server. 我有OVH服务器。 I think server name is a problem. 我认为服务器名称有问题。 I'm getting the following error: 我收到以下错误:

MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
 ---> System.Net.Sockets.SocketException (0x80004005): Żądana nazwa jest prawidłowa, ale dane żądanego typu nie zostały znalezione
     at System.Net.Dns.GetAddrInfo(String name)
     at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
     at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
     at MySql.Data.Common.MyNetworkStream.GetHostEntry(String hostname)
     at MySql.Data.Common.MyNetworkStream.CreateStream(MySqlConnectionStringBuilder settings, Boolean unix)
     at MySql.Data.Common.StreamCreator.GetTcpStream(MySqlConnectionStringBuilder settings)
     at MySql.Data.Common.StreamCreator.GetStream(MySqlConnectionStringBuilder settings)
     at MySql.Data.MySqlClient.NativeDriver.Open()
     at MySql.Data.MySqlClient.NativeDriver.Open()
     at MySql.Data.MySqlClient.Driver.Open()
     at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
     at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
     at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
     at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
     at MySql.Data.MySqlClient.MySqlPool.GetConnection()
     at MySql.Data.MySqlClient.MySqlConnection.Open()
     at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
     at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
     at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
     at _Default.Page_Load(Object sender, EventArgs e)
 in c:\Users\Adam\Documents\Visual Studio 2013\WebSites\WebSite2\Default.aspx.cs:line 20
  • Add ProviderName=MySql.Data.MySqlClient to your connection string ProviderName=MySql.Data.MySqlClient添加到您的连接字符串
  • Add reference MySQL.Data to your project 将参考MySQL.Data添加到您的项目

Additional troubleshooting, Make sure about the following: 其他疑难解答,请确保有关以下内容:

  1. database started and accepting queries. 数据库已启动并接受查询。
  2. Can you connect to the database locally on the database server? 您可以在数据库服务器上本地连接到数据库吗?
  3. Do you have a firewall between the web server and the database server or are they on the same machine? Web服务器和数据库服务器之间是否有防火墙,或者它们在同一台计算机上?

More Help on the following reference: http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL 有关以下参考的更多帮助: http : //www.codeproject.com/Articles/43438/Connect-C-to-MySQL

You are missing provider name. 您缺少提供者名称。 please try this one 请试试这个

String sCON = "Data Source=mysql51-105.perso; Initial Catalog=adamlato;  User Id=name; Password=pass;";

This error commonly occurs if the connection string isn't specified correctly. 如果未正确指定连接字符串,通常会发生此错误。

For those coming from a SqlServer background, note that MySql 's connection string is very different: 对于那些来自SqlServer背景的用户,请注意MySql的连接字符串非常不同:

  • providerName="MySql.Data.MySqlClient" in place of providerName="System.Data.SqlClient" providerName="MySql.Data.MySqlClient"代替providerName="System.Data.SqlClient"

For the connectionString= settings: 对于connectionString=设置:

  • Server=Host.Name.com (or IP) - (same, but no :Port suffix) Server=Host.Name.com (或IP)-(相同,但没有:Port后缀)
  • Port=nnnn - default to 3306 Port=nnnn默认为3306
  • DataBase=MyDatabase . DataBase=MyDatabase Not Initial Catalog 不是Initial Catalog
  • Uid= or User Id= (same) Uid=User Id= (相同)
  • Pwd= or Password= (same) Pwd=Password= (相同)
  • Add Charset=utf8 if you require Unicode Support (eg if your Unicode chars are stored as ????) 如果需要Unicode支持,请添加Charset=utf8 (例如,如果Unicode字符存储为????)

try modifying your machine.config file to include the present connectionstring alongside the default sqlExpress connectionString. 尝试修改您的machine.config文件,以在默认的sqlExpress connectionString旁边包含当前的连接字符串。 Also make sure your actual connectionstring in your code doesn't contain the port details. 还要确保代码中的实际连接字符串不包含端口详细信息。 Worked for me... 为我工作...

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

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