我正在尝试使用此连接字符串将IIS6上的.NET Framework 4.0 WCF REST服务连接到SQL Server 2008 R2数据库:
<add name="EReportEntities"
connectionString="metadata=res://*/EReportModel.csdl|res://*/EReportModel.ssdl|res://*/EReportModel.msl;provider=System.Data.SqlClient;provider connection string="Server=localhost;Database=ADM;User Id=sa;Password=XXXXXXXX;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
但我不能。我明白了:
Event Type: Failure Audit
Event Source: MSSQLSERVER
Event Category: (4)
Event ID: 18456
Date: 10/19/2012
Time: 10:47:48 AM
User: N/A
Computer: UNILEVER
Description:
Login failed for user 'sa'. [CLIENT: <local machine>]
For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.
Data:
0000: 18 48 00 00 0e 00 00 00 .H......
0008: 09 00 00 00 55 00 4e 00 ....U.N.
0010: 49 00 4c 00 45 00 56 00 I.L.E.V.
0018: 45 00 52 00 00 00 07 00 E.R.....
0020: 00 00 6d 00 61 00 73 00 ..m.a.s.
0028: 74 00 65 00 72 00 00 00 t.e.r...
但是,如果我使用sa用户登录SQL Server Management Studio,我可以登录。
我做错了什么?
答案 0 :(得分:1)
鉴于您的连接字符串指向localhost
,您的WCF服务无法连接就不足为奇了。
connectionString="metadata=res://*/EReportModel.csdl|res://*/EReportModel.ssdl|res://*/EReportModel.msl;
provider=System.Data.SqlClient;
provider connection string="Server=localhost;
Database=ADM;
. . .
我经常建议将您的WCF服务包装在try..catch
中,如果出现问题,请将连接字符串附加到错误消息中,以检查您的服务 是否正在使用正确的连接字符串。
(我曾经有过我的web.config
包含多个连接字符串的时间,但是我的实体框架/ LINQ to SQL决定使用错误的连接字符串。这将确定这种情况。)
例如,这是我将使用的代码,用于简单的WCF服务:
public List<wsCustomer> GetAllCustomers()
{
NorthwindDataContext dc = null;
try
{
dc = new NorthwindDataContext();
List<wsCustomer> results = new List<wsCustomer>();
foreach (Customer cust in dc.Customers)
{
results.Add(new wsCustomer()
{
CustomerID = cust.CustomerID,
CompanyName = cust.CompanyName,
City = cust.City
});
}
return results;
}
catch (Exception ex)
{
OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
response.StatusDescription = ex.Message.Replace("\r\n", "") + " " + dc.Connection.ConnectionString;
return null;
}
}
使用此代码,如果出现问题,您可以看到SQL Server错误以及它尝试使用的服务器/数据库。
此处有更多详情: WCF Deployment and troubleshooting
答案 1 :(得分:0)