Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created an SDI application with DB support & working with an MS SQL SERVER 2000 DB.

How can I detach a named DB using CRecordset or CDatabase or Windows API functions ?

Help me please.
Posted
Updated 14-Feb-12 5:17am
v2

1 solution

You do this from CDatabase.

First, open your database using a connect string. This will vary according to the ODBC drivers you may have loaded. You can use the ODBCAdmin to create a DSN, by Calling CDatabase::Open with a NULL connect string.

Then use the CDatabase::ExecuteSQL to send an SQL detach statement. ie: "sp_detach_db 'mydb'"
 
Share this answer
 
Comments
Mr. Tomay 15-Feb-12 5:35am    
Do you mean this ?:
CDatabase MyDatabase;
MyDatabase.Open(...);
...
...
MyDatabase.ExecuteSQL(_T("sp_detach_db MyDB.mdf"));
JackDingler 15-Feb-12 10:53am    
Don't include the file name. You just want the internal database name.

Note that the files will not be deleted.
Mr. Tomay 25-Sep-12 22:18pm    
Ah Like this:
CDatabase MyDatabase;
MyDatabase.Open(...);
...
...
MyDatabase.ExecuteSQL(_T("sp_detach_db MyDB"));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900