C# MySQL连接问题:无法连接到指定的MySQL主机
P粉994092873
P粉994092873 2023-08-20 19:50:26
[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 : &lt;TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"&gt;&lt;TraceIdentifier&gt;http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled&lt;/TraceIdentifier&gt;&lt;Description&gt;未处理的异常&lt;/Description&gt;&lt;AppDomain&gt;DBSync.exe&lt;/AppDomain&gt;&lt;Exception&gt;&lt;ExceptionType&gt;MySql.Data.MySqlClient.MySqlException, MySql.Data, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d&lt;/ExceptionType&gt;&lt;Message&gt;无法连接到指定的任何MySQL主机。&lt;/Message&gt;&lt;StackTrace&gt; 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 SettingsTest01My DocumentsSharpDevelop ProjectsDBSyncDBSyncMainForm.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;amp; m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message&amp;amp;amp; m) at System.Windows.Forms.ButtonBase.WndProc(Message&amp;amp;amp; m) at System.Windows.Forms.Button.WndProc(Message&amp;amp;amp; m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp;amp; m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;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;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 SettingsTest01My DocumentsSharpDevelop ProjectsDBSyncDBSyncProgram.cs:line 27&lt;/StackTrace&gt;&lt;ExceptionString&gt;MySql.Data.MySqlClient.MySqlException (0x80004005): 无法连接到指定的任何MySQL主机。 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 SettingsTest01My DocumentsSharpDevelop ProjectsDBSyncDBSyncMainForm.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;amp; m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message&amp;amp;amp; m) at System.Windows.Forms.ButtonBase.WndProc(Message&amp;amp;amp; m) at System.Windows.Forms.Button.WndProc(Message&amp;amp;amp; m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp;amp; m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;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;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 SettingsTest01My DocumentsSharpDevelop ProjectsDBSyncDBSyncProgram.cs:line 27&lt;/ExceptionString&gt;&lt;DataItems&gt;&lt;Data&gt;&lt;Key&gt;Server Error Code&lt;/Key&gt;&lt;Value&gt;1042&lt;/Value&gt;&lt;/Data&gt;&lt;/DataItems&gt;&lt;/Exception&gt;&lt;/TraceRecord&gt;</pre> <p>我可以通过mysql workbench连接到mysql服务器并查询数据库。只有代码不起作用。 编辑:我注意到当我使用sharpdevelop时出现错误,而在使用Visual Studio时没有出现错误。</p>
P粉994092873
P粉994092873

全部回复(1)
P粉078945182

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

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号