Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
‘How to create an Azure SQL Database programmatically’ with less frustration
/****************************** Module Header ******************************\
* DOWNLOADED FROM https://code.msdn.microsoft.com/How-to-create-an-Azure-SQL-dbd0bf6a
* Modified By: Adam Fisher (https://github.com/therightstuff)
* Module Name: Program.cs
* Project: CSCreateAzureSQL
* Copyright (c) Microsoft Corporation.
*
* This sample will show how to create a SQL database on Microsoft Azure
* using C#
*
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/en-us/openness/licenses.aspx#MPL
* All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using Microsoft.Azure;
using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Sql;
using Microsoft.Azure.Management.Sql.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
namespace CreateAzureSQLdatabaseCS
{
class Program
{
// AAD -> app registration -> NATIVE app
/* Azure Subscription ID */
private static string azureSubscriptionId = "106515a2-0e9e-41ec-a724-62931cab80b6";
/* Location of server and location for database (eg. Australia East) */
private static string location = "Central US";
/* Database Edition (eg. Standard)*/
private static string edition = "Basic";
/* Name of Service Object (eg. S0) */
private static string requestedServiceObjectName = null;// or "Basic";
/* Name of Resource Group containing SQL Server */
private static string resourceGroupName = "ResourceGroupName";
/* Name of SQL Server ServerName ONLY from ServerName.database.windows.net */
private static string serverName = "ServerName";
/* Name of Database */
private static string databaseName = "CodeCreatedDB1";
/* Tenant ID or AAD domain */
private static string domainName = "mydirectory.onmicrosoft.com";
/* Authentication variables from Azure Active Directory (AAD) */
/* Active Directory Client ID (app id) */
private static string applicationId = "1b312893-e746-4b06-bbe7-94e15acb6f82";
/* Active Directory Client ID (object id) */
private static string objectId = "5b64b990-72f9-49d8-bc48-6b3cfddac469";
// who can tell if this will change?
private static string clientId = applicationId;
/* redirect URI defined on Azure Active Directory App Registration */
private static string redirectUri = "http://myredirecturi";
/* redirect URI */
private static string clientAppUri = redirectUri;
static void Main(string[] args)
{
var token = GetAccessToken();
SqlManagementClient client = new SqlManagementClient(new TokenCloudCredentials(azureSubscriptionId, token.AccessToken));
DatabaseCreateOrUpdateParameters databaseParameters = new DatabaseCreateOrUpdateParameters()
{
Location = location,
Properties = new DatabaseCreateOrUpdateProperties()
{
Edition = edition,
RequestedServiceObjectiveName = requestedServiceObjectName,
}
};
var dbResponse = client.Databases.CreateOrUpdate(resourceGroupName, serverName, databaseName, databaseParameters);
Console.WriteLine("Database {0} created with status code: {1}. Service Objective: {2} ", dbResponse.Database.Name, dbResponse.StatusCode, dbResponse.Database.Properties.ServiceObjective);
}
/// <summary>
/// Prompts for user credentials when first run or if the cached credentials have expired.
/// </summary>
/// <returns>The access token from AAD</returns>
private static AuthenticationResult GetAccessToken()
{
/* valid login urls:
login.microsoftonline.com
login.windows.net
*/
AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/" + domainName);
AuthenticationResult token = authContext.AcquireToken
("https://management.azure.com/"/* the Azure Resource Management endpoint */,
clientId, new Uri(clientAppUri),
PromptBehavior.Auto /* with Auto user will not be prompted if an unexpired token is cached */);
return token;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.