Does SQL Server Express 2008 not separate a file attached automatically?

The MSDN documentation for SQLEXPRESS says:

When an application first establishes a connection from a running instance of SQL Server Express, SQL Server Express automatically attaches the .mdf file. When the user closes the application, SQL Server Express separates the .mdf file from the instance.

This does not seem to be happening. If I replaced the MDF file with a new name of the same name (after deleting the log file, of course), SQL Server Express will refuse to attach it.

I tried almost any combination of connection string parameters, and it drove me crazy. Any suggestions?

+3
source share
2 answers

Rollback / close occurs. If this does not happen, you cannot replace the MDF file because it will be used. The documentation you are quoting is not entirely accurate. The correct documentation is on SQL Server 2005 Express Edition User Instances :

  • Databases of the user instance are set to Auto Close parameter , so if there are no connections to the database for 8-10 minutes, the database is disconnected and the file is closed. It happens automatically, but may be required for a while, especially if the connection pool is enabled for your connection.
  • sp_detach_db . Visual Studio , , IDE .

, , , , MDF LDF ( ) .

:

  • LDF . , MDF LDF .
  • , MDF LDF. SQL Server , .
  • . SQL Express , . RANU ERRORLOG ( ), systen .
+4
    private static void DetachMdf(string dbServer, string dbName)
    {
        SqlConnection.ClearAllPools();
        using (SqlConnection conn = new SqlConnection(string.Format("Server={0};Database=master;Integrated Security=SSPI", dbServer)))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("sp_detach_db", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@dbname", dbName);
                cmd.ExecuteNonQuery();
            }
        }
    }
0

Source: https://habr.com/ru/post/1725634/


All Articles