0

我正在使用 Entity DbContext 作为项目数据存储。所以我想从程序中复制 .mdf 和 .ldf 。(SaveAs 命令)无法复制文件,因为我收到文件正在使用错误。

我可以分离数据库,复制文件(.mdf,.ldf),将数据库重新附加到文件。

4

1 回答 1

0

环顾四周后,其他人可能会感兴趣

    /// <summary> Detach a database from LocalDB. This MUST be done prior to deleting it. It must also be done after a inadvertent (or ill advised) manual delete.</summary>
    /// <param name="dbName">The NAME of the database, not its filename.</param>
    /// <param name="directory"></param>
    /// <param name="newname"></param>
    /// <param name="newdirectory"></param>
    /// <remarks></remarks>
    public static void CopyDatabase(string dbName, string directory, string newname = null, string newdirectory = null)
    {
        string dbfilename = null;
        // Connect to the MASTER database in order to excute the detach command on it
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder()
                                             {
                                                 DataSource = @"(LocalDB)\v11.0",
                                                 InitialCatalog = "master",
                                                 IntegratedSecurity = true
                                             };
        using (SqlConnection connection = new SqlConnection(builder.ToString()))
        {
            try
            {
                connection.Open();

                DataTable databases = connection.GetSchema("Databases");
                foreach (DataRow database in databases.Rows)
                {
                    string databaseName = database.Field<String>("database_name");
                    short dbID = database.Field<short>("dbid");
                    DateTime creationDate = database.Field<DateTime>("create_date");
                    if (databaseName.StartsWith(dbName, true, null) || databaseName.EndsWith(dbName, true, null) || databaseName.EndsWith(dbName + ".mdf", true, null))
                    {
                        dbfilename = databaseName;
                        break;
                    }

                }
                if (String.IsNullOrEmpty(dbfilename)) return;


                var cmd = connection.CreateCommand();
                // Before the database file can be detached from code the workaround below has to be applied.
                // http://gunnalag.wordpress.com/2012/02/27/fix-cannot-detach-the-database-dbname-because-it-is-currently-in-use-microsoft-sql-server-error-3703/
                //cmd.CommandText = String.Format("SELECT [name] FROM sys.databases d WHERE d.database_id > 4");

                cmd.CommandText = String.Format("ALTER DATABASE [{0}] SET OFFLINE WITH ROLLBACK IMMEDIATE", dbfilename);
                cmd.ExecuteNonQuery();
                try
                {
                    string filename = Path.Combine(directory, dbName + ".mdf");
                    File.Copy(filename, Path.Combine(newdirectory ?? directory, dbName ?? newname + ".pdk"), true);
                    filename = Path.Combine(directory, dbName + "_log.ldf");
                    File.Copy(filename, Path.Combine(newdirectory ?? directory, dbName ?? newname + "_log.lpdf"), true);
                }
                catch (Exception ex)
                {
                    Helpers.DebugLogger.Caught(ex, "CopyDatabase file copying failed");
                }
                finally
                {
                    cmd.CommandText = String.Format("ALTER DATABASE [{0}] SET ONLINE", dbfilename);
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Helpers.DebugLogger.Caught(ex, "CopyDatabase altering database failed");
            }
        }

    }
于 2013-09-16T00:09:29.403 回答