英文:
Creating an Azure SQL Server without Microsoft.Azure.Management.Sql.Fluent
问题
我想通过编程方式创建Azure SQL Server,我在网上找到了一段示例代码
static void CreateServer()
{
// 创建一个SQL数据库管理客户端
SqlManagementClient sqlClient = new SqlManagementClient(new TokenCloudCredentials(subscriptionId, token.AccessToken));
// 创建服务器
ServerCreateOrUpdateParameters serverParameters = new ServerCreateOrUpdateParameters()
{
Location = location,
Properties = new ServerCreateOrUpdateProperties()
{
AdministratorLogin = administratorLogin,
AdministratorLoginPassword = administratorPassword,
Version = serverVersion
}
};
var serverResult = sqlClient.Servers.CreateOrUpdate(resourceGroupName, serverName, serverParameters);
}
...但我后来发现我需要的包已被弃用。我应该使用Azure.ResourceManager.Sql而不是Microsoft.Azure.Management.Sql.Fluent
但是我找不到像上面那样清晰的示例,使用新的包来创建新的Azure SQL服务器。这是我非常不熟悉的领域,所以我看不懂这些定义。有人能提供一个与上面示例一样简洁的代码片段,使用新的包来实现相同的功能吗?
非常感谢。
英文:
I want to programmatically create an Azure SQL Server and I found an example piece of code online
static void CreateServer()
{
// Create a SQL Database management client
SqlManagementClient sqlClient = new SqlManagementClient(new TokenCloudCredentials(subscriptionId, token.AccessToken));
// Create a server
ServerCreateOrUpdateParameters serverParameters = new ServerCreateOrUpdateParameters()
{
Location = location,
Properties = new ServerCreateOrUpdateProperties()
{
AdministratorLogin = administratorLogin,
AdministratorLoginPassword = administratorPassword,
Version = serverVersion
}
};
var serverResult = sqlClient.Servers.CreateOrUpdate(resourceGroupName, serverName, serverParameters);
}
... but I have since found that the packages I need for this are deprecated. Instead of Microsoft.Azure.Management.Sql.Fluent I should be using Azure.ResourceManager.Sql
But I can't find any example as clear as the above to create a new Azure SQL server using the new packages. This is an area I'm very unfamiliar with so I can't see the wood for the trees and I get lost in the definitions. Can someone please provide a code snippet as minimalistic as the above to achieve the same thing using new packages?
Many thanks.
答案1
得分: 1
I have created SQL server in azure using the below C# code without Microsoft.Azure.Management.Sql.Fluent
:
static async Task Main(string[] args)
{
string tenant_Id = "TenantId";
string clnt_Id = "ClientId";
string clnt_Secrt = "Secret";
string subscrip_Id = "SubscriptionID";
string resource_GrpName = "Resource Group";
string server_Name = "myservertest4411";
string admin_Login = "Rajesh";
string admin_Pwd = "Password";
string authenticationUrl = $"https://login.microsoftonline.com/{tenant_Id}/oauth2/token";
string requestBody = $"grant_type=client_credentials&client_id={clnt_Id}&client_secret={clnt_Secrt}&resource=https://management.azure.com/";
var content = new StringContent(requestBody, Encoding.UTF8, "application/x-www-form-urlencoded");
using (var httpClient = new HttpClient())
{
var res = await httpClient.PostAsync(authenticationUrl, content);
string res_Content = await res.Content.ReadAsStringAsync();
string access_Token = JsonConvert.DeserializeObject<dynamic>(res_Content).access_token;
string create_ServerUrl = $"https://management.azure.com/subscriptions/{subscrip_Id}/resourceGroups/{resource_GrpName}/providers/Microsoft.Sql/servers/{server_Name}?api-version=2021-02-01-preview";
string server_RequestBody = "{{ \"location\": \"eastus\", \"properties\": {{ \"administratorLogin\": \"{admin_Login}\", \"administratorLoginPassword\": \"{admin_Pwd}\", \"version\": \"12.0\" }} }}";
var server_Req = new HttpRequestMessage(HttpMethod.Put, create_ServerUrl);
server_Req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", access_Token);
server_Req.Content = new StringContent(server_RequestBody, Encoding.UTF8, "application/json");
var server_Resp = await httpClient.SendAsync(server_Req);
if (server_Resp.IsSuccessStatusCode)
{
Console.WriteLine("SQL Server created successfully!");
}
else
{
string errorResponseContent = await server_Resp.Content.ReadAsStringAsync();
Console.WriteLine($"Failed to create SQL Server. Error: {errorResponseContent}");
}
}
}
In azure you can see the SQL server created.
英文:
I have created SQL server in azure using the below C# code without Microsoft.Azure.Management.Sql.Fluent
static async Task Main(string[] args)
{
string tenant_Id = "TenantId";
string clnt_Id = "ClientId";
string clnt_Secrt = "Secret";
string subscrip_Id = "SubscriptionID";
string resource_GrpName = "Resource Group";
string server_Name = "myservertest4411";
string admin_Login = "Rajesh";
string admin_Pwd = "Password";
string authenticationUrl = $"https://login.microsoftonline.com/{tenant_Id}/oauth2/token";
string requestBody = $"grant_type=client_credentials&client_id={clnt_Id}&client_secret={clnt_Secrt}&resource=https://management.azure.com/";
var content = new StringContent(requestBody, Encoding.UTF8, "application/x-www-form-urlencoded");
using (var httpClient = new HttpClient())
{
var res = await httpClient.PostAsync(authenticationUrl, content);
string res_Content = await res.Content.ReadAsStringAsync();
string access_Token = JsonConvert.DeserializeObject<dynamic>(res_Content).access_token;
string create_ServerUrl = $"https://management.azure.com/subscriptions/{subscrip_Id}/resourceGroups/{resource_GrpName}/providers/Microsoft.Sql/servers/{server_Name}?api-version=2021-02-01-preview";
string server_RequestBody = $"{{ \"location\": \"eastus\", \"properties\": {{ \"administratorLogin\": \"{admin_Login}\", \"administratorLoginPassword\": \"{admin_Pwd}\", \"version\": \"12.0\" }} }}";
var server_Req = new HttpRequestMessage(HttpMethod.Put, create_ServerUrl);
server_Req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", access_Token);
server_Req.Content = new StringContent(server_RequestBody, Encoding.UTF8, "application/json");
var server_Resp = await httpClient.SendAsync(server_Req);
if (server_Resp.IsSuccessStatusCode)
{
Console.WriteLine("SQL Server created successfully!");
}
else
{
string errorResponseContent = await server_Resp.Content.ReadAsStringAsync();
Console.WriteLine($"Failed to create SQL Server. Error: {errorResponseContent}");
}
}
}
In azure you can see the SQL server created.
-
You need a create an app in the App registrations of Azure.
-
I have given the owner permissions to the app, depending on your requirement you can choose the permissions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论