You are here: detach database file from local SQL Server Express instance by C#? « MsSQL Server « IT news, forums, messages
detach database file from local SQL Server Express instance by C#?

Posted by sg71.cherub@gmail.com on 01/30/08 12:09

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!

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация