创建Azure SQL Server,无需使用Microsoft.Azure.Management.Sql.Fluent。

huangapple go评论91阅读模式
英文:

Creating an Azure SQL Server without Microsoft.Azure.Management.Sql.Fluent

问题

  1. 我想通过编程方式创建Azure SQL Server,我在网上找到了一段示例代码
  2. static void CreateServer()
  3. {
  4. // 创建一个SQL数据库管理客户端
  5. SqlManagementClient sqlClient = new SqlManagementClient(new TokenCloudCredentials(subscriptionId, token.AccessToken));
  6. // 创建服务器
  7. ServerCreateOrUpdateParameters serverParameters = new ServerCreateOrUpdateParameters()
  8. {
  9. Location = location,
  10. Properties = new ServerCreateOrUpdateProperties()
  11. {
  12. AdministratorLogin = administratorLogin,
  13. AdministratorLoginPassword = administratorPassword,
  14. Version = serverVersion
  15. }
  16. };
  17. var serverResult = sqlClient.Servers.CreateOrUpdate(resourceGroupName, serverName, serverParameters);
  18. }
  19. ...但我后来发现我需要的包已被弃用。我应该使用Azure.ResourceManager.Sql而不是Microsoft.Azure.Management.Sql.Fluent
  20. 但是我找不到像上面那样清晰的示例,使用新的包来创建新的Azure SQL服务器。这是我非常不熟悉的领域,所以我看不懂这些定义。有人能提供一个与上面示例一样简洁的代码片段,使用新的包来实现相同的功能吗?
  21. 非常感谢。
英文:

I want to programmatically create an Azure SQL Server and I found an example piece of code online

  1. static void CreateServer()
  2. {
  3. // Create a SQL Database management client
  4. SqlManagementClient sqlClient = new SqlManagementClient(new TokenCloudCredentials(subscriptionId, token.AccessToken));
  5. // Create a server
  6. ServerCreateOrUpdateParameters serverParameters = new ServerCreateOrUpdateParameters()
  7. {
  8. Location = location,
  9. Properties = new ServerCreateOrUpdateProperties()
  10. {
  11. AdministratorLogin = administratorLogin,
  12. AdministratorLoginPassword = administratorPassword,
  13. Version = serverVersion
  14. }
  15. };
  16. var serverResult = sqlClient.Servers.CreateOrUpdate(resourceGroupName, serverName, serverParameters);
  17. }

