3

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?

7
  • 1
    It's in the same location as your *.cs file - but your executable is not in this directory! When you build&run, your executable is most likely built in bin\debug - and in that directory, there's no test.mdf file....... Commented Feb 24, 2012 at 5:31
  • soo I have to use? the /../../Test.MDF? Commented Feb 24, 2012 at 5:36
  • No - you should attach your MDF file to the SQL Server instance on your computer, give it a logical name and connect to it using that logical name instead of fiddilng around with the .MDF file and its location ..... Commented Feb 24, 2012 at 5:37
  • but what if I am going to a different computer? then working on it there? and I am trying to give considerations if I am going to install this application somewhere Commented Feb 24, 2012 at 5:38
  • 1
    Then you should be using something like SQL Server Compact Edition which is a simple, single-file database - no installation necessary ever.... Commented Feb 24, 2012 at 6:20

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What does Application.StartUpPath do?
It returns the folder location of your executable. So say your .EXE is in "C:\Programming\Projects\C#\testProject1\bin\Debug". That's the path it returns. And the reason we have to add "\\Test.mdf" is to add the filename to that path, so that the full path looks like: C:\Programming\Projects\C#\testProject1\bin\Debug\Test.mdf
0

Better you add your .mdf file in your project.. Add Existing Item=>Choose .mdf file from folder. After adding .mdf file in project, in Web.config or App.Config file connection string will be generate automatically and you can use that connection string to boot your store. Now as you build your project new .mdf file get copy into /bin/dubug folder and you dont need to write single line of code to connect your .mdf file.

Comments

0

Use this connection string :

SqlConnection connect = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename="+ AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.Length - 10)+"sampleDatabase.mdf;Integrated Security=True");

Because bin\Debug\ string length = 10, this is why we subtract 10; and now you can get the solution path address and can connect the MDF database.

Comments

-1

It works for me in C# with Visual Studio 2017 when I use |DataDirectory|.

You need to change your MDF database to the following:

SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\students.mdf;Integrated Security=True");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.