我是 SQL 新手,正在尝试使用 C# 创建数据库。这是我的代码...
private void CreateDBBtn_Click(object sender, EventArgs e)
{
String connectionString = GetConnectionString();
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
String SQLCommand = "CREATE DATABASE MyDatabase ON PRIMARY " +
"(NAME = MyDatabase_Data, " +
"FILENAME = 'D:\\MyDatabase.mdf'," +
"SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%)";
SqlCommand cmd = new SqlCommand(SQLCommand, connection);
try
{
cmd.ExecuteNonQuery();
}
catch (SqlException ae)
{
MessageBox.Show(ae.Message);
}
}
private String GetConnectionString()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = @".\SQLSERVER";
builder.AttachDBFilename = @"D:\MyDatabase.mdf";
builder.IntegratedSecurity = true;
builder.ConnectTimeout = 30;
builder.UserInstance = true;
return builder.ConnectionString;
}
但这给了我错误...
其中 D:\MyDataBase.mdf 文件在我的 PC 上的大小为 3.13 MB。