c# - Why do I get "Database already exists" when I'm using "AttachDbFileName" when I move the solution to another directory? -
i have application jigsaw uses database topscores.mdf not included in project. want make application find database in application's folder. example if move .exe file , database file debug folder desktop application should use database desktop , not search in debug folder.
if let them in debug folder there no problem , connection database established, when put them on desktop :
unhandled exception has occurred in application. if click continue, application ignore error , attempt continue. database 'd:\programing\projects visual studio 2010\jigsaw\jigsaw\bin\debug\topscores.mdf' exists. choose different database name. cannot attach file 'c:\users\addy\desktop\jigsaw\topscores.mdf' database topscores.mdf
my connection string :
string apppath = path.getdirectoryname(application.executablepath); string connstring = @"server =.\sqlexpress; database=topscores.mdf; trusted_connection=true; attachdbfilename= "+@apppath+@"\topscores.mdf"; conn = new sqlconnection(connstring); sorry bad english :(
this happening because don't leverage user instance=true; when connect it. literally told sql server attach database running sql instance directory first loaded from.
detach database hand running sql instance, change connection string use user instance=true;, run debug folder, , run desktop, , you'll see success.
Comments
Post a Comment