Leo
Leo

Reputation: 31

Impossible to DETACH database (from c#)

I can ATTACH database and copy all the data from one database to another.But on the end i can't detach it using same code.

     var connection = new SQLiteConnection(connection)
     connection.Open();
     sqlAttachCommand = "ATTACH database '" + fileLoc + "' AS toMerge";
     var cmd= new SQLiteCommand(sqlAttachCommand);
     cmd.Connection = connection;
     cmd.ExecuteNonQuery();
     ...
     sqlAttachCommand = "DETACH database '" + fileLoc + "'";   

The exception is:

SQL logic error or missing database no such database: C:\temp\database.db".

This is strange because I did attach for it and I did copy of all the data from this database.

Upvotes: 1

Views: 371

Answers (1)

rene
rene

Reputation: 42444

The DETACH DATABASE command takes a schema-name as parameter, not a database file.

So your detach command should be like this:

sqlAttachCommand = "DETACH database toMerge";   

as in your ATTACH command you named the thing toMerge.

In the SQL Exception the missing database isn't referring to the fact that SQLite lost that file, it is mere trying to tell that you're using a schema-name that doesn't exist.

Upvotes: 1

Related Questions