c# - 无法连接到任何指定的 MySQL 主机,以前的工作代码

标签 c# mysql asp.net web-config

总结:

尝试通过 ASP.NET Web 应用程序连接到 MySQL 数据库时,出现“无法连接到任何指定的 MySQL 主机,以前工作的代码”错误。完整错误如下。

奇怪的是,我只是在一个临时的新网站项目中开发了该应用程序,并且在那里一切正常。但是,当我将它用于我试图在其中实现它的网站的离线版本时,会出现此错误。有趣的是,我实际上使用的是一个已经在网站内设置并正常运行的连接字符串。所以我很困惑为什么我的代码在别处运行,而连接字符串在别处运行,但是当我尝试将它们一起使用时,我收到“无法连接”错误。

我尝试过的:

第一件事显然是检查我的连接字符串的格式是否正确。我仔细检查了这一点,但正如我所说,连接字符串已经(并且仍然)与网站上的其他页面一起工作。

我还看到许多关于此错误的帖子,建议我在 web.config 文件中包含该标记。然而,这并没有解决我的问题。

我还验证了我的 MySQL 数据库已正常运行。如前所述,我可以获得用于处理页面其他方面的连接字符串。

代码片段:

完整错误:

MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the     specified MySQL hosts. ---> System.Security.SecurityException: Request for the     permission of type 'System.Net.SocketPermission, System, Version=2.0.0.0,     Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
 at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) 
at System.Security.CodeAccessPermission.Demand() 
at System.Net.Sockets.Socket.CheckCacheRemote(EndPoint& remoteEP, Boolean isOverwrite) 
at System.Net.Sockets.Socket.BeginConnectEx(EndPoint remoteEP, Boolean flowContext, AsyncCallback callback, Object state) 
at System.Net.Sockets.Socket.BeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state) 
at MySql.Data.Common.StreamCreator.CreateSocketStream(IPAddress ip, Boolean unix) at MySql.Data.Common.StreamCreator.GetStream(UInt32 timeout) 
at MySql.Data.MySqlClient.NativeDriver.Open() 
The action that failed was: Demand 
The type of the first permission that failed was: System.Net.SocketPermission 
The first permission that failed was: 
The demand was for: 
The granted set of the failing assembly was: 
The assembly or AppDomain that failed was: MySql.Data, Version=6.2.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d 
The method that caused the failure was: System.IO.Stream CreateSocketStream(System.Net.IPAddress, Boolean) 
The Zone of the assembly that failed was: Intranet The Url of the assembly that failed was: file://PROAPPSRV/inetpub/wwwroot/MyChiller/bin/MySql.Data.DLL -    
-- End of inner exception stack trace --- 
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 TCCVC_Login.ValidateUser(Object sender, EventArgs e)

C# MySQL 连接器:

    using (MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["newrst110ConnectionString"].ConnectionString))
    {

        using (MySqlCommand cmd = conn.CreateCommand())
        {
            try
            {
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandText = @"SELECT custpass AS hash, (lastlogin IS NOT NULL) AS firstLogin FROM rts110.mychiller_users
                                        WHERE custusername = @user";
                cmd.Parameters.AddWithValue("@user", Login1.UserName);
                MySqlDataReader reader = cmd.ExecuteReader();

                DataTable returnTable = new DataTable();
                returnTable.Load(reader);
                reader.Close();

                // UPDATE LAST LOGIN DATE
                cmd.CommandText = @"UPDATE mychiller_users
                                        SET lastLogin = CURDATE()
                                        WHERE custusername = @userName";
                cmd.Parameters.AddWithValue("@userName", Login1.UserName);
                cmd.ExecuteNonQuery();

                cmd.Dispose();

                // . . . 
                // Additional code down here that handles the return and closes the connection.

连接字符串:

<add name="newrst110ConnectionString" connectionString="Datasource=70.103.118.100;Database=rts110;uid=####;pwd=####;" providerName="MySql.Data.MySqlClient"/>

有谁知道为什么当各个组件都正常运行时我会遇到这个错误?

预先感谢您的帮助。

更新

我一直在修补这个问题,看看是否可以缩小问题的范围。我注意到当我创建一个测试页面并在下面创建非常基本的连接器时,我收到一个应用程序安全错误,也在下面。

简单连接器

DataTable returnTable = new DataTable();

    using (var conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["workorderConnectionString"].ConnectionString))
    {
        conn.Open();
        using (var cmd = conn.CreateCommand())
        {
            try
            {
                cmd.CommandText = @"SELECT * FROM workorder.jobnumber";

                MySqlDataReader reader = cmd.ExecuteReader();

                returnTable.Load(reader);
                reader.Close();
                cmd.Dispose();

            }
            catch (Exception ex)
            {
                lbltest.Text = ex.ToString();
            }
            finally
            {
                conn.Close();
            }
        }
    }

应用程序错误:

Server Error in '/' Application.

Security Exception

Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. 

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Net.SocketPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[SecurityException: Request for the permission of type 'System.Net.SocketPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
System.Security.CodeAccessPermission.Demand() +54
System.Net.Sockets.Socket.CheckCacheRemote(EndPoint& remoteEP, Boolean isOverwrite) +270
System.Net.Sockets.Socket.BeginConnectEx(EndPoint remoteEP, Boolean flowContext, AsyncCallback callback, Object state) +175
System.Net.Sockets.Socket.BeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state) +179
MySql.Data.Common.StreamCreator.CreateSocketStream(IPAddress ip, Boolean unix) +239
MySql.Data.Common.StreamCreator.GetStream(UInt32 timeout) +650
MySql.Data.MySqlClient.NativeDriver.Open() +366

根据我的谷歌搜索,这再次指向 Web.Config 文件中的标记,我已经将其包含在其中。也许我错误地包含了这个?

我想知道它是否也可能与我拥有的两个 Web.config 文件有关(一个位于包含成员页面的子文件夹中,以限制未经验证 session 的访问)。我尝试在两者中添加,但没有发现任何区别。

最佳答案

你能附上你的连接字符串的版本吗?您的配置文件似乎配置有误。

关于c# - 无法连接到任何指定的 MySQL 主机,以前的工作代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44269588/

相关文章:

c# - 如何复制表条目及其依赖表行mysql

c# - AppDomain.CurrentDomain.AssemblyResolve 可能存在的信任问题或我应该注意的其他问题

c# - 模型未在 ASP.NET MVC 3 中传递

python - 什么是用一个键和多个值更改字典以获得所需输出的 ​​Pythonic 方法?

c# - 将整数转换为三位数

c# - 具有损坏索引的对象的 Razor Pages 模型绑定(bind)列表

c# - 如何在C#中动态创建连接字符串

PHP 和 MySQL : Replace Image if the File Name (not file extension) is Identical?

asp.net - 网站的SEO布局

mysql - 将 PK 合并到表格中的 A 列