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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user962206
  • 15,637
  • 61
  • 177
  • 270
  • 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....... – marc_s Feb 24 '12 at 05:31
  • soo I have to use? the /../../Test.MDF? – user962206 Feb 24 '12 at 05: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 ..... – marc_s Feb 24 '12 at 05: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 – user962206 Feb 24 '12 at 05:38
  • If that computer is on the same network - just connect to your SQL Server. If it's a separate network/computer, install SQL Server Express, attach the MDF, and you're good to go – marc_s Feb 24 '12 at 05:39
  • But my problem here is that, everyday I always switch to different computers, in school, at my house, friends house, I can't install SQL Server Express in my school, only my house. – user962206 Feb 24 '12 at 05:45
  • 1
    Then you should be using something like [SQL Server Compact Edition](http://blogs.msdn.com/b/sqlservercompact/archive/2011/01/12/microsoft-sql-server-compact-4-0-is-available-for-download.aspx) which is a simple, single-file database - no installation necessary ever.... – marc_s Feb 24 '12 at 06:20

3 Answers3

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.

Jason
  • 6,878
  • 5
  • 41
  • 55
  • What does Application.StartUpPath do? – user962206 Feb 24 '12 at 05:24
  • 1
    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 – Jason Feb 24 '12 at 05:26
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.

Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Monk
  • 1
  • 2