简体   繁体   English

无法使用C#应用程序的登录名和密码访问sql数据库

[英]Unable to access sql database with login and password, C# application

I'm writing Windows Service in C# and inside of it I'm unable to connect to the database. 我正在用C#编写Windows服务,并且在其中无法连接数据库。 This is the question I've asked recently: SqlException: Cannot open database "path\\db.mdf" requested by the login. 这是我最近问过的问题:SqlException:无法打开登录请求的数据库“ path \\ db.mdf”。 The login failed. 登录失败。 Login failed for user 'NT AUTHORITY\\SYSTEM' 用户“ NT AUTHORITY \\ SYSTEM”的登录失败

What I've done so far: 到目前为止,我所做的是:

1) In server properties set flag "Identity verification with Sql Server and Windows" 2) Created login for sql server. 1)在服务器属性中,设置标志“使用Sql Server和Windows进行身份验证”。2)为sql服务器创建登录名。 Added user to the database with that login. 使用该登录名将用户添加到数据库。 3) Added all available roles to this login. 3)在此登录名中添加了所有可用角色。

Part 1: If I connect to the server using Sql Server 2012 Identity Verification and than trying to attach database that is in some folder I get: 第1部分:如果我使用Sql Server 2012身份验证连接到服务器,而不是尝试附加某个文件夹中的数据库,则会得到:

CREATE FILE encountered operating system error 5(Access is denied.) 
while attempting to open or create the physical file 'path\db.mdf'. 
(Microsoft SQL Server, error: 5123)

But I can attach the database if it's in this folder: 但是,如果数据库位于此文件夹中,则可以附加它:

C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA

That's in SSMS. 在SSMS中。

Part 2: What do I get when trying to attach database from Visual Studio 2012 : 第2部分:尝试从Visual Studio 2012附加数据库时,我会得到什么:

The attempt to attach to the database failed with the following information:  
Login failed for user test_user

If I use this connection string in my code: 如果我在代码中使用此连接字符串:

<add name="ConnectionName"
         connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;
     provider=System.Data.SqlClient;provider connection string='data source=(LocalDB)\v11.0;
     attachdbfilename=&quot;path\db.mdf&quot;;
     connect timeout=30;
     User Id=test_user;Password=1;
     MultipleActiveResultSets=True;
     App=EntityFramework'"
         providerName="System.Data.EntityClient" />

I get from my Windows Service app (written in C#) the following exception: 我从Windows服务应用程序(用C#编写)得到以下异常:

EntityException: The underlying provider failed on open. 

Inner excetion: SqlException: Login failed for user test_user.

I'm sure I typed the login properly. 我确定我正确键入了登录名。 I get this error whether the database is in MSSQL\\DATA folder or not. 无论数据库是否位于MSSQL \\ DATA文件夹中,我都会收到此错误。

There is something wrong with the permissions to access some folders on my computer, but I don't know why, I've given this user all the permissions! 访问我的计算机上某些文件夹的权限有问题,但是我不知道为什么,我已授予该用户所有权限!

Also, one time after computer restart the windows service started properly and was able to connect to the database, but after some time it lost this ability. 另外,在计算机重新启动一次后,Windows服务便正常启动并能够连接到数据库,但是一段时间后,它失去了此功能。 And I don't know why. 而且我不知道为什么。

Please help, I'm struggling with this issue for the last 4 days. 请帮助,过去4天我一直在努力解决此问题。 Thanks! 谢谢!

If you are writing an application you have to decide how to handle security. 如果要编写应用程序,则必须决定如何处理安全性。

There are two solutions for security in SQL Server. SQL Server有两种安全性解决方案。

1 - AD Security - Is SQL Server going to handle it via AD? 1-AD安全-SQL Server是否要通过AD处理它? the make sure the connection string uses trusted security. 确保连接字符串使用受信任的安全性。 Make sure to add the employees AD account as a login/user and set the default database to the application one, not master. 确保将雇员AD帐户添加为登录名/用户,并将默认数据库设置为应用程序一个,而不是主数据库。 See my article on crafting databases. 请参阅我有关构建数据库的文章。

http://craftydba.com/?p=656 http://craftydba.com/?p=656

2 - Standard Security - If the application is going to handle it, you need to use standard security. 2-标准安全性-如果应用程序要处理它,则需要使用标准安全性。 Create an application account (login/user). 创建一个应用程序帐户(登录/用户)。

Next, create an internal user table with different security levels. 接下来,创建一个具有不同安全级别的内部用户表。 You will want to use application roles to enforce the different security levels. 您将需要使用应用程序角色来实施不同的安全级别。

Overall, you might want to look into using schema to separate and secure database objects. 总体而言,您可能希望研究使用模式来分离和保护数据库对象。

http://craftydba.com/?p=1532 http://craftydba.com/?p=1532

It looks like you have some reading and home work to do ... 看来您需要阅读和做家庭作业...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM