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

时间:2014-02-16 09:17:43

标签: c# mysql asp.net .net

我遇到了与MySQL数据库连接的问题。我有MySQLData.dll。我的连接字符串如下所示:

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

我有OVH服务器。我认为服务器名称是个问题。我收到以下错误:

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

4 个答案:

答案 0 :(得分:2)

  • ProviderName=MySql.Data.MySqlClient添加到您的连接字符串
  • 将参考MySQL.Data添加到您的项目

其他疑难解答,请确保以下内容:

  1. 数据库已启动并接受查询。
  2. 您可以在数据库服务器上本地连接数据库吗?
  3. 您是否在Web服务器和数据库服务器之间有防火墙,或者它们是否位于同一台计算机上?
  4. 以下参考资料的更多帮助: http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL

答案 1 :(得分:0)

您缺少提供商名称。请尝试这个

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

答案 2 :(得分:0)

如果未正确指定连接字符串,则通常会发生此错误。

对于来自SqlServer背景的人,请注意MySql的连接字符串非常不同:

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

对于connectionString=设置:

  • Server=Host.Name.com(或IP) - (相同但没有:Port后缀)
  • Port=nnnn - 默认为3306
  • DataBase=MyDatabase。不是Initial Catalog
  • Uid=User Id=(相同)
  • Pwd=Password=(相同)
  • 如果您需要Unicode支持,请添加Charset=utf8(例如,如果您的Unicode字符存储为????)

答案 3 :(得分:0)

尝试修改machine.config文件以包含当前的连接字符串以及默认的sqlExpress connectionString。还要确保代码中的实际连接字符串不包含端口详细信息。为我工作......