Exporting Azure Database from C #
My C # program works with Azure Database.
I am using the Microsoft.Rest and Microsoft.Azure.Management libraries to do some things (copy, manage, delete, etc.).
I'm trying to export to Azure DB, but I can't seem to find how to do it in C #. Does anyone know how I can do this, or direct me to an example?
source to share
I found a solution to my problem: I had to update my Microsoft.Azure.Management.Sql library. Now I can use this export method:
public static export ImportExportResponse (these are IDatabasesOperations operations, string resourceGroupName, string serverName, string databaseName, ExportRequest parameters);
source to share
Based on my understanding, you are talking about Azure SQL Database. I assumed you can Export Azure SQL Database to BACPAC file .
I'm trying to export to Azure DB, but I can't seem to find how to do it in C #.
As per your description, I checked Microsoft Azure Management Libraries and found that you can refer to the following piece of code to export your sql sql database to azure blob storage:
CertificateCloudCredentials credential = new CertificateCloudCredentials("{subscriptionId}","{managementCertificate}");
var sqlManagement = new SqlManagementClient(credential);
var result = sqlManagement.Dac.Export("{serverName}", new DacExportParameters()
{
BlobCredentials = new DacExportParameters.BlobCredentialsParameter()
{
StorageAccessKey = "{storage-account-accesskey}",
Uri = new Uri("https://{storage-accountname}.blob.core.windows.net/{container-name}")
},
ConnectionInfo = new DacExportParameters.ConnectionInfoParameter()
{
DatabaseName = "{dbname}",
ServerName = "{serverName}.database.windows.net",
UserName = "{username}",
Password = "{password}"
}
});
And you can use sqlManagement.Dac.GetStatus
to get the status of the export operation.
In addition, Microsoft Azure Management Libraries use Export Database (classic) , for the new resource based REST API you can refer here . Alternatively, you can refer to create a storage account and use Microsoft Azure Storage Explorer for an easy way to manage storage resources. For more details, you can refer to here .
source to share
You can use Microsoft.Azure.Management.Fluent
to export your database to a file .bacpac
and save it to BLOB .bacpac
. For this, there are several things you need to do.
- Create an AZAD (Azure Active Directory) application and a service principal that can access resources. Follow this link for a complete guide.
- In the first step, you will need the Application (Client) ID, Client Secret and Client ID.
- Install
Microsoft.Azure.Management.Fluent
NuGet packages and import namespacesMicrosoft.Azure.Management.Fluent
,Microsoft.Azure.Management.ResourceManager.Fluent
andMicrosoft.Azure.Management.ResourceManager.Fluent.Authentication
. - Replace the placeholders in the code snippets below with the appropriate values โโfor your use case.
-
Enjoy!
var principalClientID = "<Applicaiton (Client) ID>"; var principalClientSecret = "<ClientSecret>"; var principalTenantID = "<TenantID>"; var sqlServerName = "<SQL Server Name> (without '.database.windows.net'>"; var sqlServerResourceGroupName = "<SQL Server Resource Group>"; var databaseName = "<Database Name>"; var databaseLogin = "<Database Login>"; var databasePassword = "<Database Password>"; var storageResourceGroupName = "<Storage Resource Group>"; var storageName = "<Storage Account>"; var storageBlobName = "<Storage Blob Name>"; var bacpacFileName = "myBackup.bacpac"; var credentials = new AzureCredentialsFactory().FromServicePrincipal(principalClientID, principalClientSecret, principalTenantID, AzureEnvironment.AzureGlobalCloud); var azure = await Azure.Authenticate(credentials).WithDefaultSubscriptionAsync(); var storageAccount = await azure.StorageAccounts.GetByResourceGroupAsync(storageResourceGroupName, storageName); var sqlServer = await azure.SqlServers.GetByResourceGroupAsync(sqlServerResourceGroupName, sqlServerName); var database = await sqlServer.Databases.GetAsync(databaseName); await database.ExportTo(storageAccount, storageBlobName, bacpacFileName) .WithSqlAdministratorLoginAndPassword(databaseLogin, databasePassword) .ExecuteAsync();
source to share
You can use Microsoft.Azure.Management.Fluent
to export your database to a file .bacpac
and save it to BLOB .bacpac
. For this, there are several things you need to do. Refer to this answer for more information.
source to share