0

I've tried to create a global SqlConnection with System.Configuration and the following path:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);

But, my main problem is that |DataDirectory| from app config does not get the current location from my database, so when I try to apply a command it does nothing. How can a create a connection that can work if I move the project on another PC without changing the connection strings?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • How would it know where to go for a database connection, if the details are in the place it doesnt know where it is? You would need to have it default to either full servername, or, ask for it on first run – BugFinder Apr 05 '17 at 08:20
  • where's the database? On the same PC as the application? Or on a remote server? – ADyson Apr 05 '17 at 08:21
  • @ADyson on the same PC – Altair RuS Apr 05 '17 at 08:22
  • _"problem is that |DataDirectory| from app config does not get the current location from my database"_ - it should. If it doesn't, use `AppDomain.CurrentDomain.SetData("DataDirectory", "C:\myDB")` (see duplicate) to change the data directory. See also [How can I get the application's path in a .NET console application?](http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application). – CodeCaster Apr 05 '17 at 08:24
  • 1
    Have you want to make MDF location relative to project location like this post: http://stackoverflow.com/questions/3500829/sql-express-connection-string-mdf-file-location-relative-to-application-locatio? – Tetsuya Yamamoto Apr 05 '17 at 08:25

0 Answers0