-1

Problem statement

I have copied my local database (.mdf file) from one project into another. When I try to connect to the copied local database, it says it does not exist and it doesn't even show the database name.


Some background

I have made an ASP.NET and ADO.NET app in my previous project. And now, I need that same database that was used (with the data intact and all) in the previous project to be in a WCF project, which is another project.


Images of the problem

Image 1: Setup

enter image description here

Image 2: The error (when I click on "Test Connection")

enter image description here


As you can see, the database does exist in my current project (first image) but Visual Studio does not find it. What is wrong here?

Compiler v2
  • 3,509
  • 10
  • 31
  • 55

2 Answers2

0

Do you have Installed localdb??

Try to remember that your mdf has to be opened with the same sql version that you have installed. If you want to read directly with code try

string connection = "Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=DIR\MYFILE.mdf;Integrated Security=True";
string consult "";
                using (SqlConnection conn = new SqlConnection(connection))
                {
                    conn.Open();
                    using (SqlCommand mycommand = new SqlCommand(consult, conn))
                    {
                        mycommand.ExecuteNonQuery();
                    }
                }

Now if you want to add de ADO, try to put add in your project an ADO.net entitity data model. and you will follow the steps.

hope you find useful.

AbbathCL
  • 79
  • 6
  • I just made it some idea how to do it with System.Data.SqlClient. doing using doesn't need to open when is already disposable, etc. – AbbathCL Jul 28 '20 at 01:11
0

I found that my folder, where my project and solution are located where for some reason placed in the wrong directory.

With that said, I couldn't find the .mdf file to connect to because I was looking at the wrong area of my computer. Now that I moved my folder to the correct location, I was able to access the .mdf file and connect to it.

This solved my issue.

Compiler v2
  • 3,509
  • 10
  • 31
  • 55