简体   繁体   English

如何在弹性池中以编程方式创建Azure SQL数据库?

[英]How to programmatically create an Azure SQL Database in an Elastic Pool?

I want to write code in C# which would programmatically create and add an Azure SQL database into an existing elastic pool. 我想用C#编写代码,该代码将以编程方式创建Azure SQL数据库并将其添加到现有的弹性池中。 I have looked into the Elastic Database Client Library, but it does not handle creation of databases, only registering existing databases as shards, which I would definitely make use of. 我已经研究了Elastic Database Client Library,但是它不处理数据库的创建,仅将现有数据库注册为分片,我肯定会使用它。

Is it possible to create the database by using something simple like SqlClient, or maybe this can be done by using the Azure SQL Management SDK or some other option? 是否可以通过使用诸如SqlClient之类的简单方法来创建数据库,或者可以通过使用Azure SQL Management SDK或某些其他选项来完成此操作?

You can use Transact SQL to create the database and add it to an elastic pool in one statement. 您可以使用Transact SQL在一个语句中创建数据库并将其添加到弹性池中。 In this example, we are creating a new database in a pool named S3M100: 在此示例中,我们在名为S3M100的池中创建一个新数据库:

CREATE DATABASE db1 ( SERVICE_OBJECTIVE = ELASTIC_POOL ( name = S3M100 ) ) 

You can also use Transact-SQL to first create the database. 您也可以使用Transact-SQL首先创建数据库。

CREATE DATABASE YourNewDB ( EDITION = 'GeneralPurpose' );

It can be a copy of another database. 它可以是另一个数据库的副本。

CREATE DATABASE YourNewDB AS COPY OF OldDB;

After that you can move it to any elastic pool. 之后,您可以将其移动到任何弹性池。

ALTER DATABASE YourNewDB   
MODIFY ( SERVICE_OBJECTIVE = ELASTIC_POOL ( name = pool1 ) ) ;  

Please see this tutorial: Create a new elastic database pool with C# . 请参阅本教程: 使用C#创建一个新的弹性数据库池

It gives you the c# code example to Create a new database in a pool : 它为您提供了c#代码示例来在池中创建新数据库

// Create a database: configure create or update parameters and properties explicitly
DatabaseCreateOrUpdateParameters newPooledDatabaseParameters = new DatabaseCreateOrUpdateParameters()
{
    Location = currentServer.Location,
    Properties = new DatabaseCreateOrUpdateProperties()
    {
        Edition = "Standard",
        RequestedServiceObjectiveName = "ElasticPool",
        ElasticPoolName = "ElasticPool1",
        MaxSizeBytes = 268435456000, // 250 GB,
        Collation = "SQL_Latin1_General_CP1_CI_AS"
    }
};

var poolDbResponse = sqlClient.Databases.CreateOrUpdate("resourcegroup-name", "server-name", "Database2", newPooledDatabaseParameters);

If you already have Azure SQL databases, you can reference Monitor and manage an elastic database pool with C# . 如果您已经拥有Azure SQL数据库,则可以引用Monitor并使用C#管理弹性数据库池

For example, Move a database into an elastic pool : 例如, 将数据库移动到弹性池中

// Retrieve current database properties.

currentDatabase = sqlClient.Databases.Get("resourcegroup-name", "server-name", "Database1").Database;

// Configure create or update parameters with existing property values, override those to be changed.
DatabaseCreateOrUpdateParameters updatePooledDbParameters = new DatabaseCreateOrUpdateParameters()
{
    Location = currentDatabase.Location,
    Properties = new DatabaseCreateOrUpdateProperties()
    {
        Edition = "Standard",
        RequestedServiceObjectiveName = "ElasticPool",
        ElasticPoolName = "ElasticPool1",
        MaxSizeBytes = currentDatabase.Properties.MaxSizeBytes,
        Collation = currentDatabase.Properties.Collation,
    }
};

// Update the database.
var dbUpdateResponse = sqlClient.Databases.CreateOrUpdate("resourcegroup-name", "server-name", "Database1", updatePooledDbParameters);

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM