简体   繁体   English

SQL Server数据库附加类型

[英]SQL Server Database attaching types

Please see the pic. 请看图片。 I am attaching DBs using following code. 我正在使用以下代码附加数据库。

Dim conn As New SqlConnection("Server=MyHomeServer\SQLExpress;Database=master;Integrated Security=SSPI")
    Dim cmd As New SqlCommand("", conn)
    cmd.CommandText = "CREATE DATABASE dbNoPWD ON ( FILENAME = 'd:\dbNoPWD.mdf' ), ( FILENAME = 'd:\dbNoPWD_log.ldf' ) FOR ATTACH"
    conn.Open()
    cmd.ExecuteNonQuery()
    cmd.Dispose()
    conn.Dispose()

Please note I donot want to give any username and password. 请注意,我不想提供任何用户名和密码。

When I ran the above code and then checked SSMS, I found my attach DB was not checked in Roles (pls. see pic.) 当我运行上面的代码然后检查SSMS时,我发现我的Attach DB没有在Roles中检查(请参见图片)。
在此处输入图片说明 The problem with this is my network computers can not access this DB. 问题是我的网络计算机无法访问该数据库。 I want to run some code like above (without sa password) and want that all my network computers can access the DB without my user get involved in setting up SSMS. 我想运行上述类似的代码(没有sa密码),并希望我的所有网络计算机都可以访问DB,而无需用户参与设置SSMS。

In your connection string when creating the database you're using windows authentication. 创建数据库时,在连接字符串中使用的是Windows身份验证。 Therefore if your ActiveDirector account is JOE then the owner of the dbNoPWD database will be JOE, not SA. 因此,如果您的ActiveDirector帐户是JOE,则dbNoPWD数据库的所有者将是JOE,而不是SA。 That's why your screenshot shows SA with no mapping to the dbNoPWD. 这就是为什么您的屏幕截图显示SA而不映射到dbNoPWD的原因。

But here's your problem: SA's mappings have nothing to do with whether or not other SqlServer logins will be able to access your database (unless they belong to the sysadmin server role). 但这是您的问题:SA的映射与其他SqlServer登录名是否能够访问您的数据库无关(除非它们属于sysadmin服务器角色)。 Each SQLServer login must have it's own mapping to your new database or else they won't be able to use it. 每个SQLServer登录名都必须具有自己的映射到您的新数据库,否则他们将无法使用它。

So, let's solve your problem. 因此,让我们解决您的问题。 If you want people to have read/write access to your new database add some TSQL after your database creation statements to map users to your new database. 如果希望人们对新数据库具有读/写访问权限,请在数据库创建语句后添加一些TSQL,以将用户映射到新数据库。 Something like this: 像这样:

use dbNoPWD
go
exec sp_addrolemember db_datareader, SQLLOGINNAME
go
exec sp_addrolemember db_datawriter, SQLLOGINNAME
go

If you have a lot of users an easier way it to create logins for entire windows groups and then just assign mapping for the entire group to your database. 如果您有很多用户,则可以使用一种更简单的方法来为整个Windows组创建登录名,然后将整个组的映射分配给数据库。

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

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