Click here to Skip to main content
14,330,747 members
Rate this:
Please Sign up or sign in to vote.
I'm working on deploying my software using Advanced Installer and I want to attach the .mdf file located in the [APPDIR] (client pc's destination folder). I came across this connection string:

<prelang="vb">Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;

This will attach with no problem when my app runs but, after its done, it will pop up an error "database mydbfile already exists, choose a diferent name" every other time the app is opened. In the description it says: "If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection." But its not happening as its trying to attach it every single time.

What I have tried:

It only works the first time, after that it will try to attach it again (it will not detect that its already there) and fail, giving me the error and the connection will not work.
Posted
Updated 4-Oct-19 20:17pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
AttachDBFilename is unique to SQL Express, it spins up a user instance of SQL Express attached to a specific DB Filename for single user mode. Database is simply the name of the database to use, it has no additional connotation. For any production server, you would most likely not be using AttachDBFilename. It is strictly useful for development and experimentation in single-user mode.


So, use a different connection string if it's already "attached".

AttachDBFileName option in Connection String[^]
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100