如何以编程方式在基本版中创建 sql 数据库?在 Windows 蔚蓝
How to create a sql database in Basic edition programmatically? in Windows Azure
语法解释如下:
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;
}
}
我到底应该把基本字符串放在哪里,因为我不确定放在哪里。
您在数据库名称后指定版本:
SqlCommand cmd2 = new SqlCommand(string.Format("CREATE DATABASE [{0:S}] (SERVICE_OBJECTIVE = 'basic');", databaseName), conn);
可以找到语法的文档here
语法解释如下:
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;
}
}
我到底应该把基本字符串放在哪里,因为我不确定放在哪里。
您在数据库名称后指定版本:
SqlCommand cmd2 = new SqlCommand(string.Format("CREATE DATABASE [{0:S}] (SERVICE_OBJECTIVE = 'basic');", databaseName), conn);
可以找到语法的文档here