0
votes

I use Azure CLI to export backup the databases in my resource group to the blobstorage, so i want to use same command on visual studio code with c#.

For example, I use the following command in Azure CLI to export the DB's in my resource group:

az sql db export -s (sql server) -n (database) -g (group) -p (password)
  -u login --storage-key " key "
  --storage-key-type
  --storage-uri (url blob)

How can I achieve this using a C# code instead?

2
Welcome to SO! It's much better if you share your non-working code as a minimal reproducible example so that the proposed solution actually applies to your problem and can help future visitors with the same problem. Otherwise, there's a high likelihood that people will be tossing out random solutions that might not help you (or anyone else) much. See How to Ask and take the tour for more information on how to ask an on-topic question - Standin.Wolf

2 Answers

0
votes

You can use the Azure Management REST API. Here is a primer for getting started using C#.

In terms of the SQL Export, here is an script example that you can port to C#. All credit to the Microsoft employee that wrote the blog!

# Sign in to Azure.
Login-AzureRmAccount
# If your Azure account is on a non-public cloud, make sure to specify the proper environment
# example for the German cloud:
# Login-AzureRmAccount -EnvironmentName AzureGermanCloud

# Fill in your subscription and SQL Database details
$subscriptionId = "11111111-aaaa-bbbb-cccc-222222222222"
$resourceGroup = "yourresourcegroup"
$server = "yourserver"
$database = "yourdatabase"
$sqlAdminLogin = "sqladmin"
# $sqlPassword = "yourpassword"
# may break if your password contains characters used by PowerShell, e.g. the $ sign
# instead setting it directly in request body further below

# Generate a unique filename for the BACPAC
$bacpacFilename = $database + (Get-Date).ToString("yyyy-MM-dd-HH-mm") + ".bacpac"
# Fill in your storage account and container name
$baseStorageUri = "https://yourstorageaccount.blob.core.windows.net/yourcontainer/"
# Compose the storage URI
$storageUri = $baseStorageUri + $bacpacFilename
# Fill in your storage access key
$storageKey= "mDyvvJ...yourstoragekey...tnlXIosA=="

# If you have multiple subscriptions, uncomment and set to the subscription you want to work with:
# Set-AzureRmContext -SubscriptionId $subscriptionId

# This is the Tenant ID from the account that created your AAD app:
$tenantId = "99999999-zzzz-yyyy-xxxx-888888888888"
# This is the Application ID from your AAD app:
$clientId = "54c45a1a-5c1a-40ad-88b6-a37e82223eda"
# This is the Secret from your AAD app:
$key = "yoursecret"

# Acquire the authentication context
$authUrl = "https://login.windows.net/${tenantId}"
$authContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]$authUrl
$cred = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential $clientId,$key
$authresult = $authContext.AcquireToken("https://management.core.windows.net/",$cred)

# Fill in the request header with authentication and content type
$authHeader = @{
'Content-Type'='application/json'
'Authorization'=$authresult.CreateAuthorizationHeader()
}
# Fill in the request body with storage details and database login
$body = @{storageKeyType = 'StorageAccessKey'; `
storageKey=$storageKey; `
storageUri=$storageUri;`
administratorLogin=$sqlAdminLogin; `
administratorLoginPassword='yourpassword';`
authenticationType='SQL'`
} | ConvertTo-Json

# Compile the details for the REST URI
$restURI = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Sql/servers/$server/databases/$database/export?api-version=2014-04-01"

# Execute the REST API command
$result = Invoke-RestMethod -Uri $restURI -Method POST -Headers $authHeader -Body $body

Write-Output $result
0
votes

Azure provides us the whole c# code sample.

Azure SQL sample for managing import/export SQL Database -

  • Create a SQL Server with one database from a pre-existing sample.
  • Create a storage account and export a database
  • Create a new database from a backup using the import functionality
  • Update an empty database with a backup database using the import functionality
  • Delete storage account, databases and SQL Server

This sample can give you more helps.

You can get the sample here: Getting started with importing and exporting SQL databases (C#)

Hope this helps.