2

I want to detach the database from SQL Server 2005 from my C# code. I use the DROP query to detach. But it statements delete the file from my local system. I want to detach the database and copy that database in runtime.

5 Answers 5

3

First try to connect to the SQL Server without providing any database name,or path just like below:

ConnectionString = @"Data Source= YourDataSource ;Integrated Security=True;Connect Timeout=30";

Note:
YourDataSource can either be . or .\SQLEXPRESS or .\MSSQLSERVER or (local)\SQLEXPRESS or (localdb)\v11.0 or etc.

Then by using the following query, detach your database.

"ALTER DATABASE [your DB] SET OFFLINE WITH ROLLBACK IMMEDIATE \n\r exec sp_detach_db @dbname = [your DB]";

Ok.

My Sample code:

sql_connect1.ConnectionString = @"Data Source=.\sqlexpress;Integrated Security=True;Connect Timeout=30";
sql_command.CommandText = "ALTER DATABASE [IRAN] SET OFFLINE WITH ROLLBACK IMMEDIATE \n\r exec sp_detach_db @dbname = [IRAN]";
                  sql_command.Connection = sql_connect1;
                  sql_connect1.Open();
                  sql_command.ExecuteNonQuery();
                  sql_connect1.Close(); 
Sign up to request clarification or add additional context in comments.

Comments

2

The SQL Server SMO API let's you do anything Sql Server management studio can do (from c# code). Check out this link http://msdn.microsoft.com/en-us/library/ms162175.aspx

2 Comments

I have tested Backing Up and Restoring Database From your link above. Backing up is successful, but restoring can't do. It shows me error "Restoring failed to my server". What do i need to do?
Hi All, I've solve this problem. I use SMO Library and I use this code. Server myserver = new Server(). myserver.DetachDatabase("Database",true).
1

you can detach a database on SqlServer by the following code: There is a stored procedure in SqlServedr 'sp_detach_db' for detach a database, has one argument DataBaseName. Here in the code 'MyDatabase' is the database name you should change it with your database name

// C# Code

SqlConnection conn = new SqlConnection("Server=(local); Data Source=;Integrated Security=SSPI");
SqlCommand cmd = new SqlCommand("", conn);

cmd.CommandText = "sys.sp_detach_db MyDatabase";

conn.Open();

cmd.ExecuteNonQuery();

cmd.Dispose();
conn.Dispose();

2 Comments

This does not work always. I get an exception "Cannot detach the database 'TestDatabase' because it is currently in use."
So, before detach it, you have to break all connections, this happens when we detach database through Sql Management Studio, a check named "Close existing connections" is there. You can find code to click on button named "Sql"(on top of the window)
0

You can use stored procedures for your problem. following link can be useful:

http://msdn.microsoft.com/en-us/library/aa259611.aspx

Comments

0

Myo Thu's comments lead me in the right direction, so here's a summary of steps on how to detach the database.

Step 1: Reference the following DLL's in your project [Reference]:

Microsoft.SqlServer.ConnectionInfo.dll

Microsoft.SqlServer.Smo.dll

Microsoft.SqlServer.Management.Sdk.Sfc.dll

Microsoft.SqlServer.SqlEnum.dll

Step 2: using:

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

Step 3: Code

var server = new Server(new ServerConnection(@"MyMachine\SQL2012"));
// Ensure database is not in use
server.KillAllProcesses("TestDatabase");
server.DetachDatabase("TestDatabase", true);

EDIT: Now documented in my blog here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.