请问是我还是sqlclient和.net core使用了不同的连接字符串方式?
旧客户端示例:
var connectionString = "Data Source=SQLSERV1\\SQLSERV1;Initial Catalog=mydb;User ID=myuser; Password=mypass;";
using (SqlConnection conn = new SqlConnection(connectionString))
{
....etc
代码运行良好,此 SQL 客户端查询返回结果。
现在在 .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"]));
当我尝试在浏览器中导航到URL时,出现以下情况:未处理的异常处理请求时发生了SqlException:在建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 无法找到服务器或服务器无法访问。请验证实例名称是否正确以及是否配置了SQL Server以允许远程连接。 (提供程序:SQL Network Interfaces,错误:26-找不到服务器/指定的实例错误)。那么为什么sqlclient可以正常看到服务器,但.NET Core却不能呢?这是它处理字符串的方式吗,即它位于域上的事实?
Server=servername; initial catalog=db;user id=dbuser;password=password;MultipleActiveResultSets=True;App=EntityFramework
- Ahmar