如何使用带有C#代码的Azure CLI命令导出数据库

我使用 Azure CLI 将我的资源组中的数据库备份导出到 blobstorage ,所以我想在带有C#的 visual studio代码上使用相同的命令

例如,我在Azure CLI中使用以下命令导出资源组中的数据库:

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)

我该如何使用C#代码来实现这一目标?

zhanghaowl 回答:如何使用带有C#代码的Azure CLI命令导出数据库

您可以使用Azure管理REST API。 Here is a primer,以开始使用C#。

在SQL导出方面,here is an script example that you can port to C#。全部归功于撰写博客的Microsoft员工!

# 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
,

Azure为我们提供了整个c#代码示例。

用于管理导入/导出SQL数据库的Azure SQL示例-

  • 使用现有示例创建一个具有一个数据库的SQL Server。
  • 创建一个存储帐户并导出数据库
  • 使用导入功能从备份创建新数据库
  • 使用导入功能使用备份数据库更新空数据库 功能
  • 删除存储帐户,数据库和SQL Server

此示例可以为您提供更多帮助。

您可以在此处获取示例:Getting started with importing and exporting SQL databases (C#)

希望这会有所帮助。

本文链接:https://www.f2er.com/3164932.html

大家都在问