0

This is my connection string:

<add name="EntityContext" 
     connectionString="data source=.\SQLEXPRESS; 
                       AttachDbFileName=|DataDirectory|\PasLockProduction.mdf;
                       Database=PasLockProduction;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"
     providerName="System.Data.SqlClient" />

I get this error:

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. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Why - do you get an error? If so: **what** error?? – marc_s Mar 13 '18 at 12:25
  • 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. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)' – Muhammad Hassan Mar 13 '18 at 12:27
  • Please **do not** put code samples, sample data or error messages into comments - since you cannot format it, it's **extremely hard** to read it.... Instead: **update** your question by editing it to provide that additional information! Thank you. – marc_s Mar 13 '18 at 12:28
  • You're connection to a `.\SQLEXPRESS` instance - are you **sure** you have installed SQL Server **Express** with that default instance name?? – marc_s Mar 13 '18 at 12:32
  • When you run `SELECT @@VERSION` against your SQL Server instance - what output do you get? – marc_s Mar 13 '18 at 12:36
  • If you use the command prompt and do this what happens? `sqlcmd -S .\sqlexpress` . Are you sure you have it installed and you're not confusing it with LocalDB? – Crowcoder Mar 13 '18 at 12:50
  • Microsoft SQL Server 2014 - 12.0.2000.8 (X64) Feb 20 2014 20:04:26 Copyright (c) Microsoft Corporation Standard Edition (64-bit) on Windows NT 6.3 (Build 16299: ) @marc_s – Muhammad Hassan Mar 13 '18 at 12:55
  • You have installed a **Standard** edition of SQL Server - but that feature of using `AttachDbFileName=.....` is available ***only*** with SQL Server **Express** edition – marc_s Mar 13 '18 at 12:56
  • ohhh i see..now what i do ?? any other solution ?? – Muhammad Hassan Mar 13 '18 at 12:58
  • i want just run my application with data base on client machine..my database created in sqlserver..any other way to do it ? – Muhammad Hassan Mar 13 '18 at 13:00
  • you need LocalDB installed if you want to use an MDF file without installing SQL Server. Search for LocalDB in google there is plenty to find there – GuidoG Mar 13 '18 at 14:18

2 Answers2

0

Try someting like this.

 <connectionStrings>
    <add name="EntityContext" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=c:\folder\PasLockProduction.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";/>
  </connectionStrings>
Joana Brandão
  • 171
  • 1
  • 7
0

Most likely your DataDirectory is not what you think it is. In the Interactive window or even in your code where you can set a breakpoint do this to see where that directory is:

var ddir = AppDomain.CurrentDomain.GetData("DataDirectory");

To set the directory to where your database file is :

AppDomain.CurrentDomain.SetData("DataDirectory", "<path to file...");
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Crowcoder
  • 11,250
  • 3
  • 36
  • 45