-3

我是 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。

4

1 回答 1

-1

我完全同意 DJ KRAZE。使用您拥有的任何服务器管理器创建它大约需要两秒钟。

于 2013-06-18T18:44:25.137 回答