... 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:

  1. static async Task Main(string[] args)
  2. {
  3. string tenant_Id = "TenantId";
  4. string clnt_Id = "ClientId";
  5. string clnt_Secrt = "Secret";
  6. string subscrip_Id = "SubscriptionID";
  7. string resource_GrpName = "Resource Group";
  8. string server_Name = "myservertest4411";
  9. string admin_Login = "Rajesh";
  10. string admin_Pwd = "Password";
  11. string authenticationUrl = $"https://login.microsoftonline.com/{tenant_Id}/oauth2/token";
  12. string requestBody = $"grant_type=client_credentials&client_id={clnt_Id}&client_secret={clnt_Secrt}&resource=https://management.azure.com/";
  13. var content = new StringContent(requestBody, Encoding.UTF8, "application/x-www-form-urlencoded");
  14. using (var httpClient = new HttpClient())
  15. {
  16. var res = await httpClient.PostAsync(authenticationUrl, content);
  17. string res_Content = await res.Content.ReadAsStringAsync();
  18. string access_Token = JsonConvert.DeserializeObject<dynamic>(res_Content).access_token;
  19. 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";
  20. string server_RequestBody = "{{ \"location\": \"eastus\", \"properties\": {{ \"administratorLogin\": \"{admin_Login}\", \"administratorLoginPassword\": \"{admin_Pwd}\", \"version\": \"12.0\" }} }}";
  21. var server_Req = new HttpRequestMessage(HttpMethod.Put, create_ServerUrl);
  22. server_Req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", access_Token);
  23. server_Req.Content = new StringContent(server_RequestBody, Encoding.UTF8, "application/json");
  24. var server_Resp = await httpClient.SendAsync(server_Req);
  25. if (server_Resp.IsSuccessStatusCode)
  26. {
  27. Console.WriteLine("SQL Server created successfully!");
  28. }
  29. else
  30. {
  31. string errorResponseContent = await server_Resp.Content.ReadAsStringAsync();
  32. Console.WriteLine($"Failed to create SQL Server. Error: {errorResponseContent}");
  33. }
  34. }
  35. }

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

  1. static async Task Main(string[] args)
  2. {
  3. string tenant_Id = &quot;TenantId&quot;;
  4. string clnt_Id = &quot;ClientId&quot;;
  5. string clnt_Secrt = &quot;Secret&quot;;
  6. string subscrip_Id = &quot;SubscriptionID&quot;;
  7. string resource_GrpName = &quot;Resource Group&quot;;
  8. string server_Name = &quot;myservertest4411&quot;;
  9. string admin_Login = &quot;Rajesh&quot;;
  10. string admin_Pwd = &quot;Password&quot;;
  11. string authenticationUrl = $&quot;https://login.microsoftonline.com/{tenant_Id}/oauth2/token&quot;;
  12. string requestBody = $&quot;grant_type=client_credentials&amp;client_id={clnt_Id}&amp;client_secret={clnt_Secrt}&amp;resource=https://management.azure.com/&quot;;
  13. var content = new StringContent(requestBody, Encoding.UTF8, &quot;application/x-www-form-urlencoded&quot;);
  14. using (var httpClient = new HttpClient())
  15. {
  16. var res = await httpClient.PostAsync(authenticationUrl, content);
  17. string res_Content = await res.Content.ReadAsStringAsync();
  18. string access_Token = JsonConvert.DeserializeObject&lt;dynamic&gt;(res_Content).access_token;
  19. string create_ServerUrl = $&quot;https://management.azure.com/subscriptions/{subscrip_Id}/resourceGroups/{resource_GrpName}/providers/Microsoft.Sql/servers/{server_Name}?api-version=2021-02-01-preview&quot;;
  20. string server_RequestBody = $&quot;{{ \&quot;location\&quot;: \&quot;eastus\&quot;, \&quot;properties\&quot;: {{ \&quot;administratorLogin\&quot;: \&quot;{admin_Login}\&quot;, \&quot;administratorLoginPassword\&quot;: \&quot;{admin_Pwd}\&quot;, \&quot;version\&quot;: \&quot;12.0\&quot; }} }}&quot;;
  21. var server_Req = new HttpRequestMessage(HttpMethod.Put, create_ServerUrl);
  22. server_Req.Headers.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;, access_Token);
  23. server_Req.Content = new StringContent(server_RequestBody, Encoding.UTF8, &quot;application/json&quot;);
  24. var server_Resp = await httpClient.SendAsync(server_Req);
  25. if (server_Resp.IsSuccessStatusCode)
  26. {
  27. Console.WriteLine(&quot;SQL Server created successfully!&quot;);
  28. }
  29. else
  30. {
  31. string errorResponseContent = await server_Resp.Content.ReadAsStringAsync();
  32. Console.WriteLine($&quot;Failed to create SQL Server. Error: {errorResponseContent}&quot;);
  33. }
  34. }
  35. }

创建Azure SQL Server,无需使用Microsoft.Azure.Management.Sql.Fluent。

In azure you can see the SQL server created.

创建Azure SQL Server,无需使用Microsoft.Azure.Management.Sql.Fluent。

  • You need a create an app in the App registrations of Azure.
    创建Azure SQL Server,无需使用Microsoft.Azure.Management.Sql.Fluent。

  • I have given the owner permissions to the app, depending on your requirement you can choose the permissions.

创建Azure SQL Server,无需使用Microsoft.Azure.Management.Sql.Fluent。

huangapple
  • 本文由 发表于 2023年5月11日 03:23:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定