简体   繁体   中英

.net SqlClient and .NET CORE Web API connection string

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 ie the fact its on a domain?

Maybe it's a syntax problem:

Data Source=Server\\Instance

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

Data Source=Server

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