简体   繁体   中英

I want to create a connection string with .mdf file in window forms application

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)

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>

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...");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM