0

I made a C# winform project in visual studio for a school exam and i want to share it with my teacher, but when i open it in another pc it doesn't work. The main problem is that the database path is set to the documents folder and i dont know how to set the database to the project folder to make the project shareable. Thanks in advance!

Now its looks like this:

SqlConnection Con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=\\Mac\Home\Documents\Raktarkeszletdb.mdf;Integrated Security=True;Connect Timeout=30");       
Josef
  • 2,869
  • 2
  • 22
  • 23
Morpheus
  • 1
  • 2

1 Answers1

0

Instead of

SqlConnection Con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=\\Mac\Home\Documents\Raktarkeszletdb.mdf;Integrated Security=True;Connect Timeout=30");   

You need to write something like this:

var projectFolder = AppDomain.CurrentDomain.BaseDirectory;
var connectionString = string.Format(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename={0}\Raktarkeszletdb.mdf;Integrated Security=True;Connect Timeout=30", projectFolder);   
var con = new SqlConnection(connectionString);

See also best way to get application folder path.

John Wu
  • 50,556
  • 8
  • 44
  • 80