3

Is it me or tis there a different way that sqlclient and .net core uses the connection string.

example old client:

var connectionString = "Data Source=SQLSERV1\\SQLSERV1;Initial Catalog=mydb;User ID=myuser; Password=mypass;";
using (SqlConnection conn = new SqlConnection(connectionString))
{
    ....etc

Code works fine, results come back from this sql client query.

Now in .NET CORE web API :

appsettings.json

"Data": {
    "DefaultConnection": {
        "ConnectionString": "Data Source=SQLSERV1\\SQLSERV1;Initial Catalog=mydb;User ID=myuser; Password=mypass;"
    }
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddApplicationInsightsTelemetry(Configuration);

    services.AddMvc()
        .AddJsonOptions(config =>
        {
            config.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        });
    services.AddDbContext<MW_MereSysContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

when i try to nav to the url in my browser i get:

An unhandled exception occurred while processing the request.SqlException: 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)

So how is it that sqlclient sees the server fine, but .net core cannot? is it the way it process the string i.e the fact its on a domain?

17
  • possible sql services stop??? Commented Dec 21, 2016 at 12:24
  • you mean the sql services in services? looks fine. Im running both a the moment to test. Commented Dec 21, 2016 at 12:27
  • sql server configuration manager -> sql server services Commented Dec 21, 2016 at 12:28
  • 1
    I used this in my project and it worked. Server=servername; initial catalog=db;user id=dbuser;password=password;MultipleActiveResultSets=True;App=EntityFramework Commented Dec 21, 2016 at 12:54
  • 2
    I wouldn't think you need to escape the backslash (\\) in the appsettings.json file. Commented Dec 21, 2016 at 12:55

1 Answer 1

0

Maybe it's a syntax problem:

Data Source=Server\\Instance

If you have a default instance, then, only use:

Data Source=Server
Sign up to request clarification or add additional context in comments.

1 Comment

Yes remove the two ‘\\’ and only use one ‘\’

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.