public void CreateDatabase(string databaseName, Plan plan) { var sqlPlan = plan as MsSqlPlan; Debug.Assert(sqlPlan != null, "plan should be typeof(MsSqlPlan)"); var formattedDatabaseName = string.Format(databaseNameFormat, databaseName); string createDatabaseCommand; if (sqlPlan.DatabaseSize >= 0) { createDatabaseCommand = string.Format(MsSqlTemplates.CreateLimitedDatabase, formattedDatabaseName, databaseFilePath, sqlPlan.DatabaseSize); } else { createDatabaseCommand = string.Format(MsSqlTemplates.CreateDatabase, formattedDatabaseName, databaseFilePath); } using (var sqlConn = new SqlConnection(connectionString)) { using (var cmd = new SqlCommand(createDatabaseCommand, sqlConn)) { sqlConn.Open(); var codeResult = (int) cmd.ExecuteScalar(); if (codeResult == -1) { throw new InvalidOperationException(string.Format("The datbase {0} already exists.", databaseName)); } } } }
public Credentials CreateBinding(string databaseName, string bindingId, Plan plan) { var sqlPlan = plan as MsSqlPlan; Debug.Assert(sqlPlan != null, "plan should be typeof(MsSqlPlan)"); var formattedDatabaseName = string.Format(databaseNameFormat, databaseName); var userName = string.Format(databaseUserFormat, bindingId); var password = System.Web.Security.Membership.GeneratePassword(24, 4); var command = string.Format(MsSqlTemplates.CreateUserForDatabase, formattedDatabaseName, userName, password); using (var sqlConn = new SqlConnection(connectionString)) { using (var cmd = new SqlCommand(command, sqlConn)) { sqlConn.Open(); var codeResult = (int)cmd.ExecuteScalar(); if (codeResult == -1) { throw new InvalidOperationException(string.Format("The user {0} already exists for the database {1}.", userName, databaseName)); } } } return new MsSqlCredentials(userName, password, dataSource, serverPort, formattedDatabaseName); }