473,667 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Programmaticall y Attach/Detach SQL Server

RP
I am using SQL Server 2005 with C# 2005. I want to programmaticall y
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.

Sep 26 '07 #1
1 8493
"RP" <rp*********@gm ail.comwrote in message
news:11******** **************@ r29g2000hsg.goo glegroups.com.. .
>I am using SQL Server 2005 with C# 2005. I want to programmaticall y
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.

Sep 26 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
15441
by: DBA | last post by:
If I detach a database on a server, then attach the database with a single file only using the data file...will I lose any transactions that were in the original log file? There is a debate going on here at work where some people think that during the detach, transactions in the log file are saved to the data file. Thanks!
4
3826
by: ad | last post by:
I use asp.net 2.0 and SqlServer 2005 Express to develop my Web application The database file of my applicaiton is "db.mdf", I put it in "myDB" directory under the application folder. I want to attach the mdf file when application start. How can I do that?
9
6938
by: ME | last post by:
I have a application that someone else has wrote that runs in a console window. It does not take parameters, but when running it asks three questions and then exits. I would like to write a small utility to attach to the console app, send the necessary answers (typically typing the letter "y" and enter) and then leave. Is this a possiblity for .net? If so what would be the best approach? Thanks, Matt
1
2101
by: Martin Simard | last post by:
Hi all, In VS 2003, when I attach to a remote process for debugging, I can see the list of modules loaded by the process before attaching to it. This list is not there anymore in VS 2005. Instead, I just have a warning for security reasons and 2 buttons (Attach | Don't attach). Let me explain a bit... I have several web applications running on different servers. All servers are XP 2003 Servers with IIS 6. My applications runs in...
0
1214
by: Alan T | last post by:
How do I attach and detach a ContexyMenuStrip to a ListView at run time?
1
5641
by: grzegorz.gazda | last post by:
I had to detach the database to delete the transaction log as it took the hole disk space. No I am trying to attach database, however I am not able to do so. The engine is asking me whether I want to continue and create new log file. I agree to it, however then I am receiving an error: Error 1813: Could not open new database 'db_name'. CREATE DATABASE aborted. Device activation error. The physical file name 'E:\SQL
0
1369
by: aboutjav.com | last post by:
Hi, How do I attach the .mdf and .ldf with the SQL Server Management Studio Express? Do I attached all 4 files? MyWebSiteName_log.LDF MyWebSiteName.mdf ASPNETDB.MDF aspnetdb_log.ldf
9
7804
by: dpatel75 | last post by:
I am trying to copy a database from a SQL 2000 SP3 Windows 2000 server to a 2005 SP2 Windows 2003 server. I am trying to use detach and attach method (have tried both within Management Studio and T-SQL) and experience an error when attaching to the 2005 server: "CREATE FILE encountered operating system error 5 (error not found) while attempting to open or create the physical file 'xxxxxxxxxxxx' (Microsoft SQL Server, Error: 5123)" I have...
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8570
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2781
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.