无法连接到任何指定的mysql主机。C#MySQL

无法连接到任何指定的mysql主机。C#MySQL,c#,mysql,visual-studio-2010,sharpdevelop,C#,Mysql,Visual Studio 2010,Sharpdevelop,当我执行代码时,我得到了上面的错误- MySqlConnection mysqlConn=new MySqlConnection("server=127.0.0.1;uid=pankaj;port=3306;pwd=master;database=patholabs;"); mysqlConn.Open(); 我尝试将服务器设置为localhost,将用户设置为root,但出现以下错误- Error: 0 : Unable to connect to any of the sp

当我执行代码时,我得到了上面的错误-

MySqlConnection mysqlConn=new MySqlConnection("server=127.0.0.1;uid=pankaj;port=3306;pwd=master;database=patholabs;");
        mysqlConn.Open();
我尝试将服务器设置为localhost,将用户设置为root,但出现以下错误-

Error: 0 : Unable to connect to any of the specified MySQL hosts.
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>DBSync.exe</AppDomain><Exception><ExceptionType>MySql.Data.MySqlClient.MySqlException, MySql.Data, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d</ExceptionType><Message>Unable to connect to any of the specified MySQL hosts.</Message><StackTrace>
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 DBSync.MainForm.BtnCalculateClick(Object sender, EventArgs e) in c:\Documents  and Settings\Test01\My Documents\SharpDevelop Projects\DBSync\DBSync\MainForm.cs:line 51
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message&amp;amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message&amp;amp; m)
at System.Windows.Forms.ButtonBase.WndProc(Message&amp;amp; m)
at System.Windows.Forms.Button.WndProc(Message&amp;amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp;amp; msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at DBSync.Program.Main(String[] args) in c:\Documents and Settings\Test01\My Documents\SharpDevelop Projects\DBSync\DBSync\Program.cs:line 27</StackTrace><ExceptionString>MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
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 DBSync.MainForm.BtnCalculateClick(Object sender, EventArgs e) in c:\Documents and Settings\Test01\My Documents\SharpDevelop Projects\DBSync\DBSync\MainForm.cs:line 51
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message&amp;amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message&amp;amp; m)
at System.Windows.Forms.ButtonBase.WndProc(Message&amp;amp; m)
at System.Windows.Forms.Button.WndProc(Message&amp;amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp;amp; msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at DBSync.Program.Main(String[] args) in c:\Documents and Settings\Test01\My Documents\SharpDevelop Projects\DBSync\DBSync\Program.cs:line 27</ExceptionString><DataItems><Data><Key>Server Error Code</Key><Value>1042</Value></Data></DataItems></Exception></TraceRecord>
错误:0:无法连接到任何指定的MySQL主机。
系统。关键事务:0:http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/UnhandledUnhandled exceptionDBSync.exeMySql.Data.MySqlClient.MySqlException,MySql.Data,版本=6.7.4.0,区域性=neutral,PublicKeyToken=C5687FC88969C44无法连接到任何指定的MySql主机。
在MySql.Data.MySqlClient.NativeDriver.Open()上
在MySql.Data.MySqlClient.Driver.Open()上
位于MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder设置)
在MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()上
在MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()上
在MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()上
在MySql.Data.MySqlClient.MySqlPool.GetConnection()上
在MySql.Data.MySqlClient.MySqlConnection.Open()上
在DBSync.MainForm.btncalculate中,单击c:\Documents and Settings\Test01\My Documents\SharpDevelop Projects\DBSync\DBSync\MainForm.cs中的(对象发送方,事件参数e):第51行
在System.Windows.Forms.Control.OnClick(EventArgs e)中
在System.Windows.Forms.Button.OnClick(EventArgs e)中
在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs-mevent)上
在System.Windows.Forms.Control.WmMouseUp(消息和m、鼠标按钮、Int32单击)
在System.Windows.Forms.Control.WndProc(Message&;m)中
在System.Windows.Forms.ButtonBase.WndProc(Message&;m)
在System.Windows.Forms.Button.WndProc(Message&;m)中
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&;m)中
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&;m)中
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)
在System.Windows.Forms.UnsafentiveMethods.DispatchMessageW(MSG&;MSG)
位于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID、Int32 reason、Int32 pvLoopData)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)
在System.Windows.Forms.Application.Run(Form mainForm)中
在c:\Documents and Settings\Test01\My Documents\SharpDevelop Projects\DBSync\DBSync\Program.cs中的DBSync.Program.Main(字符串[]args)处:第27MySql.Data.MySqlClient.MySqlException(0x80004005)行:无法连接到任何指定的MySQL主机。
在MySql.Data.MySqlClient.NativeDriver.Open()上
在MySql.Data.MySqlClient.Driver.Open()上
位于MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder设置)
在MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()上
在MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()上
在MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()上
在MySql.Data.MySqlClient.MySqlPool.GetConnection()上
在MySql.Data.MySqlClient.MySqlConnection.Open()上
在DBSync.MainForm.btncalculate中,单击c:\Documents and Settings\Test01\My Documents\SharpDevelop Projects\DBSync\DBSync\MainForm.cs中的(对象发送方,事件参数e):第51行
在System.Windows.Forms.Control.OnClick(EventArgs e)中
在System.Windows.Forms.Button.OnClick(EventArgs e)中
在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs-mevent)上
在System.Windows.Forms.Control.WmMouseUp(消息和m、鼠标按钮、Int32单击)
在System.Windows.Forms.Control.WndProc(Message&;m)中
在System.Windows.Forms.ButtonBase.WndProc(Message&;m)
在System.Windows.Forms.Button.WndProc(Message&;m)中
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&;m)中
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&;m)中
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)
在System.Windows.Forms.UnsafentiveMethods.DispatchMessageW(MSG&;MSG)
位于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID、Int32 reason、Int32 pvLoopData)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)
在System.Windows.Forms.Application.Run(Form mainForm)中
在c:\Documents and Settings\Test01\My Documents\SharpDevelop Projects\DBSync\DBSync\Program.cs中的DBSync.Program.Main(字符串[]args)处:第27行服务器错误代码1042
我可以通过mysql workbench连接到mysql服务器并查询数据库。只有代码不起作用。
编辑:我注意到错误是在我使用sharpdevelop时出现的,而不是在我使用Visual Studio时出现的。

