C# MySQL連線問題:無法連線到指定的MySQL主機
P粉994092873
P粉994092873 2023-08-20 19:50:26
0
1
457
<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.dlt; ;ExceptionType>MySql.Data.MySqlClient.MySqlException, MySql.Data, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d</ExceptionType></訊息><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) 中指定的 D005).任何位置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 args) Value>1042</Value></Data></DataItems></Exception></TraceRecord></pre> <p>我可以透過mysql工作台連接到mysql伺服器並查詢資料庫。只有程式碼失效。 編輯:我注意到當我使用sharpdevelop時出現錯誤,而在使用Visual Studio時沒有出現錯誤。</p>
P粉994092873
P粉994092873

全部回覆(1)
P粉078945182

有時候連接字串中的間距和參數的順序是有關係的(根據個人經驗和一個漫長的夜晚 :S)

所以要堅持使用標準格式,請參考這裡

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!