简体   繁体   English

通过WCF服务调用时数据库连接不起作用

[英]Database connection not working when called through wcf service

My application has a DAL layer with a working local database. 我的应用程序具有一个DAL层,其中包含一个有效的本地数据库。 I have tests interacting with the db and they work fine. 我有与数据库交互的测试,它们工作正常。

I also have a wcf service that uses the functions provided by my DAL. 我也有使用我的DAL提供的功能的wcf服务。 However, when the client application calls the service, no data gets retrieved. 但是,当客户端应用程序调用服务时,不会检索任何数据。 Instead, I get an exception: 相反,我得到一个例外:

Message = "The underlying provider failed on Open." Message =“基础提供程序在打开时失败。”

"System.Data.Entity" “ System.Data.Entity”

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

With an inner exception: 有一个内部例外:

"Unable to connect to any of the specified MySQL hosts." “无法连接到任何指定的MySQL主机。”

{"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)" “在MySql.Data.MySqlClient.Driver.Open()\\ r \\ n在MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder设置)\\ r \\ n在MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()\\ r \\ n在MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()\\ r \\ n在MySql.Data.MySqlClient.MySqlPool.GetConnection()\\ r \\ n System.Data.EntityClient.EntityConnection.OpenStoreConnectionIfIf上的MySql.Data.MySqlClient.MySqlConnection.Open()\\ r \\ n(Boolean openCondition,DbConnection storeConnectionToOpen,DbConnection originalConnection,String exceptionCode,String tryedOperation,Boolean&closeStoreConnectionOnFailure)”

I verified that my connection string is the same as what my tests use. 我验证了我的连接字符串与测试使用的字符串相同。

<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"/>

The database is a local mysql database, the user and database both exist. 该数据库是本地mysql数据库,用户和数据库都存在。 The services are also local, running in IIS Express. 这些服务也是本地的,在IIS Express中运行。

Why am I getting this exception? 为什么会出现此异常?

This could be due to several reasons please check below points: 这可能是由于多种原因,请检查以下几点:

  • If you are using System.Data.SqlClient in your connection then change it to System.Data.Odbc 如果在连接中使用System.Data.SqlClient ,则将其更改为System.Data.Odbc
  • Check database name 检查数据库名称
  • Check you port is open or not. 检查您的端口是否打开。 (probably 3306) (大概是3306)
  • Change your bind-address in your my.ini file (from 127.0.0.1 to 0.0.0.0) 更改my.ini文件中的绑定地址(从127.0.0.1更改为0.0.0.0)
  • Check the user your trying to connect to server has necessary privileges 检查您尝试连接服务器的用户是否具有必要的特权

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM