如何以编程方式在Basic版本中创建sql数据库?在Windows Azure中

时间:2015-07-15 17:26:26

标签: c# sql azure azure-sql-database azure-elastic-scale

这里解释了这个的语法:

How to programatically create Sql Azure database of type Basic/Standard edition through Enity Framework code first

但是,我的代码实现如下:

 public static bool CreateDatabaseIfNotExists(string connectionString, string databaseName)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand(
                    string.Format("SELECT * FROM sys.databases WHERE [name]=\'{0:S}\'", databaseName),
                    conn);

                cmd.CommandTimeout = int.MaxValue;

                if (cmd.ExecuteScalar() == null)
                {
                    SqlCommand cmd2 = new SqlCommand(
                        string.Format("CREATE DATABASE [{0:S}];", databaseName),
                        conn);
                    cmd2.CommandTimeout = int.MaxValue;

                    cmd2.ExecuteNonQuery();

                    return true;
                }
                else
                    return false;
            }
        }

我应该把基本字符串放在哪里,因为我不知道放在哪里。

1 个答案:

答案 0 :(得分:5)

您可以在数据库名称后面指定版本:

SqlCommand cmd2 = new SqlCommand(string.Format("CREATE DATABASE [{0:S}] (SERVICE_OBJECTIVE = 'basic');", databaseName), conn);

可以找到语法文档here