Esempio n. 1
0
        /// <summary>
        /// Creates a new sql database.
        /// </summary>
        /// <param name="databaseName">The name for the new database</param>
        /// <param name="databaseMaxSizeInGB">The maximum size of the new database</param>
        /// <param name="databaseCollation">The collation for the new database</param>
        /// <param name="databaseEdition">The edition for the new database</param>
        /// <returns>The newly created Sql Database</returns>
        public Database CreateNewDatabase(
            string databaseName,
            int?databaseMaxSizeInGB,
            string databaseCollation,
            DatabaseEdition databaseEdition,
            ServiceObjective serviceObjective)
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = this.subscription.CreateClient <SqlManagementClient>();

            this.AddTracingHeaders(sqlManagementClient);

            // Create the database
            DatabaseCreateResponse response = sqlManagementClient.Databases.Create(
                this.serverName,
                new DatabaseCreateParameters()
            {
                Name    = databaseName,
                Edition = databaseEdition != DatabaseEdition.None ?
                          databaseEdition.ToString() : DatabaseEdition.Web.ToString(),
                CollationName           = databaseCollation ?? string.Empty,
                MaximumDatabaseSizeInGB = databaseMaxSizeInGB ??
                                          (databaseEdition == DatabaseEdition.Business || databaseEdition == DatabaseEdition.Premium ? 10 : 1),
                ServiceObjectiveId = serviceObjective != null ? serviceObjective.Id.ToString() : null,
            });

            // Construct the resulting Database object
            Database database = CreateDatabaseFromResponse(response);

            return(database);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new Sql Database.
        /// </summary>
        /// <param name="databaseName">The name for the new database.</param>
        /// <param name="databaseMaxSize">The max size for the database.</param>
        /// <param name="databaseCollation">The collation for the database.</param>
        /// <param name="databaseEdition">The edition for the database.</param>
        /// <returns>The newly created Sql Database.</returns>
        public Database CreateNewDatabase(
            string databaseName,
            int?databaseMaxSize,
            string databaseCollation,
            DatabaseEdition databaseEdition)
        {
            // Create a new request Id for this operation
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Create the new entity and set its properties
            Database database = new Database();

            database.Name = databaseName;

            if (databaseMaxSize != null)
            {
                database.MaxSizeGB = (int)databaseMaxSize;
            }

            if (!string.IsNullOrEmpty(databaseCollation))
            {
                database.CollationName = databaseCollation;
            }

            if (databaseEdition != DatabaseEdition.None)
            {
                database.Edition = databaseEdition.ToString();
            }

            // Save changes
            this.AddToDatabases(database);
            try
            {
                this.SaveChanges(SaveChangesOptions.None);

                // Re-Query the database for server side updated information
                database = this.RefreshEntity(database);
                if (database == null)
                {
                    throw new ApplicationException(Resources.ErrorRefreshingDatabase);
                }
            }
            catch
            {
                this.ClearTrackedEntity(database);
                throw;
            }

            // Load the extra properties for this object.
            this.LoadExtraProperties(database);

            return(database);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a new sql database.
        /// </summary>
        /// <param name="databaseName">The name for the new database</param>
        /// <param name="databaseMaxSize">The maximum size of the new database</param>
        /// <param name="databaseCollation">The collation for the new database</param>
        /// <param name="databaseEdition">The edition for the new database</param>
        /// <returns>The newly created Sql Database</returns>
        public Database CreateNewDatabase(
            string databaseName,
            int?databaseMaxSize,
            string databaseCollation,
            DatabaseEdition databaseEdition)
        {
            this.clientRequestId = SqlDatabaseManagementHelper.GenerateClientTracingId();

            ISqlDatabaseManagement channel = GetManagementChannel();

            SqlDatabaseInput input = new SqlDatabaseInput();

            input.Name          = databaseName;
            input.CollationName = databaseCollation ?? string.Empty;

            //determine the edition
            if (databaseEdition != DatabaseEdition.None)
            {
                input.Edition = databaseEdition.ToString();
            }
            else
            {
                input.Edition = string.Empty;
            }

            //determine the maximum size
            if (databaseMaxSize.HasValue)
            {
                input.MaxSizeGB = databaseMaxSize.ToString();
            }

            //create a new database on the server
            SqlDatabaseResponse response =
                channel.EndNewDatabase(
                    channel.BeginNewDatabase(this.subscriptionId, this.serverName, input, null, null));

            Database database = CreateDatabaseFromResponse(response);

            return(database);
        }
        /// <summary>
        /// Updates the property on the database with the name <paramref name="databaseName"/>.
        /// </summary>
        /// <param name="databaseName">The database to update.</param>
        /// <param name="newDatabaseName">
        /// The new database name, or <c>null</c> to not update.
        /// </param>
        /// <param name="databaseMaxSize">
        /// The new database max size, or <c>null</c> to not update.
        /// </param>
        /// <param name="databaseEdition">
        /// The new database edition, or <c>null</c> to not update.
        /// </param>
        /// <returns>The updated database object.</returns>
        public Database UpdateDatabase(
            string databaseName,
            string newDatabaseName,
            int? databaseMaxSize,
            DatabaseEdition? databaseEdition)
        {
            // Find the database by name
            Database database = GetDatabase(databaseName);

            // Update the name if specified
            if (newDatabaseName != null)
            {
                database.Name = newDatabaseName;
            }

            // Update the max size and edition properties
            database.MaxSizeGB = databaseMaxSize;
            database.Edition = databaseEdition == null ? null : databaseEdition.ToString();

            database.IsRecursiveTriggersOn = null;

            // Mark the database object for update and submit the changes
            this.UpdateObject(database);
            try
            {
                this.SaveChanges();
            }
            catch
            {
                this.RevertChanges(database);
                throw;
            }

            return this.GetDatabase(database.Name);
        }
        /// <summary>
        /// Creates a new Sql Database.
        /// </summary>
        /// <param name="databaseName">The name for the new database.</param>
        /// <param name="databaseMaxSize">The max size for the database.</param>
        /// <param name="databaseCollation">The collation for the database.</param>
        /// <param name="databaseEdition">The edition for the database.</param>
        /// <returns>The newly created Sql Database.</returns>
        public Database CreateNewDatabase(
            string databaseName,
            int? databaseMaxSize,
            string databaseCollation,
            DatabaseEdition databaseEdition)
        {
            // Create a new request Id for this operation
            this.clientRequestId = SqlDatabaseManagementHelper.GenerateClientTracingId();

            // Create the new entity and set its properties
            Database database = new Database();
            database.Name = databaseName;

            if (databaseMaxSize != null)
            {
                database.MaxSizeGB = (int)databaseMaxSize;
            }

            if (!string.IsNullOrEmpty(databaseCollation))
            {
                database.CollationName = databaseCollation;
            }

            if (databaseEdition != DatabaseEdition.None)
            {
                database.Edition = databaseEdition.ToString();
            }

            // Save changes
            this.AddToDatabases(database);
            try
            {
                this.SaveChanges(SaveChangesOptions.None);

                // Re-Query the database for server side updated information
                database = this.RefreshEntity(database);
                if (database == null)
                {
                    throw new ApplicationException(Resources.ErrorRefreshingDatabase);
                }
            }
            catch
            {
                this.ClearTrackedEntity(database);
                throw;
            }

            // Load the extra properties for this object.
            database.LoadExtraProperties(this);

            return database;
        }
        /// <summary>
        /// Updates the property on the database with the name <paramref name="databaseName"/>.
        /// </summary>
        /// <param name="databaseName">The database to update.</param>
        /// <param name="newDatabaseName">
        /// The new database name, or <c>null</c> to not update.
        /// </param>
        /// <param name="databaseMaxSize">
        /// The new database max size, or <c>null</c> to not update.
        /// </param>
        /// <param name="databaseEdition">
        /// The new database edition, or <c>null</c> to not update.
        /// </param>
        /// <param name="serviceObjective">
        /// The new service objective, or <c>null</c> to not update.
        /// </param>
        /// <returns>The updated database object.</returns>
        public Database UpdateDatabase(
            string databaseName,
            string newDatabaseName,
            int? databaseMaxSizeGb,
            long? databaseMaxSizeBytes,
            DatabaseEdition? databaseEdition,
            ServiceObjective serviceObjective)
        {
            // Find the database by name
            Database database = GetDatabase(databaseName);

            // Update the name if specified
            if (newDatabaseName != null)
            {
                database.Name = newDatabaseName;
            }

            // Update the max size and edition properties
            if (databaseMaxSizeGb != null)
            {
                database.MaxSizeGB = (int)databaseMaxSizeGb;
            }
            if (databaseMaxSizeBytes != null)
            {
                database.MaxSizeBytes = (long)databaseMaxSizeBytes;
            }

            database.Edition = databaseEdition == null ? null : databaseEdition.ToString();

            database.IsRecursiveTriggersOn = null;

            // Update the service objective property if specified
            if (serviceObjective != null)
            {
                database.ServiceObjectiveId = serviceObjective.Id;
            }
            else
            {
                database.ServiceObjectiveId = null;
            }

            // Mark the database object for update and submit the changes
            this.UpdateObject(database);
            try
            {
                this.SaveChanges();
            }
            catch
            {
                this.RevertChanges(database);
                throw;
            }

            return this.GetDatabase(database.Name);
        }
        /// <summary>
        /// Creates a new Sql Database.
        /// </summary>
        /// <param name="databaseName">The name for the new database.</param>
        /// <param name="databaseMaxSizeGb">The max size for the database in GB.</param>
        /// <param name="databaseMaxSizeBytes">The max size for the database in bytes.</param>
        /// <param name="databaseCollation">The collation for the database.</param>
        /// <param name="databaseEdition">The edition for the database.</param>
        /// <param name="serviceObjective">The service object to assign to the database</param>
        /// <returns>The newly created Sql Database.</returns>
        public Database CreateNewDatabase(
            string databaseName,
            int?databaseMaxSizeGb,
            long?databaseMaxSizeBytes,
            string databaseCollation,
            DatabaseEdition databaseEdition,
            string serviceObjectiveName)
        {
            builder["Database"] = null;

            string commandText = "CREATE DATABASE [{0}] ";

            if (!string.IsNullOrEmpty(databaseCollation))
            {
                commandText += " COLLATE {1} ";
            }

            List <string> arguments = new List <string>();

            if (databaseMaxSizeGb != null || databaseMaxSizeBytes != null)
            {
                arguments.Add(" MAXSIZE={2} ");
            }

            if (databaseEdition != DatabaseEdition.None)
            {
                arguments.Add(" EDITION='{3}' ");
            }

            if (!string.IsNullOrEmpty(serviceObjectiveName))
            {
                arguments.Add(" SERVICE_OBJECTIVE='{4}' ");
            }

            if (arguments.Count > 0)
            {
                commandText += "(" + string.Join(", ", arguments.ToArray()) + ")";
            }

            string maxSizeVal = string.Empty;

            if (databaseMaxSizeGb != null)
            {
                maxSizeVal = databaseMaxSizeGb.Value.ToString() + "GB";
            }
            else if (databaseMaxSizeBytes != null)
            {
                if (databaseMaxSizeBytes > (500 * 1024 * 1024))
                {
                    maxSizeVal = (databaseMaxSizeBytes / (1024 * 1024 * 1024)).ToString() + "GB";
                }
                else
                {
                    maxSizeVal = (databaseMaxSizeBytes / (1024 * 1024)).ToString() + "MB";
                }
            }

            SqlCollationCheck(databaseCollation);

            commandText = string.Format(
                commandText,
                SqlEscape(databaseName),
                databaseCollation,
                SqlEscape(maxSizeVal),
                SqlEscape(databaseEdition.ToString()),
                SqlEscape(serviceObjectiveName));

            builder["Database"] = null;
            using (var connection = CreateConnection())
            {
                using (DbCommand command = connection.CreateCommand())
                {
                    command.CommandTimeout = commandTimeout;
                    command.CommandText    = commandText;
                    connection.Open();

                    command.ExecuteNonQuery();
                }
            }

            return(GetDatabase(databaseName));
        }
        /// <summary>
        /// Update a database on the server.
        /// </summary>
        /// <param name="databaseName">The name of the database to modify.</param>
        /// <param name="newDatabaseName">The new name of the database.</param>
        /// <param name="databaseMaxSizeInGB">The new maximum size of the database.</param>
        /// <param name="databaseEdition">The new edition of the database.</param>
        /// <param name="serviceObjective">The new service objective of the database.</param>
        /// <returns>The updated database.</returns>
        public Database UpdateDatabase(
            string databaseName,
            string newDatabaseName,
            int? databaseMaxSizeInGB,
            long? databaseMaxSizeInBytes,
            DatabaseEdition? databaseEdition,
            ServiceObjective serviceObjective)
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = AzureSession.ClientFactory.CreateClient<SqlManagementClient>(this.profile, subscription, AzureEnvironment.Endpoint.ServiceManagement);
            this.AddTracingHeaders(sqlManagementClient);

            // Retrieve the specified database
            DatabaseGetResponse database = sqlManagementClient.Databases.Get(
                this.serverName,
                databaseName);

            DatabaseUpdateParameters parameters = new DatabaseUpdateParameters()
            {
                Name = !string.IsNullOrEmpty(newDatabaseName) ? newDatabaseName : database.Database.Name,
                MaximumDatabaseSizeInGB = databaseMaxSizeInGB,
                MaximumDatabaseSizeInBytes = databaseMaxSizeInBytes,
            };
            parameters.Edition = (database.Database.Edition ?? string.Empty);
            if (databaseEdition.HasValue)
            {
                if (databaseEdition != DatabaseEdition.None)
                {
                    parameters.Edition = databaseEdition.ToString();
                }
            }
            parameters.ServiceObjectiveId = database.Database.ServiceObjectiveId;
            if (serviceObjective != null)
            {
                parameters.ServiceObjectiveId = serviceObjective.Id.ToString();
            }

            // Update the database with the new properties
            DatabaseUpdateResponse response = sqlManagementClient.Databases.Update(
                this.serverName,
                databaseName,
                parameters
                );

            // Construct the resulting Database object
            Database updatedDatabase = CreateDatabaseFromResponse(response);
            return updatedDatabase;
        }
        /// <summary>
        /// Creates a new sql database.
        /// </summary>
        /// <param name="databaseName">The name for the new database</param>
        /// <param name="databaseMaxSizeInGB">The maximum size of the new database</param>
        /// <param name="databaseCollation">The collation for the new database</param>
        /// <param name="databaseEdition">The edition for the new database</param>
        /// <returns>The newly created Sql Database</returns>
        public Database CreateNewDatabase(
            string databaseName,
            int? databaseMaxSizeInGB,
            long? databaseMaxSizeInBytes,
            string databaseCollation,
            DatabaseEdition databaseEdition,
            ServiceObjective serviceObjective)
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = AzureSession.ClientFactory.CreateClient<SqlManagementClient>(this.profile, subscription, AzureEnvironment.Endpoint.ServiceManagement);
            this.AddTracingHeaders(sqlManagementClient);

            DatabaseCreateParameters parameters = new DatabaseCreateParameters()
            {
                Name = databaseName,
                Edition = databaseEdition != DatabaseEdition.None ?
                    databaseEdition.ToString() : null,
                CollationName = databaseCollation ?? string.Empty,
                MaximumDatabaseSizeInGB = databaseMaxSizeInGB,
                MaximumDatabaseSizeInBytes = databaseMaxSizeInBytes,
                ServiceObjectiveId = serviceObjective != null ? serviceObjective.Id.ToString() : null,
            };

            // Create the database
            DatabaseCreateResponse response = sqlManagementClient.Databases.Create(
                this.serverName,
                parameters);

            // Construct the resulting Database object
            Database database = CreateDatabaseFromResponse(response);
            return database;
        }
        /// <summary>
        /// Update a database on the server.
        /// </summary>
        /// <param name="databaseName">The name of the database to modify.</param>
        /// <param name="newDatabaseName">The new name of the database.</param>
        /// <param name="databaseMaxSizeInGB">The new maximum size of the database.</param>
        /// <param name="databaseEdition">The new edition of the database.</param>
        /// <param name="serviceObjective">The new service objective of the database.</param>
        /// <returns>The updated database.</returns>
        public Database UpdateDatabase(
            string databaseName,
            string newDatabaseName,
            int? databaseMaxSizeInGB,
            DatabaseEdition? databaseEdition,
            ServiceObjective serviceObjective)
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = this.subscription.CreateClient<SqlManagementClient>();
            this.AddTracingHeaders(sqlManagementClient);

            // Retrieve the specified database
            DatabaseGetResponse database = sqlManagementClient.Databases.Get(
                this.serverName,
                databaseName);

            // Update the database with the new properties
            DatabaseUpdateResponse response = sqlManagementClient.Databases.Update(
                this.serverName,
                databaseName,
                new DatabaseUpdateParameters()
                {
                    Id = database.Id,
                    Name = !string.IsNullOrEmpty(newDatabaseName) ?
                        newDatabaseName : database.Name,
                    Edition = databaseEdition.HasValue && (databaseEdition != DatabaseEdition.None) ?
                        databaseEdition.ToString() : (database.Edition ?? string.Empty),
                    CollationName = database.CollationName ?? string.Empty,
                    MaximumDatabaseSizeInGB = databaseMaxSizeInGB.HasValue ?
                        databaseMaxSizeInGB.Value : database.MaximumDatabaseSizeInGB,
                    ServiceObjectiveId = serviceObjective != null ?
                        serviceObjective.Id.ToString() : null,
                });

            // Construct the resulting Database object
            Database updatedDatabase = CreateDatabaseFromResponse(response);
            return updatedDatabase;
        }
        /// <summary>
        /// Creates a new sql database.
        /// </summary>
        /// <param name="databaseName">The name for the new database</param>
        /// <param name="databaseMaxSizeInGB">The maximum size of the new database</param>
        /// <param name="databaseCollation">The collation for the new database</param>
        /// <param name="databaseEdition">The edition for the new database</param>
        /// <returns>The newly created Sql Database</returns>
        public Database CreateNewDatabase(
            string databaseName,
            int? databaseMaxSizeInGB,
            string databaseCollation,
            DatabaseEdition databaseEdition)
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = this.subscription.CreateClient<SqlManagementClient>();
            this.AddTracingHeaders(sqlManagementClient);

            // Create the database
            DatabaseCreateResponse response = sqlManagementClient.Databases.Create(
                this.serverName,
                new DatabaseCreateParameters()
                {
                    Name = databaseName,
                    Edition = databaseEdition != DatabaseEdition.None ?
                        databaseEdition.ToString() : DatabaseEdition.Web.ToString(),
                    CollationName = databaseCollation ?? string.Empty,
                    MaximumDatabaseSizeInGB = databaseMaxSizeInGB ??
                        (databaseEdition == DatabaseEdition.Business ? 10 : 1),
                });

            // Construct the resulting Database object
            Database database = CreateDatabaseFromResponse(response);
            return database;
        }
        /// <summary>
        /// Update a database on the server.
        /// </summary>
        /// <param name="databaseName">The name of the database to modify</param>
        /// <param name="newDatabaseName">The new name of the database</param>
        /// <param name="databaseMaxSize">The new maximum size of the database</param>
        /// <param name="databaseEdition">The new edition of the database</param>
        /// <returns>The updated database</returns>
        public Database UpdateDatabase(
            string databaseName,
            string newDatabaseName,
            int? databaseMaxSize,
            DatabaseEdition? databaseEdition,
            ServiceObjective serviceObjective)
        {
            this.clientRequestId = SqlDatabaseManagementHelper.GenerateClientTracingId();

            ISqlDatabaseManagement channel = GetManagementChannel();

            Database database = this.GetDatabase(databaseName);

            //make sure the database exists.
            if (database == null)
            {
                throw new Exception(
                    "Error: Result of GetDatabase() in ServerDataServiceCertAuth.UpdateDatabase() is null");
            }

            SqlDatabaseInput input = new SqlDatabaseInput();

            //Set the database ID and collation
            input.Id = database.Id.ToString();
            input.CollationName = database.CollationName;

            if (serviceObjective != null)
            {
                input.ServiceObjectiveId = serviceObjective.Id.ToString();
            }

            //Determine what the new name for the database should be
            if (!string.IsNullOrEmpty(newDatabaseName))
            {
                input.Name = newDatabaseName;
            }
            else
            {
                input.Name = database.Name;
            }

            //Determine what the new edition for the database should be
            if (databaseEdition.HasValue && (databaseEdition != DatabaseEdition.None))
            {
                input.Edition = databaseEdition.ToString();
            }
            else
            {
                input.Edition = database.Edition;
            }

            //Determine what the new maximum size for the database should be.
            if (databaseMaxSize.HasValue)
            {
                input.MaxSizeGB = databaseMaxSize.ToString();
            }
            else
            {
                input.MaxSizeGB = database.MaxSizeGB.ToString();
            }

            //Send the update request and wait for the response.
            SqlDatabaseResponse response = channel.EndUpdateDatabase(
                channel.BeginUpdateDatabase(
                    this.subscriptionId,
                    this.serverName,
                    databaseName,
                    input,
                    null,
                    null));

            //Transform the response into a database object.
            Database updatedDatabase = CreateDatabaseFromResponse(response);

            return updatedDatabase;
        }
        /// <summary>
        /// Creates a new sql database.
        /// </summary>
        /// <param name="databaseName">The name for the new database</param>
        /// <param name="databaseMaxSize">The maximum size of the new database</param>
        /// <param name="databaseCollation">The collation for the new database</param>
        /// <param name="databaseEdition">The edition for the new database</param>
        /// <returns>The newly created Sql Database</returns>
        public Database CreateNewDatabase(
            string databaseName,
            int? databaseMaxSize,
            string databaseCollation,
            DatabaseEdition databaseEdition)
        {
            this.clientRequestId = SqlDatabaseManagementHelper.GenerateClientTracingId();

            ISqlDatabaseManagement channel = GetManagementChannel();

            SqlDatabaseInput input = new SqlDatabaseInput();
            input.Name = databaseName;
            input.CollationName = databaseCollation ?? string.Empty;

            //determine the edition
            if (databaseEdition != DatabaseEdition.None)
            {
                input.Edition = databaseEdition.ToString();
            }
            else
            {
                input.Edition = string.Empty;
            }

            //determine the maximum size
            if (databaseMaxSize.HasValue)
            {
                input.MaxSizeGB = databaseMaxSize.ToString();
            }

            //create a new database on the server
            SqlDatabaseResponse response =
                channel.EndNewDatabase(
                    channel.BeginNewDatabase(this.subscriptionId, this.serverName, input, null, null));

            Database database = CreateDatabaseFromResponse(response);

            return database;
        }