Search code examples
c#databasevisual-studiopathmdf

Setting The Path of an db mdf file in local folder


How come The path I inserted in my Database Context is not working? Here's the code for my path

private string dbPath = @"TEST.MDF"
DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath);

But when I run a query this gives me an error

An attempt to attach an auto-named database for file TEST.MDF failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

And This is how My Folder looks like this

enter image description here

The mdf file is in the same location of my cs source code but the thing is they are not reading the path correctly.

my idea for this is that when I transfer to a diffrent pc I don't have to set up the paths again and again. are there any fix for this?


Solution

  • What about

    private string dbPath = Application.StartupPath + "\\TEST.MDF";
    

    But your Test.mdf isn't in the correct directory. Move it into \bin\Debug for this code to work.