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

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

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 = &quot;TenantId&quot;;
      string clnt_Id = &quot;ClientId&quot;;
      string clnt_Secrt = &quot;Secret&quot;;
      string subscrip_Id = &quot;SubscriptionID&quot;;

       string resource_GrpName = &quot;Resource Group&quot;;
       string server_Name = &quot;myservertest4411&quot;;
       string admin_Login = &quot;Rajesh&quot;;
       string admin_Pwd = &quot;Password&quot;;

       string authenticationUrl = $&quot;https://login.microsoftonline.com/{tenant_Id}/oauth2/token&quot;;
       string requestBody = $&quot;grant_type=client_credentials&amp;client_id={clnt_Id}&amp;client_secret={clnt_Secrt}&amp;resource=https://management.azure.com/&quot;;
       var content = new StringContent(requestBody, Encoding.UTF8, &quot;application/x-www-form-urlencoded&quot;);

       using (var httpClient = new HttpClient())
            {
                var res = await httpClient.PostAsync(authenticationUrl, content);
                string res_Content = await res.Content.ReadAsStringAsync();
                string access_Token = JsonConvert.DeserializeObject&lt;dynamic&gt;(res_Content).access_token;

                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;;
                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;;
                var server_Req = new HttpRequestMessage(HttpMethod.Put, create_ServerUrl);
                server_Req.Headers.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;, access_Token);
                server_Req.Content = new StringContent(server_RequestBody, Encoding.UTF8, &quot;application/json&quot;);

                var server_Resp = await httpClient.SendAsync(server_Req);
                if (server_Resp.IsSuccessStatusCode)
                {
                    Console.WriteLine(&quot;SQL Server created successfully!&quot;);
                }
                else
                {
                    string errorResponseContent = await server_Resp.Content.ReadAsStringAsync();
                    Console.WriteLine($&quot;Failed to create SQL Server. Error: {errorResponseContent}&quot;);
                }
            }
     }

创建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:

确定