有时连接字符串中参数的间距和顺序很重要(基于个人经验和长夜:S)

因此,请坚持标准格式

Server=myserver地址;端口=1234;数据库=myDataBase; Uid=我的用户名;Pwd=我的密码


使用
SqlConnectionStringBuilder
简化连接

System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Initial Catalog"] = "Server";
builder["Data Source"] = "db";
builder["integrated Security"] = true;
string connexionString = builder.ConnectionString;
SqlConnection connexion = new SqlConnection(connexionString);
try { connexion.Open(); return true; }
catch { return false; }

只是遇到了同样的问题。在目标计算机上安装.NET framework解决了此问题

更好的是,确保所有必需的依赖项都存在于运行代码的机器中

using System;
using System.Linq;
using MySql.Data.MySqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            // add here your connection details
            String connectionString = "Server=localhost;Database=database;Uid=username;Pwd=password;";
            try
            {
                MySqlConnection connection = new MySqlConnection(connectionString);
                connection.Open();

                Console.WriteLine("MySQL version: " + connection.ServerVersion);
                connection.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.ReadKey();
        }
    }
}

确保您的数据库服务器正在运行,如果它没有运行,那么它就无法建立连接,默认情况下mysql在3306上运行,因此不需要提及端口如果相同,如果端口号不同,那么我们需要提及端口,因为这是Google上的最高结果:

如果您的连接最初工作正常,但在多次成功连接后,您开始看到此错误HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort 0xFFFF (DWORD) HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort\TcpTimedWaitDelay 60 (DWORD)
netsh int ipv4 set dynamicport tcp start=10000 num=50000
MysqlConn.ConnectionString = "Server=127.0.0.1;Database=patholabs;Uid=pankaj;Pwd=master;"
MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
conn_string.Server = "127.0.0.1";
conn_string.Port = 3306;
conn_string.UserID = "root";
conn_string.Password = "myPassword";
conn_string.Database = "myDB";



MySqlConnection MyCon = new MySqlConnection(conn_string.ToString());

try
{
    MyCon.Open();
    MessageBox.Show("Open");
    MyCon.Close();
    MessageBox.Show("Close");
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
"Server=127.0.0.1:3306;Uid=username;Pwd=password;Database=db;"
"Server=127.0.0.1;Port=3306;Uid=username;Pwd=password;Database=db;"
Server
Port
Uid
Pwd
Database