0

So the problem at hand is that I need to use a local database across multiple machines. On my machine it works fine, but on others it gets a directory error due to the fact that the connection directory is as seen below:

SqlConnection sqlCon = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Kenzie\source\repos\nicksinishtaj\moodAudio\DB\login_DB.mdf;Integrated Security=True;Connect Timeout=30");

Is there a way I can make this directory more versatile over many machines?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
kbeezy
  • 3
  • 1
  • Don't. LocalDB is meant only for development and testing. It's a feature of SQL Server Express so there's no reason to use it in production when you *already* have SQL Server Express installed. You'll be able to use `Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True` to connect the same way on all machines, no matter the edition – Panagiotis Kanavos Jun 11 '20 at 16:52
  • If you want multiple developers on the same project to use the same path, you'll have to store the database in your repo and use relative paths. For web projects you can already use `AttachDbFilename=|DataDirectory|\TestDb.mdf;"` to load the database from `App_Data` – Panagiotis Kanavos Jun 11 '20 at 16:54
  • As stated above, But if you want to use a standard folder for this kind of data then look at _Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)_ – Steve Jun 11 '20 at 16:55
  • 3
    Does this answer your question? [Connection string with relative path to the database file](https://stackoverflow.com/questions/1833640/connection-string-with-relative-path-to-the-database-file) – Abdul Hadi Jun 11 '20 at 16:58
  • Never use AttachDbFilename database is attached to a SQL Server. The mdf file is owned by the SQL Server and does not allow users access to the file. So simply remove the AttachDbFilename fro the connection string. – jdweng Jun 11 '20 at 17:04
  • What is the type of your project? Winforms?WPF?WebForm?Or any other type? – 大陸北方網友 Jun 12 '20 at 01:30

0 Answers0