将Windows服务连接到sql server的问题 [英] Problem to connect windows service to sql server

查看:112
本文介绍了将Windows服务连接到sql server的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我创建了Windows服务,通过互联网将数据从本地数据库传输到SQL Server但连接总是失败,尽管在Windows窗体中相同的连接也能正常工作应用程序,是Windows服务需要特殊的连接字符串或配置来连接到SQL服务器或者我错过了什么



任何人都可以帮助我

- -------------------------------------------------

您好再次



感谢您的所有回复,但我仍然遇到同样的问题,连接在Windows应用程序中工作但在服务中不起作用申请,请在下面找到代码,希望任何人都可以帮助我





服务帐号:

LocalService



连接字符串

name =myconnprovidername =System.Data.SqlClientconnectionstring =Data Source = serverip \SQLEXPRESS;

初始目录=数据库; U ser ID = username;密码= mypassword



代码

ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings [ myconn];

SqlConnection destConnection = new SqlConnection(settings.ToString());

destConnection.Open();





例外

建立网络相关或特定于实例的错误连接到SQL Server。

找不到服务器或无法访问服务器。验证实例名称是否正确以及

SQL Server配置为允许远程连接。 br />
(提供者:SQL网络接口,错误:26 - 错误定位指定的服务器/实例)

Hi

I created windows service to transfer data from local database to SQL Server through internet but the connection always failed although the same connection works very will in Windows Form Application, Is Windows Service need special connection string or configuration to connect to sql server or I missing something

anyone can help me
---------------------------------------------------
Hi again

Thanks for all your reply but I still have the same problem the connection work in windows application but didn't work in Services Application , kindly find below the code hope that any one can help me


Service Account:
LocalService

connection string
name="myconn" providername="System.Data.SqlClient" connectionstring="Data Source=serverip\SQLEXPRESS;
Initial Catalog=database; User ID=username; Password=mypassword"

Code
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["myconn"];
SqlConnection destConnection = new SqlConnection(settings.ToString ());
destConnection.Open();


Exception
A network-related or instance-specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections.
(provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

推荐答案

我最好的猜测就是你'重新使用Windows身份验证连接到SQL?



如果是这种情况,您需要确保运行服务的用户在SQL中具有权限。



如果您查看系统,管理工具,服务并找到您的服务。您可以调整与其关联的凭据。



当您运行Windows窗体应用程序时,运行它的安全上下文是登录用户的安全上下文。将代码移动到服务时,安全上下文不会持久。



通常,当我们安装服务时,它们会根据网络服务或本地系统安全原则进行安装。这些不应该被赋予访问SQL的权限。



您也可以创建一个SQL用户并使用实现SQL身份验证的连接字符串。
My best guess would be that you're using windows authentication to connect to SQL?

If this is the case, you need to make sure the user under which the service is running has permissions within SQL.

If you look in System, Administrative Tools, Services and find your service. You can adjust the credentials associated with it.

When you run a windows form application the security context under which it is run is that of the user who's logged in. That security context doesn't persist when you move the code to a service.

Often when we install services they install against the Network Service or Local System security principles. These shouldn't be given access to SQL.

You could also create an SQL user and use a connection string which implements SQL authentication instead.


检查以下几点。

1.如果你在app.config中定义你的连接字符串< appSettings>

然后请尝试在<结构> < ConnectionString>下的部分。



2.如果你已经完成了积分1,那么检查system.Configuration的引用是否给予服务。 />


3.如果你已经完成了积分号码。 2然后检查你是不是试图获取连接字符串的功能,我的意思是说构造函数或其他什么。



如果都超过3分没关系,那么

你可以在这个仪表板上粘贴你的两个连接代码吗?



之后我会帮你。

因为我已经创建了许多Windows服务,就像你的方法一样。

我想我会帮助你。



Mahesh Patel

MCA-2010
check below points.
1. If you are define your connection string in app.config under <appSettings>
Then please try to define it in <Configuration> section under <ConnectionString>.

2. If you have already done points one then check that the reference of system.Configuration is given to service or not.

3. if you have also already done point no. 2 then check that your are not try to getting the value of connection string out of function, i mean to say out of constructor or whatever.

If all above 3 points are ok, then
Can you paste your both connection code in this dash board?

After it i will help you.
Because i have created many windows service just like your approach.
and i think i will help you.

Mahesh Patel
MCA-2010


通常,服务在本地系统帐户下运行。该帐户无法访问任何网络/互联网资源。将您的服务用户更改为具有网络访问权限的用户。
Typically, a service runs under "Local System" account. That account cannot access any network / internet resources. Change the user for your service to a user with network access rights.


这篇关于将Windows服务连接到sql server的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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