如何在 c# 中为 SQL Azure 启用内部 Azure 服务

How do enable internal Azure Services for SQL Azure in c#

如何启用 允许的服务:WINDOWS AZURE SERVICES 如在 c# 管理门户中看到的那样?

        _client = new SqlManagementClient(GetSubscriptionCredentials());

        var result = _client.Servers.CreateAsync(new ServerCreateParameters
        {
            AdministratorUserName = _config.ServerUserName,
            AdministratorPassword = _config.ServerPassword,
            Location = _config.Location,
            Version = "12.0"
        }, CancellationToken.None).Result;

        var sqlServerName = result.ServerName;

        // This will go away once we can enable the Azure internal firewall settings == Yes
        var ipAddress = _firewallManagement.GetPublicIP();
        var firewall = _client.FirewallRules.Create(sqlServerName, new FirewallRuleCreateParameters("Server", ipAddress, ipAddress));

只需添加 0.0.0.0 作为 start_ip_address 和 end_ip_address 就像下面的 T-SQL 到 sys.firewall_rules

exec sp_set_firewall_rule N'MicrosoftServices','0.0.0.0','0.0.0.0'

不要介意 0.0.0.0 范围,SQLAzure 知道它仅适用于您订阅中的 Azure IP。

select * from sys.firewall_rules

id  name    start_ip_address    end_ip_address  create_date modify_date
7   MicrosoftService    0.0.0.0 0.0.0.0 2015-07-29 13:34:55.790 2015-07-29 13:34:55.790

Azure SQL 数据库防火墙

When an application from Azure attempts to connect to your database server, the firewall verifies that Azure connections are allowed. A firewall setting with starting and ending address equal to 0.0.0.0 indicates these connections are allowed.

https://msdn.microsoft.com/en-us/library/azure/ee621782.aspx#ConnectingFromAzure

添加和删除 SQL Azure 防火墙规则编程

http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/adding-and-deleting-sql-azure-firewall-rules-programmatically/

public void AddFirewallRule(FirewallRule rule)
        {
            using (SqlConnection conn = new SqlConnection(this.ConnectionString))
            using (SqlCommand cmd = conn.CreateCommand())
            {
                conn.Open();
                cmd.CommandText = "sp_set_firewall_rule";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = rule.Name;
                cmd.Parameters.Add("@start_ip_address", SqlDbType.VarChar).Value = rule.startIPAddress.ToString();
                cmd.Parameters.Add("@end_ip_address", SqlDbType.VarChar).Value = rule.endIPAdress.ToString();
                cmd.ExecuteNonQuery();
            }
        }

https://azure.microsoft.com/en-gb/documentation/articles/sql-database-client-library/ 有最新的方法