1

I am using Visual Studio 2013 C# and SQL Server 2014. I am getting an error when I try to get the database file connection string here it shows.

Failed to open a connection to the database

A network-related or instance-specific error occurred while establishing a connection to SQL server. The server was not found or accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.(provider:named pipes provider,error:40- could not open a connection to SQL Server)"

I poked around online to try to find a solution and have watched some videos too but none worked for me.

How did I try to get connection string:

  • I turn off SQL Server services
  • Then I copy the database file from C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA
  • Then I clicked on PROJECT in C# to Add new data source
  • Then database/Dataset/New connection/change
  • Then I selected Microsoft SQL Server Database File then I browsed to get database file
  • I have tried different Data source, but nothing happened
Community
  • 1
  • 1
Basit M
  • 31
  • 3
  • 2
    You turned off SQL service and still trying to connect... Wow – Giddy Naya Aug 01 '19 at 22:33
  • Then how to run a window application on another PC? – Basit M Aug 01 '19 at 22:47
  • Open a new question for that if your current problem is solved. – Giddy Naya Aug 01 '19 at 22:49
  • You meant! I can't get connection string if i turn off sql service. Okay, I understand that.If you want to publish your project in VS with database then why will you need sql service on? – Basit M Aug 01 '19 at 22:55
  • You cannot make use of your database if the service is not running. – Giddy Naya Aug 01 '19 at 22:57
  • i also tried on sql service on but same problem – Basit M Aug 01 '19 at 22:58
  • There are lots off post on sqlserver error problem. You just need to search for it using your error log. [See post particularly for your current error](https://stackoverflow.com/questions/9945409/how-do-i-fix-the-error-named-pipes-provider-error-40-could-not-open-a-connec) – Giddy Naya Aug 01 '19 at 23:01
  • Thank you so much :) it may help me. – Basit M Aug 01 '19 at 23:04
  • It looks like you may have more than one version of SQL Server installed on your PC. The database file you copies was in following folder : MSSQL12.SQLEXPRESS. Yet you said you were using SQL server 2014. Which version of the server are you using? The best way of debugging is to use SQL Server Management Studio to find out which server you are using. Then your c# connection string should use the Sever/Instance of the database in the SSMS login window. You need both the server name and the instance of the database in the c# connection string. The server also must be running. – jdweng Aug 01 '19 at 23:58
  • 1
    When you say "then I selected Microsoft SQL Server Database File then I browsed to get database file" do you mean that you're trying to get Visual Studio to open the `SomeDatabaseName.mdf` file directly? It's not like an Access database file, it can't do that. – AlwaysLearning Aug 02 '19 at 03:39
  • 1
    If you want to "free" your `.mdf` database file from a full SQL Server instance, and fiddle around with this free-floating `.mdf` on your own (**not** recommended!), then you **MUST** use SQL Server **Express** - only that edition supports these "independent" `.mdf` files - and from the looks of it, you don't have that, you're using a "full" SQL Server version (Web, Standard, Enterprise, Developer). – marc_s Aug 02 '19 at 05:01
  • I have figured it out I reinstalled windows 10 again, then I installed VS 2013 and SQL Server Express 2014 but it still got an error it says your database. mdf file must be 706 version, then again, I installed SQL Server Express 2012 so now it works fine. Thank you so much :) – Basit M Aug 06 '19 at 18:32

0 Answers0