Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AttachDBFileName and then detach in code 1

Status
Not open for further replies.

CP60

Programmer
Oct 16, 2008
145
DE

I can use AttachDBFileName in an ADO connection in my application to attach a database to a SQL instance.

Also in code (VB), how can I detach the mdf again in order to move it to another location or even or delete it completely?

Thanks!
 

I can use oConn.Execute "drop database MyDB" to delete it completely.
But in code, how do just disconnect from the sql instance?
 
To detach a database...


When executing this code, you should connect to the master database so things don't get weird with the connection.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
To simply disconnect, you would use the Close() method of the Connection object. This will release the connection back to the connection pool in IIS, or close the connection to SQL for other types of applications.
 
gmmastros,

Thank you for that, it was just what I was looking for!

I connected to the 'master' DB and used:

Call oConn.Execute("EXEC sp_detach_db 'W:\Temp\SQL 2014\MyDB1.mdf', 'true';")
Call oConn.Execute("EXEC sp_detach_db 'MyDB2', 'true';")


Worked perfectly!


yelworcm, that is not what I meant. Sorry for the misunderstanding and making it clearer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top