c# - 如何从 Windows 服务访问 localDB

标签 c# sql .net sql-server

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

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

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

最佳答案

所以我自己找到了解决方案,如何从作为本地系统运行的 Windows 服务连接 (localdb) 的实例:

我用过这篇文章: 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(如第 1 篇文章中所述),问题是我无法连接名为 pipe np://的 sqlcmd。我在第三篇文章中找到了答案:

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.

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

所以我创建了用户:user,密码:pass321!@

在我的 Windows 服务中,我的 Sql connectionString 看起来是:

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

将集成安全设置为 false 值也很重要。

也许它会对某人有所帮助。

关于c# - 如何从 Windows 服务访问 localDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21830727/

相关文章:

sql - jsonb LIKE 查询数组中的嵌套对象

c# - 在 VS2010 的 .Net 2.0 项目中使用托管 C++ (.Net 2.0) 时出现问题

c# - 如何将 MonoGame.Framework 添加到 Xamarin 中的 Farseer Physics for MonoGame 项目?

c# - 让 XAML 设计器在 VS2010 中显示设计

sql - 使用 CROSS JOIN SEQUENCE 生成大型重复表

c# - 如何安装色谱柱?

c# - 使用枚举作为值识别选中哪个单选按钮

c# - WPF 中是否有 BitBlt 的等价物?

c# - 如何解决试图在 C# 中除以零?

c# - SQL 查询错误 - "Column cannot be null"