TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Soumya Surendran
NA
170
0
C# code for detaching a sql server database
Oct 14 2010 12:58 AM
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 =
"Data Source=.\\SQLEXPRESS;AttachDbFilename="
+
GlobalClass
.app.ToString()+
"\\dbRDAgent.mdf;Integrated Security=True;Connect Timeout=30;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 =
GlobalClass
.app.ToString()+
"\\dbRDAgent.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?
Reply
Answers (
3
)
Reminder
Iterating through a resource file