在 Visual Studio 2010 数据库项目中编写服务器对象脚本

发布于 2024-11-15 17:23:30 字数 229 浏览 4 评论 0原文

预部署脚本 (Script.PreDeployment.sql) 是放置创建服务器级对象(特别是我的情况下的登录)的 sql 命令的最佳位置吗?

我需要为项目中的数据库用户创建服务器登录名,这似乎是放置它们的唯一模糊逻辑位置,因为项目结构中的其他所有内容(通过反转我们的开发数据库创建)都是特定于数据库的。因此,我只想检查其他人在使用 Visual Studio 数据库项目时存储服务器级命令的位置。

非常感谢。

Is the Pre-Deployment script (Script.PreDeployment.sql) the best place to put sql commands that create server level objects (specifically logins in my case)?

I need to create server logins for the database users in the project and this seems the only vaguely logical place to put them as everything else in the project structure (created from reversing our dev db) is database specific. I therefore just wanted to check where others store their server level commands when working with Visual Studio Database Projects.

Many Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木格 2024-11-22 17:23:30

部署数据库之前所需的任何内容都将位于该脚本中。

在我们的示例中,我们在部署后脚本中添加登录名。当我们部署数据库时不需要它们,我们决定将所有与安全相关的命令放在同一个地方。

IF NOT EXISTS (SELECT * FROM sys.server_principals WHERE name = N'$(DefaultLogin)')
BEGIN
    BEGIN TRY
        CREATE LOGIN [$(DefaultLogin)] FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english]
    END TRY
    BEGIN CATCH
        -- A try-catch is needed in case a user with a different name is created for the LOGIN specified.
    END CATCH
END
GO

IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'$(DefaultLogin)')
BEGIN
    BEGIN TRY
        CREATE USER [$(DefaultLogin)] FOR LOGIN [$(DefaultLogin)] WITH DEFAULT_SCHEMA=[dbo]
        -- The db_owner role is added to the current database.
        EXEC sp_addrolemember N'db_owner', N'$(DefaultLogin)'
    END TRY
    BEGIN CATCH
        -- A try-catch is needed in case a user with a different name is created for the LOGIN specified.
    END CATCH
END

此处是部署前和部署后参考。

Anything you need prior to deploying your database would be in that script.

In our case, we add logins in the post deployment script. They are not needed when we deploy the database, and we decided to put all the security related commands in the same place.

IF NOT EXISTS (SELECT * FROM sys.server_principals WHERE name = N'$(DefaultLogin)')
BEGIN
    BEGIN TRY
        CREATE LOGIN [$(DefaultLogin)] FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english]
    END TRY
    BEGIN CATCH
        -- A try-catch is needed in case a user with a different name is created for the LOGIN specified.
    END CATCH
END
GO

IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'$(DefaultLogin)')
BEGIN
    BEGIN TRY
        CREATE USER [$(DefaultLogin)] FOR LOGIN [$(DefaultLogin)] WITH DEFAULT_SCHEMA=[dbo]
        -- The db_owner role is added to the current database.
        EXEC sp_addrolemember N'db_owner', N'$(DefaultLogin)'
    END TRY
    BEGIN CATCH
        -- A try-catch is needed in case a user with a different name is created for the LOGIN specified.
    END CATCH
END

Here is the Pre and Post deployment reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文