SQL Server Attach/Detach MDF

KingKaeru

Supreme [H]ardness
Joined
Jul 16, 2000
Messages
6,465
Hi Guys,

Can anyone point me in the direction of how I would go about attaching and detaching SQL Server MDF datafiles? I am trying to write a tool to do this automatically. Googling only results in information on doing it manually through the enterprise manager.

Thanks,
--KK
 
For those interested, I found a solution:

Code:
Prototype (C/C++)
HRESULT AttachDB(SQLDMO_LPCSTR DBName,
SQLDMO_LPCSTR DataFiles,
SQLDMO_LPBSTR pResult);

--KK
 
sp_attach_db and sp_detach_db are the commands to use to attach or detatch a database.

I don't know what you intend your program to do, but when it wants to attach or detach a database, it will have to issue those commands. Exactly how you issue those commands will depend on what language you're using. In C#, you might use a SqlCommand object's ExecuteNonQuery() method.

The method who's prototype you've quoted is a part of SQL-DMO, which is depricated for SQL SMO.
 
Thanks mike,

I actually found the sp_attach_db and sp_detach_db commands you mentioned in the T-SQL documentation earlier. I'm using C# right now and they seem to be doing the job perfectly :)

--KK
 
Back
Top