Last active
April 26, 2017 20:56
-
-
Save JefClaes/c25e90d01b5a376e4e0a to your computer and use it in GitHub Desktop.
Force create LocalDB database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var conn = new SqlConnection(@"Data Source=(LocalDb)\v11.0;Initial Catalog=Master;Integrated Security=True")) | |
{ | |
conn.Open(); | |
var cmd = new SqlCommand(); | |
cmd.Connection = conn; | |
cmd.CommandText = string.Format(@" | |
IF EXISTS(SELECT * FROM sys.databases WHERE name='{0}') | |
BEGIN | |
ALTER DATABASE [{0}] | |
SET SINGLE_USER | |
WITH ROLLBACK IMMEDIATE | |
DROP DATABASE [{0}] | |
END | |
DECLARE @FILENAME AS VARCHAR(255) | |
SET @FILENAME = CONVERT(VARCHAR(255), SERVERPROPERTY('instancedefaultdatapath')) + '{0}'; | |
EXEC ('CREATE DATABASE [{0}] ON PRIMARY | |
(NAME = [{0}], | |
FILENAME =''' + @FILENAME + ''', | |
SIZE = 25MB, | |
MAXSIZE = 50MB, | |
FILEGROWTH = 5MB )')", | |
databaseName); | |
cmd.ExecuteNonQuery(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment