如何从Windows服务访问的LocalDBWindows、LocalDB

2023-09-04 22:38:09 作者:唯美

我需要在本地使用的数据库。例如,从连接时,我在(的LocalDB)\ V11.0 Server和Visual Studio中创建的数据库2012年一切正常控制台应用程序。但我的应用程序是Windows服务。尝试连接到我的数据库输出:

I need to use data base locally. I have created database in (LocalDB)\v11.0 Server with Visual Studio 2012. Everything is ok when connecting from e.g. console application. But my application is Windows Service. trying to connect to my data base outputs with:

"... The login failed.Login failed for user 'NT AUTHORITY\SYSTEM"

对此有什么解决办法?也许我应该考虑设立其他数据库服务器呢?如果是,如何设置它,以便它是可见的,只有在本地?

Is there any solution for this? Maybe I should think of setting up other db server for it? If yes, how to set up it so that it would be visible only locally?

推荐答案

所以,我发现了自己的解决方案,如何连接的(的LocalDB)从Windows服务而运行于本地系统的实例:

So i found out solution myself, how to connect instance of (localdb) from Windows Service which runs as Local System:

我用这个文章: http://technet.microsoft.com/pl-pl/library/hh212961.aspx 的http://dba.fyicenter.com/faq/sql_server_2/Verifying_a_Login_Name_with_SQLCMD_Tool.html http://social.technet.microsoft.com/wiki/contents/articles/4609.troubleshoot-sql-server-2012-ex$p$pss-localdb.aspx

I used this articles: http://technet.microsoft.com/pl-pl/library/hh212961.aspx http://dba.fyicenter.com/faq/sql_server_2/Verifying_a_Login_Name_with_SQLCMD_Tool.html http://social.technet.microsoft.com/wiki/contents/articles/4609.troubleshoot-sql-server-2012-express-localdb.aspx

所以,我分享我的LocalDB(如在第一条)的问题是我不能SQLCMD命名管道NP连接://。我发现前面回答的第3条:

So i shared my localdb (as in 1st article) the problem was I couldn't connect with sqlcmd named pipe np://. I found anwer in 3rd article:

在使用SQLCMD,请确保您使用的是SQL Server 2012中   版(%计划Files%\微软SQL发现   服务器\ 110 \ TOOLS \ BINN)。如果你有SQLCMD的previous版本   安装,调用单独的命令行SQLCMD将最有可能   使用旧的版本(未的LocalDB感知)因为旧的路径   第一次出现在PATH环境变量。它可能是一个好   理念,在一般情况下,手动调整你的PATH环境变量,以便   该110版本拿起第一。

When using sqlcmd, ensure that you are using the SQL Server 2012 version (found in %Program Files%\Microsoft SQL Server\110\Tools\Binn). If you have previous versions of sqlcmd installed, calling sqlcmd alone from the command line will most likely use the old version (which isn't localdb-aware) since the older path appears first in your PATH environment variable. It may be a good idea, in general, to manually adjust your PATH environment variable so that the 110 versions are picked up first.

其实这个小信息是至关重要的;)

This small information in fact was crucial;)

所以我创建的用户:用户密码: pass321 @

so i created user: user with password: pass321!@.

在我的Windows服务,我的SQL的connectionString如下:

In my windows service my Sql connectionString looks:

"Data Source=(localdb)\\.\\MyInstanceShared;Integrated Security=false;User Id=user;Password=pass321!@"

集成安全设置为错误的价值也很重要。

也许这将帮助别人。

 
精彩推荐
图片推荐