Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Programmatically Attach/Detach SQL Server

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RP - 26 Sep 2007 19:57 GMT
I am using SQL Server 2005 with C# 2005. I want to programmatically
Detach a database, then copy its files from the data folder to another
location and then Attach the database back. Can it be done using C# ?
If yes, please illustrate.
Alberto Poblacion - 26 Sep 2007 20:33 GMT
>I am using SQL Server 2005 with C# 2005. I want to programmatically
> Detach a database, then copy its files from the data folder to another
> location and then Attach the database back. Can it be done using C# ?

  Yes, you can do that by openning a SqlConnection to another database
(such as "master"), and sending a call to the system stored procedure
sp_detach_db. You can do this by means of the method ExecuteNonQuery() of a
SqlCommand object. Note that the detach will fail if the database is in use.
   After copying the database files (System.IO.File.Copy(...)), you
reattach the database with a call to sp_attach_db, or better yet, CREATE
DATABASE database_name FOR ATTACH.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.