我是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文件的大小为3.13 MB。
答案 0 :(得分:-1)
我完全同意DJ KRAZE。使用你拥有的任何服务器管理器创建它需要大约两秒钟。