Sql server Can';我无法从WCF服务连接到SQL Server,但我可以从Management Studio连接

Sql server Can';我无法从WCF服务连接到SQL Server,但我可以从Management Studio连接,sql-server,wcf,sql-server-2008,iis-6,database-connection,Sql Server,Wcf,Sql Server 2008,Iis 6,Database Connection,我正在尝试使用以下连接字符串将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;pr

我正在尝试使用以下连接字符串将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=&quot;Server=localhost;Database=ADM;User Id=sa;Password=XXXXXXXX;multipleactiveresultsets=True;App=EntityFramework&quot;" 
     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...
事件类型:失败审核
事件源:MSSQLSERVER
活动类别:(4)
事件编号:18456
日期:2012年10月19日
时间:上午10:47:48
用户:不适用
计算机:联合利华
说明:
用户“sa”登录失败。[客户:]
有关更多信息,请参阅帮助和支持中心,网址为http://go.microsoft.com/fwlink/events.asp.
数据:
0000:1848000e00.H。。。。。。
0008:09 00 55 00 4e 00…联合国。
0010:49 00 4c 00 45 00 56 00 I.L.E.V。
0018:45 00 52 00 00 07 00 E.R。。。。。
0020:00006D0061007300..m.a.s。
0028:74 00 65 00 72 00 t.e.r。。。
但是,如果我使用sa用户登录SQLServerManagementStudio,我可以登录


我做错了什么?

尝试在

处使用其他连接字符串变量。您的WCF服务无法连接并不奇怪,因为您的连接字符串指向
localhost

connectionString="metadata=res://*/EReportModel.csdl|res://*/EReportModel.ssdl|res://*/EReportModel.msl;
provider=System.Data.SqlClient;
provider connection string=&quot;Server=localhost;
Database=ADM;
. . . 
我经常建议您将WCF服务包装为一个
try..catch
,如果出现问题,请将连接字符串附加到错误消息中,以检查您的服务是否使用了正确的连接字符串

(我的
web.config
曾包含多个连接字符串,但我的Entity Framework/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;
    }
}
公共列表GetAllCustomers()
{
NorthwindDataContext dc=null;
尝试
{
dc=新的NorthwindDataContext();
列表结果=新列表();
foreach(dc客户中的客户)
{
结果.添加(新wsCustomer()
{
CustomerID=cust.CustomerID,
CompanyName=cust.CompanyName,
城市=客户城市
});
}
返回结果;
}
捕获(例外情况除外)
{
OutgoingWebResponseContext响应=WebOperationContext.Current.OutgoingResponse;
response.StatusCode=System.Net.HttpStatusCode.InternalServerError;
response.StatusDescription=ex.Message.Replace(“\r\n”,”)+“+dc.Connection.ConnectionString;
返回null;
}
}
使用此代码,如果出现问题,您可以看到SQL Server错误以及它试图使用的服务器/数据库

详情如下:

这是实体框架的连接字符串-您是否使用EF从WCF服务与SQL Server对话?是的,我使用的是实体框架。您的数据库是否与IIS位于同一台服务器上?您正在连接字符串中使用
Server=localhost
。是的,两者都在同一台计算机中。系统管理员现在告诉我这是一个“表面暴露”的问题…是的,我知道。我问过这个问题,DB管理员告诉我没有问题:((