c# - 通过 wcf 服务调用时数据库连接不起作用

标签 c# mysql wcf exception

我的应用程序有一个带有工作本地数据库的 DAL 层。我有与数据库交互的测试,它们工作正常。

我还有一个 wcf 服务,它使用我的 DAL 提供的功能。但是,当客户端应用程序调用该服务时,不会检索到任何数据。相反,我得到了一个异常(exception):

Message = "The underlying provider failed on Open."

"System.Data.Entity"

StackTrace = "at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)\r\n ...

有一个内部异常:

"Unable to connect to any of the specified MySQL hosts."

{"No such host is known"}

" at MySql.Data.MySqlClient.NativeDriver.Open()\r\n at MySql.Data.MySqlClient.Driver.Open()\r\n at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)\r\n at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()\r\n at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()\r\n at MySql.Data.MySqlClient.MySqlPool.GetConnection()\r\n at MySql.Data.MySqlClient.MySqlConnection.Open()\r\n at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)"

我验证了我的连接字符串与我的测试使用的相同。

<add name="loansEntities" connectionString="metadata=res://*/LoansDbModel.csdl|res://*/LoansDbModel.ssdl|res://*/LoansDbModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost:3306;User Id=root;password=123456;database=dbname;Persist Security Info=True&quot;" providerName="System.Data.EntityClient"/>

数据库是本地mysql数据库,用户和数据库都存在。 这些服务也是本地的,在 IIS Express 中运行。

为什么我会收到此异常?

最佳答案

这可能是由于多种原因造成的,请检查以下几点:

  • 如果您在连接中使用 System.Data.SqlClient,请更改 它到System.Data.Odbc
  • 检查数据库名称
  • 检查端口是否打开。 (大概3306)
  • 更改 my.ini 文件中的绑定(bind)地址(从 127.0.0.1 更改为 0.0.0.0)
  • 检查您尝试连接到服务器的用户是否拥有必要的权限

关于c# - 通过 wcf 服务调用时数据库连接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35464804/

相关文章:

c# - 如何为字符串中的特定关键字预着色?

c# - 具有自动生成列的可编辑 GridView

mysql - 创建一个基于日期的循环 Mysql

mysql - 无法从远程系统连接 EC2 中的 AWS MySQL

c# - 通过自定义跟踪监听器一次完成 WCF 请求-响应日志记录

c# - WCF 将自定义 HTTP header 添加到请求中

c# - 命名空间在类中找到,但在 aspx 中找不到

mysql - 在 MySQL 中如何从具有相似值的行中选择所有 id

c# - 如何用MYSQL通过WCF Service插入操作?

c# - "Match making"算法?