From a8df6a8b9b89fdf21dc945e88592a88643bdfd0d Mon Sep 17 00:00:00 2001 From: Benjamin Hodgson Date: Thu, 10 Mar 2016 12:51:30 +0000 Subject: [PATCH] Escape DB names correctly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I’m trying to use SchemaZen for a database whose name contains a dot (i.e. `MyDatabase.Dev`). Presently it fails because the DB names are not being escaped (with square brackets) correctly. This fixes that. --- model/DBHelper.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/model/DBHelper.cs b/model/DBHelper.cs index 52eb0359..e63eae9f 100644 --- a/model/DBHelper.cs +++ b/model/DBHelper.cs @@ -39,13 +39,16 @@ public static void ExecBatchSql(string conn, string sql) { public static void DropDb(string conn) { var cnBuilder = new SqlConnectionStringBuilder(conn); - var dbName = cnBuilder.InitialCatalog; - if (DbExists(cnBuilder.ToString())) { + var initialCatalog = cnBuilder.InitialCatalog; + + var dbName = "[" + initialCatalog + "]"; + + if (DbExists(cnBuilder.ToString())) { cnBuilder.InitialCatalog = "master"; ExecSql(cnBuilder.ToString(), "ALTER DATABASE " + dbName + " SET SINGLE_USER WITH ROLLBACK IMMEDIATE"); ExecSql(cnBuilder.ToString(), "drop database " + dbName); - cnBuilder.InitialCatalog = dbName; + cnBuilder.InitialCatalog = initialCatalog; ClearPool(cnBuilder.ToString()); } } @@ -54,7 +57,7 @@ public static void CreateDb(string conn) { var cnBuilder = new SqlConnectionStringBuilder(conn); var dbName = cnBuilder.InitialCatalog; cnBuilder.InitialCatalog = "master"; - ExecSql(cnBuilder.ToString(), "CREATE DATABASE " + dbName); + ExecSql(cnBuilder.ToString(), "CREATE DATABASE [" + dbName + "]"); } public static bool DbExists(string conn) {