首页  >  问答  >  正文

C# MySQL连接问题:无法连接到指定的MySQL主机

<p>当我执行以下代码时,我遇到了上述错误 -</p> <pre class="brush:php;toolbar:false;">MySqlConnection mysqlConn=new MySqlConnection("server=127.0.0.1;uid=pankaj;port=3306;pwd=master;database=patholabs;"); mysqlConn.Open();</pre> <p>我尝试将服务器设置为localhost,用户设置为root,但是我遇到了以下错误 -</p> <pre class="brush:php;toolbar:false;">错误:0:无法连接到指定的任何MySQL主机。 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>未处理的异常</Description><AppDomain>DBSync.exe</AppDomain><Exception><ExceptionType>MySql.Data.MySqlClient.MySqlException, MySql.Data, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d</ExceptionType><Message>无法连接到指定的任何MySQL主机。</消息><StackTrace> 在 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() 在 c:Documents and SettingsTest01My DocumentsSharpDevelop ProjectsDBSyncDBSyncMainForm.cs 中的 DBSync.MainForm.BtnCalculateClick(Object sender, EventArgs 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(Message&m, MouseButtons 按钮, 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.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg) 在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32原因,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 SettingsTest01My DocumentsSharpDevelop ProjectsDBSyncDBSyncProgram.cs:line 27MySql.Data.MySqlClient.MySqlException (0x80004005) 中的 DBSync.Program.Main(String[] args) 处:无法连接到指定的任何位置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() 在 c:Documents and SettingsTest01My DocumentsSharpDevelop ProjectsDBSyncDBSyncMainForm.cs 中的 DBSync.MainForm.BtnCalculateClick(Object sender, EventArgs 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(Message&m, MouseButtons 按钮, 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.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg) 在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32原因,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 SettingsTest01My DocumentsSharpDevelop ProjectsDBSyncDBSyncProgram.cs:line 27</ExceptionString><DataItems><Data><Key>服务器错误代码</Key><中的 DBSync.Program.Main(String[] args) Value>1042</Value></Data></DataItems></Exception></TraceRecord></pre> <p>我可以通过mysql工作台连接到mysql服务器并查询数据库。只有代码失效。 编辑:我注意到当我使用sharpdevelop时出现错误,而在使用Visual Studio时没有出现错误。</p>
P粉994092873P粉994092873426 天前477

全部回复(1)我来回复

  • P粉078945182

    P粉0789451822023-08-21 15:06:06

    有时候连接字符串中的间距和参数的顺序是有关系的(根据个人经验和一个漫长的夜晚 :S)

    所以要坚持使用标准格式,请参考这里

    回复
    0
  • 取消回复