好的,首先,您不需要关闭连接。USE [DatabaseName] 将切换上下文,您将在数据库上。
以下是以编程方式创建数据库和登录的脚本。但是,第一次运行时,系统必须使用您的 Windows 凭据来创建数据库。后续运行可以通过新的数据库用户执行。
USE [master]
SET QUOTED_IDENTIFIER ON
GO
--1. Create Database
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'YourDatabase')
BEGIN
ALTER DATABASE [YourDatabase] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE [YourDatabase]
END
/* Create Marketplace_v1_1 database. */
USE [master]
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE DATABASE "YourDatabase"
GO
USE [master]
GO
ALTER DATABASE [YourDatabase] MODIFY FILE ( NAME = N'YourDatabase', FILEGROWTH = 10%)
GO
--2. Create Login/USer
USE [YourDatabase]
GO
IF EXISTS (SELECT * FROM sys.database_principals WHERE name = N'YourDatabaseUserName')
DROP USER [YourDatabaseUserName]
GO
USE [master]
GO
IF EXISTS (SELECT * FROM sys.server_principals WHERE name = N'YourDatabaseUserName')
DROP LOGIN [YourDatabaseUserName]
GO
CREATE LOGIN [YourDatabaseUserName]
WITH PASSWORD=N'YourPassword'
, DEFAULT_DATABASE=[YourDatabase]
, CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
USE [YourDatabase]
GO
--3. Grant Permission to your user, you can remove unecessary permissions depending on your requirements
--3.1 Permissions To read/write data to tables
CREATE USER [YourDatabaseUserName] FOR LOGIN [YourDatabaseUserName]
EXEC sp_addrolemember N'db_datareader', N'YourDatabaseUserName'
EXEC sp_addrolemember N'db_datawriter', N'YourDatabaseUserName'
GO
--3.2. Permissions to backup/restore database
EXEC master..sp_addsrvrolemember @loginame = N'YourDatabaseUserName', @rolename = N'bulkadmin'
GO
EXEC master..sp_addsrvrolemember @loginame = N'YourDatabaseUserName', @rolename = N'dbcreator'
GO
USE [YourDatabase]
GO
EXEC sp_addrolemember N'db_owner', N'YourDatabaseUserName'
GO
EXEC sys.sp_addsrvrolemember @loginame = N'YourDatabaseUserName', @rolename = N'processadmin'
GO
USE [YourDatabase]
GO
EXEC sp_addrolemember N'db_backupoperator', N'YourDatabaseUserName'
GO
--3.3. Grant permissions to operate on database objects
USE [YourDatabase]
GO
GRANT CREATE TABLE TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT AUTHENTICATE TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT BACKUP DATABASE TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT BACKUP LOG TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT CREATE FUNCTION TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT CREATE PROCEDURE TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT CREATE TYPE TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT CREATE VIEW TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT DELETE TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT EXECUTE TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT INSERT TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT SELECT TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT UPDATE TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT ALTER ON SCHEMA::dbo TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT REFERENCES TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT ALTER ANY ROLE TO [YourDatabaseUserName]
GO
USE [YourDatabase]
GO
GRANT CONTROL TO [YourDatabaseUserName]
GO