protected virtual void ExecuteSqlFile(string path, string connectionString, string databaseName) { var statements = new List <string>(); using (var stream = System.IO.File.OpenRead(UrlHelpers.MapPath(path))) using (var reader = new StreamReader(stream)) { string statement; while ((statement = ReadNextStatementFromStream(reader)) != null) { statements.Add(statement); } } var builder = new SqlConnectionStringBuilder(connectionString); //now create connection string to 'master' dabatase. It always exists. builder.InitialCatalog = "Hrm_" + databaseName; foreach (string stmt in statements) { using (var conn = new SqlConnection(builder.ToString())) { conn.Open(); using (var command = new SqlCommand(stmt, conn)) { command.ExecuteNonQuery(); } } } }