How to Attach and Detach a Database

Sometimes you want to transfer or backup your database in SQL Server. When the database is attached you cannot copy/move it to any location. All you have to do is detach it first using SQL Server Management Tools.

Another reason why I write this tutorial is to teach you so that when we finish the Library System project you will be able to switch from my own database to your own database just in case you want to differentiate each of the table.

To attach a database

1. Open SQL Server Management Tools.

2. Right click Database and click Attach.

Attach a Database

3. Click Add.

Add Button Attach

4. Locate the database and click OK button.

Locate Database

5. Click OK button again.

OK Button Attach

That’s it! The database is now attached to using SQL Server Management Tools.

To detach a database

1. Open SQL Server Management Tools.

2. Right click the database that you’d like to detach, point your mouse to Tasks menu and click Detach.

Detach a Database

3. Click OK button.

OK Button Detach

That’s it, again! :)

If you encounter an error after you click the OK button, check if the database is not in use.

Back to Visual Basic .NET 2008 Tutorial.

Comments

sample program. vb6 and sql server 2008. diko kasi makuha connection string :) unlike poh sa .net ang dali. sir kuha ko na pla ung looping tru dtpicker wherein it skips saturday and sunday :)

In reply to by ced

SqlConnection conn = new SqlConnection("Server=IT-2;Data Source=;Integrated Security=SSPI"); SqlCommand cmd = new SqlCommand("", conn); cmd.CommandText = "ALTER DATABASE PreInspection SET OFFLINE"; conn.Open(); cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Dispose();

Add new comment