OK、何よりもまず、接続を閉じる必要はありません。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