如何使用C#代码的azure cli命令导出数据库
我使用Azure CLI将我的资源组中的数据库导出到blobstorage,因此我希望在visual studio代码上使用与c#相同的命令 例如,我在Azure CLI中使用以下命令导出我的资源组中的数据库:如何使用C#代码的azure cli命令导出数据库,c#,azure,visual-studio-code,azure-sql-database,azure-cli,C#,Azure,Visual Studio Code,Azure Sql Database,Azure Cli,我使用Azure CLI将我的资源组中的数据库导出到blobstorage,因此我希望在visual studio代码上使用与c#相同的命令 例如,我在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 (u
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代码来实现这一点?您可以使用Azure管理REST API。用于开始使用C# 在SQL导出方面。所有这一切都归功于撰写博客的微软员工
# 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
希望这有帮助。欢迎使用SO!如果您将不工作的代码作为一个最小的可复制示例共享,这样建议的解决方案实际上适用于您的问题,并且可以帮助将来的访问者解决相同的问题,这会更好。否则,人们很可能会抛出可能对你(或其他人)帮助不大的随机解决方案。有关如何提问主题问题的更多信息,请参阅如何提问和参加本教程