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!

Check to see if DB is already Attached

Status
Not open for further replies.

DroopyA

Programmer
Aug 5, 2003
52
0
0
US
How can I tell if a Database is already attached to SQL/MSDE or not?

I'm creating a new install for a custom aplication that uses SQL as the database and I'm trying to figure out the best way to attach the database. One way is to attach it durring the install... another option is to have the application check to see if the database is attached and if it's not attach it when it runs.

How can I tell if MyDatabase.mdf has already been attached to SQL Server/MSDE or not? Is it better to attach it durring the run of the .exe or should I attach the database durring the install?
 
You can look at master..sysaltfiles but I think that doesn't always contain all the databases.

Looping through all the databaes in sysdatabases and looking at sysfiles in those databases should do it.

======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
Thank you! I guess I'm better off attaching it durring the install. Looping through all of the databases is far too much work to be done every time you launch my program.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top