0

I created a database with VS2015 express when I use this connecion string :

Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\example\documents\visual studio 2015\Projects\materials summury\Rexnord materials summury\Database1.mdf";Integrated Security=True

The connection is working, but when I use this one :

Data Source=(LocalDB)\MSSQLLocalDB; AttachDbFilename=".\ Database1.mdf";Integrated Security=true;

I got the message : the login failed. can you help me with this relative connection ? Thanks in advance

xAminex
  • 393
  • 2
  • 6
  • 18

1 Answers1

1

ConnectionString allows for metadata like DataDirectory You can use Data Source=[DataDirectory]Database1.mdf to get the the project root. Using DataDirectory or it's equivelant symbol ~ you cannot go higher up then the the solutions folder itself.

https://msdn.microsoft.com/en-us/library/cc716756.aspx

Madmenyo
  • 8,389
  • 7
  • 52
  • 99
  • Thanks Menno Gouw, I used your solution but I got this error message : a database with the same name exists or specified file cannot be opened or it is located on UNC share. – xAminex Apr 18 '16 at 16:48
  • Your database is probably not in the `App_Data` folder. You can set the `DataDirectory` by doing `AppDomain.CurrentDomain.SetData("DataDirectory", your_path)` – Madmenyo Apr 18 '16 at 17:23
  • Thanks for you answer Menno, the program found the Database but, it seems that there are another problem this is the complet message : An attempt to attach an auto-named database for file C:\Users\example\Documents\Visual Studio 2015\Projects\materials summury\materials summury\bin\Debug\ Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. – xAminex Apr 18 '16 at 17:28
  • @xAminex Not sure about that, probably deserves it's own question but you might want to look for similar question on SO first. – Madmenyo Apr 18 '16 at 17:30
  • Thanks Menno, I did but I didn't find any thing, I'm gonna post it – xAminex Apr 18 '16 at 17:35