windows服务与sql数据库的连接 [英] Connection between windows service and sql database

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

问题描述

我正在尝试在用 C# 编写的 Windows 服务和 SQL Server Express 数据库之间建立连接.但是当 connection.open 被调用时,我得到这个异常

I am trying to establishing a connection between a Windows service written in C# and a SQL Server Express database. But when connection.open is called I get this exception

System.data.sqlClient.sqlexception:

System.data.sqlClient.sqlexception:

{"由于执行失败,无法生成 SQL Server 的用户实例启动用户实例的进程.连接将是关闭."}

{"Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed."}

但是当我将其作为控制台应用程序运行时,相同的代码工作正常.有人可以帮助我吗?

But the same code works fine when I run it as console application.Can someone please help me with this??

_connection = new SqlConnection("Data Source=.\\MSSQLSERVER1;AttachDbFilename=D:\\vinay\\project\\LightHistorian\\LH_DB.MDF‌​;Integrated Security=True;User Instance=True;");

我给了与本地系统相同的凭据.它可以工作几次,但在其他时间不工作.但是一旦发生这种异常,如果我重新启动机器,它就可以工作.想知道问题是什么??

i gave same credentials as local system.It works a times and doesnt at some other time.But when ever this exception happens,If i restart the machine it works.Wonder what the problem is??

我为 Windows 服务和控制台应用程序使用了相同的连接

I have used same connection for both windows service and console application

推荐答案

当您执行控制台应用程序时,EXE 在您的用户凭据下运行.
当您执行服务时,哪些是用户凭据?您的服务是否在 Locl 服务帐户下运行?还是系统?或者什么?
尝试手动设置您的服务必须使用的凭据(在 Windows 服务面板中,检查您的服务属性)并尝试使用您运行控制台应用程序的同一用户/密码来执行它.

When you execute your console application, the EXE runs under your user credentials.
When you execute your service, which are user credentials? Is your service running under Locl Service Account? Or System? Or what?
Try to manually set the credentials your service must run with (in Windows services panel, check your service properties) and try to execute it with the same user/pwd you run your console app with.

更新取自此处:
在本地 SQL Server Express 实例上使用用户实例
用户实例功能在连接期间动态创建新的 SQL Server 实例.这仅适用于本地 SQL Server 2005 实例,并且仅适用于通过本地命名管道使用 Windows 身份验证进行连接时.目的是能够为在计算机上具有有限管理权限的用户创建完全权限的 SQL Server 实例.
我可以认为您的服务凭据未被授予创建一个新的 SQL 服务器实例,因此请尝试删除该部分.
无论如何,在服务属性上设置用户凭据,看看会发生什么.

UPDATE taken from here:
Using an User Instance on a local SQL Server Express instance
The User Instance functionality creates a new SQL Server instance on the fly during connect. This works only on a local SQL Server 2005 instance and only when connecting using windows authentication over local named pipes. The purpose is to be able to create a full rights SQL Server instance to a user with limited administrative rights on the computer.
I could think that your service credentials are not granted to create a new SQL server instance, so try to remove that part.
Anyway, set user credentials on service properties and see what happens.

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

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