How to export SQL Database directly to blob storage programmatically

前端 未结 4 2046
野性不改
野性不改 2020-12-20 02:40

I need to programmatically backup/export a SQL Database (either in Azure, or a compatible-one on-prem) to Azure Storage, and restore it to another SQL Database. I would lik

4条回答
  •  旧巷少年郎
    2020-12-20 03:26

    It's similiar to the Brando's answer but this one uses a stable package:

    using Microsoft.WindowsAzure.Management.Sql;
    

    Nuget

    Using the same variables in the Brando's answer, the code will be like this:

    var azureSqlServer = "xxxxxxx"+".database.windows.net";
    var azureSqlServerName = "xxxxxxx";
    
            SqlManagementClient managementClient = new SqlManagementClient(new TokenCloudCredentials(subscriptionId, GetAccessToken(tenantId, clientId, secretKey)));
    
            var exportParams = new DacExportParameters()
            {
                BlobCredentials = new DacExportParameters.BlobCredentialsParameter()
                {
                    StorageAccessKey = storageKey,
                    Uri = new Uri(baseStorageUri)
                },
                ConnectionInfo = new DacExportParameters.ConnectionInfoParameter()
                {
                    ServerName = azureSqlServer,
                    DatabaseName = azureSqlDatabase,
                    UserName = adminLogin,
                    Password = adminPassword
                }
            };
            var exportResult = managementClient.Dac.Export(azureSqlServerName, exportParams);
    

提交回复
热议问题