detach database file from local SQL Server Express instance by C#?



I am using the following C# code to establish a SQL connect to a SQL
database file:



// connection string

// attach a SQL database file to a local SQL server express instance

string _connectionString = @"Server=.\SQLExpress; AttachDbFilename=C:
\BalanceDatabase_1.mdf; Trusted_Connection=Yes; User Instance=True";



// using System.Data.SqlClient;

SqlConnection _sqlConnection = new SqlConnection(_connectionString);

// open the connection

_sqlConnection.Open();



// do something



// close the connection

_sqlConnection.Close();



So far, the connection works fine.



However, next, I want to copy the database file to another folder. So
the following codes:

// source database file name

string sourceDatabaseFileName = @"C:\BalanceDatabase_1.mdf";

// target database file name
string targetDatabaseFileName = @"D:\BalanceDatabase_1.mdf";

// copy database file

System.IO.File.Copy(sourceDatabaseFileName, targetDatabaseFileName,
true);



Then the program came with runtime exception: "IOException was
unhandled: The process cannot access the file 'C:
\BalanceDatabase_1.mdf' because it is being used by another process."



Is it because the database file was sill attached to the local SQL
Server express instance? What can I do to bypass this problem? Detach
the database file? or dispose the local SQL Server express instance?



Many thanks indeed!
.



Relevant Pages

  • Re: Installing SQL Server 2005 Standard and Express Editions side
    ... With SQL Express not installed and only with SQL Standard I couldn't be able ... to embed database file into VS 2005 project. ... If you install the default SQL Express ...
    (microsoft.public.sqlserver.setup)
  • Re: Creating SQL Server CE Files in an MFC project
    ... it automatically copies the database file to the target ... I am looking for some similar functionality in a native C++ MFC VS2005 ... i.e. accessing SQL Server CE from native C++ code. ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Local datastore for visual basic .net aplication on a pda
    ... I have also built a drug interaction mobile app for a major healthcare ... use SQL CE 2.0 or some other mobile rdbms ... > database file as an local datastore without the SQL server running? ...
    (microsoft.public.sqlserver.ce)
  • Re: Problem with SQLServerCE
    ... There are thousands of applications running on SQL CE, and I believe we are missing something here than a bug. ... deployed and overwriting the new and updated database file on the device. ... > SqlCeConnection connection = null; ...
    (microsoft.public.sqlserver.ce)
  • Re: Advice requested for SQLCE MDI app
    ... Having written an EBook on SQLCe, I think your approach has a lot of ... Consider that the SQLCe engine (formerly known as SQL Everywhere and ... SQL Mobile) is a full-blown SQL engine that can execute complex JOINs. ... As a strategy, I am loading a complete SQLCE database file into a DataSet, ...
    (microsoft.public.dotnet.framework.adonet)