连接到SQL数据库时出现问题(但仅适用于WCF服务) [英] Problem connecting to SQL database (but only with WCF service)

查看:129
本文介绍了连接到SQL数据库时出现问题(但仅适用于WCF服务)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到了障碍,希望有人可以建议一些尝试的方法.我试图从WCF服务访问SQL数据库(MS SQL Server 2005,在Windows Server 2003下),但无法建立连接(在服务内),但在控制台应用程序WinForms中使用相同的代码应用程序,并且可以连接ASP.NET网页.

配置如下:

机器A:在Win 2003 Server下运行的SQL Server 2005. B:这是一台在Windows Server 2003下提供我的WCF服务并运行测试应用程序的计算机.

用于连接到SQL我正在使用Windows身份验证.



我有三个C#项目:WCF Web服务,控制台应用程序和WinForms应用程序.

在WCF服务项目中,我具有:

public void Test()
> {
字符串connectionString =数据源= {SQLSERVERNAME};初始目录= {TABLENAME};集成安全性= True'';
SqlConnection conn = new SqlConnection(connectionString);
conn.Open() ;
conn.Close();
}



在我的骗局中使用我拥有的服务的唯一应用程序:

main()
{
... ...
string connectionString =数据源= {SQLSERVERNAME};初始目录= {TABLENAME}; Integrated Security = True'';
SqlConnection conn =新的SqlConnection(connectionString);
conn.Open();
conn.Close();


MyServiceClient客户端=新的MyServiceClient ();
client.Test();
...
}



当我运行控制台应用程序(在VS调试器中)时,conn.Open ();作品.但是,当我进入client.Test()并执行Test()内的conn.Open()调用时,出现异常:

"用户'{MY_COMPANY'S_DOMAIN}登录失败{MY_MACHINE_NAME} $'."


很奇怪.为什么当我从应用程序执行但在WCF Web服务中无法运行时,它为什么起作用?为什么要尝试以我的机器名称运行?我在IIS集成Windows身份验证中检查了站点上的属性.


我应该指出,这似乎仅在Web服务内发生.在我的机器上,我可以:

-使用MS SQL Server Management Studio连接到SQL Server,浏览并运行查询
-运行具有DataGridView控件的WinForms应用程序,该控件能够运行查询针对数据库(并且使用相同的连接字符串)
-运行ASP.NET网页,该网页具有GridView控件,并且还可以针对数据库运行查询



有什么建议吗?





Jeff

I'm running into a roadblock here and hoping someone can suggest some things to try.  I'm trying to access a SQL database (MS SQL Server 2005, under Windows Server 2003) from my WCF service but I'm not able to make a connection (within the service), yet the same code in a console application, WinForms Application, and an ASP.NET web page is able to connect.

The configuration is as follows:

Machine A: SQL Server 2005 running under Win 2003 Server

Machine B: This is the machine with is serving my WCF service as well as running the test apps, under Windows Server 2003

For connecting to SQL I'm using Windows authentication.



I have three C# projects: A WCF web service, a console app, and a WinForms app.

In the WCF service project I have:

public void Test()
{
    string connectionString = "Data Source={SQLSERVERNAME};Initial Catalog={TABLENAME};Integrated Security=True";
    SqlConnection conn = new SqlConnection(connectionString);
    conn.Open();
    conn.Close();
}



In my console app which uses the service I have:

main()
{
    ...
    string connectionString = "Data Source={SQLSERVERNAME};Initial Catalog={TABLENAME};Integrated Security=True";
    SqlConnection conn = new SqlConnection(connectionString);
    conn.Open();
    conn.Close();


    MyServiceClient client = new MyServiceClient();
    client.Test();
    ...
}



When I run the console app (in the VS debugger), conn.Open(); works.  However, when I step into client.Test() and execute the conn.Open() call that's within Test(), I get the exception:

"Login failed for user '{MY_COMPANY'S_DOMAIN}\{MY_MACHINE_NAME}$'."


Weird.  Why does it work when I execute from the app but not when within a WCF web service?  And why is it trying to run as my machine's name?  I checked properties on the site in IIS integrated windows authentication is checked.


I should point out that this seems to only happen within the web service.  On my machine I can:

-Connect to the SQL server with MS SQL Server Management Studio, browse, and run queries
-Run a WinForms app which has a DataGridView control which is able to run a query against the database (and it uses the same connection string)
-Run an ASP.NET web page which has a GridView control and am able to run a query against the database as well



Any suggestions?



Thanks,

Jeff

推荐答案

Hello Jeff,

我不确定您的情况如何.可能是您的WCF服务无法访问SQL Server实例.

请尝试本文中给出的步骤,

Hello Jeff,

I am not sure whats happening in your scenario. May be your WCF service dose not have access to SQL server instance.

Please try the steps given in this article,

http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How To - Use SQL Role Provider with Windows Authentication in WCF calling from Windows Forms&referringTitle=How Tos

Hope this will work for you.

Thanks,
Jay


这篇关于连接到SQL数据库时出现问题(但仅适用于WCF服务)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