-2

New to using SQL Server inside of Visual Studio.

I am trying to create a portable program that will read in a CSV file and insert the data into a database.

I have created the database and added the .mdf file to my solution. However I cannot for the life of me figure out how to create a SqlConnection to said database.

The error I get is:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.

The database is named testdb.mdf and here is a snippet of the code I have tried:

openCon = new SqlConnection();
openCon.ConnectionString = "Data Source=(LocalDB)\v11.0;" +
                           "AttachDbFilename=" +
                            AppDomain.CurrentDomain.BaseDirectory + 
                           "testdb.mdf;" +
                           "Integrated Security=True";
openCon.Open();

I still receive the error after having tried the "duplicate post solution":

openCon.ConnectionString = "Data Source=.\\SQLEXPRESS;" +
                           "AttachDbFilename=" + AppDomain.CurrentDomain.BaseDirectory + "Encounter.mdf;" +
                           "Integrated Security=True";
8
  • 2
    Whats the error you are having? Commented Aug 3, 2017 at 19:11
  • Possible duplicate of How do I connect to an MDF database file? Commented Aug 3, 2017 at 19:16
  • A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. Commented Aug 3, 2017 at 19:17
  • Do a simple google search on C# connection string in App.Config Commented Aug 3, 2017 at 19:21
  • See Database ConnectionStrings Commented Aug 3, 2017 at 19:23

1 Answer 1

1

Figured it out. I was being an idiot and not escaping the '\' before the v11.0.

Thanks all!

openCon = new SqlConnection("Data Source=(LocalDB)\\v11.0;" +
                                       "AttachDbFilename=" + AppDomain.CurrentDomain.BaseDirectory + "Encounter.mdf;" +
                                       "Integrated Security=True");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.