What is Original DB Name when attaching db

Hi All,  

We have an application that operates on a named instance of  MSDE.   In the instance there can be 1 -> many databases all with a standard structure.

Anyway,  when we ship a new database the MDF is sent on CD, the application then copies this to a specifed location and attaches it.    Hey presto...

However the snag is that we need to be able to attach the database as the original name of the database, simple enough however, on top of this requirement we need to effectively have the ability to display the original name of the database in an "Attach Database" dialog box (kind of like a file browser dialog which displays the original db name kind of like this:

Please select database to attach:

MyDb  (mydatabase.mdf)
TestDb (anotherdb.mdf)


I am sure that somehow retrievable from unattached MDF since in EM when you select a file to attach it displays the original db name as default in the dialog box.  We dont want to resort to shipping and further files containing this info as that will lead to other problems.

One dirty option we have which we will take if we have no other alternative to is to open the file in a binary stream and append to the end the name, then strip this off before attaching.

Any help would be appreciated.
LVL 18
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
Not familiar with MSDE, but if the command:

FROM < backup_device >

is available, you could run that within your app, capture the output, and extract the db name.  That command doesn't require you to know the db name up front.
ShogunWadeAuthor Commented:
Trouble is it's not a backp, but rather the MDF file itself.
Scott PletcherSenior DBACommented:
DOH!  Sorry about that.  Don't think I can help.

I've looked thru .MDFs on occassion and haven't see the db name in plain text that I recall.  Perhaps it's in DBCS or encoded in some way.
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

ShogunWadeAuthor Commented:
What method have you used to look into the MDF ?  A binany file viewer or something of that nature ?
Scott PletcherSenior DBACommented:
Mostly DBCC PAGE, which does do a binary print essentially, and also just using Word and Notepad.  Also tried doing a string search for the db name in the file ane couldn't find it.  

Of course, when you print using DBCC PAGE, each four bytes of data is "flipped", e.g, if the print out shows "001e0010" for the first four bytes, the actual values are "10001e00" (byte 4 -> 1, byte 3 -> 2, etc.).  Tricky to read.  Didn't verify if it's actually stored that way or just prints that way.
ShogunWadeAuthor Commented:

Ive worked it out.  

I traced EM in Profiler and this is how it is done......

dbcc checkprimaryfile (N'C:\mydb.mdf', 2)


Database name      MyDatabaseName
Database version      539
Collation      872468488

with 0 as the parameter it returns whether it's an MDF or not

and with 3 as the parameter it returns the entire file group filename and virtual name.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShogunWadeAuthor Commented:
Thanks for your input scott hope you dont mind if I reclaim points .
Scott PletcherSenior DBACommented:
No, not at all.  Glad you posted the solution, I'll try to remember that in the future.
ShogunWadeAuthor Commented:
I like to share this sort of stuff.

what I forgot to mention is that the call only works on a non attached DB.  It errors on a sharing violation if it is aleady attached.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.