我的应用程序有一个带有工作本地数据库的 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="server=localhost:3306;User Id=root;password=123456;database=dbname;Persist Security Info=True"" 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/