【问题标题】:Create Azure database in resource group programmatically using C#使用 C# 以编程方式在资源组中创建 Azure 数据库
【发布时间】:2017-12-08 11:32:30
【问题描述】:

我正在尝试使用 C# 将主数据库作为模板创建 Azure 数据库。我可以创建数据库,但找不到为新数据库指定资源组的方法。

【问题讨论】:

    标签: database azure azure-resource-group


    【解决方案1】:

    以下代码 sn -p 需要 Windows Azure 管理类库。

     如果尚未安装,请在包管理器控制台中运行以下命令

    PM> Install-Package Microsoft.WindowsAzure.Management.Libraries -Pre
    

    以下代码展示了如何使用 C# 获取云凭证订阅。

    public static string subscriptionId = "{Your-Subscription-id}"; 
    
            public static string base64EncodedCertificate = "Your-certificate-Base64-string"; 
    
            static SubscriptionCloudCredentials getCredentials() 
    
            { 
    
                return new CertificateCloudCredentials(subscriptionId, new X509Certificate2(Convert.FromBase64String(base64EncodedCertificate))); 
    
            } 
    

    以下代码展示了如何使用 C# 代码创建 SQL 数据库。

    static void Main(string[] args) 
    
          { 
    
              
    
              SqlManagementClient client = new SqlManagementClient(getCredentials()); 
    
                //Server Name is your SQL Azure DataBase server name, for example:mysqlserver001 
    
              client.Databases.Create("{Server-Name}", new Microsoft.WindowsAzure.Management.Sql.Models.DatabaseCreateParameters() 
    
              { 
    
                  Name = "SqlDBTest", 
    
                  MaximumDatabaseSizeInGB = 1, 
    
                  CollationName = "SQL_Latin1_General_CP1_CI_AS", 
    
                  Edition="Web" 
    
              }); 
    
              
    
              Console.ReadLine(); 
    
          } 
    

    【讨论】:

    • 抱歉,我看不到我在哪里指定资源组。我在 DatabaseCreateParameters 中找不到任何提及资源组
    • 另外我在函数链中找不到任何方法来设置资源组。我目前正在使用它的 Fluent 版本在资源组中创建存储帐户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 2017-07-31
    相关资源
    最近更新 更多