Skip to content

Commit

Permalink
Merge pull request #76 from benjamin-hodgson/master
Browse files Browse the repository at this point in the history
Escape DB names correctly
  • Loading branch information
sethreno committed Mar 10, 2016
2 parents 548faf1 + a8df6a8 commit 99c7844
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions model/DBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand All @@ -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) {
Expand Down

0 comments on commit 99c7844

Please sign in to comment.