C# .NET核心可以´;无法连接到SQL数据库
我构建了一个新的.net核心Web API,并连接到SQL DB。我在将API与数据库连接时遇到问题。我尝试了一个本地数据库C# .NET核心可以´;无法连接到SQL数据库,c#,sql-server,asp.net-core,C#,Sql Server,Asp.net Core,我构建了一个新的.net核心Web API,并连接到SQL DB。我在将API与数据库连接时遇到问题。我尝试了一个本地数据库“DefaultConnection”:“Server=(localdb)\\MSSQLLocalDB;Initial Catalog=ib;Integrated Security=SSPI”,和一个远程数据库(请参阅appsettings.json) 远程:Microsoft SQL Server Express(64位)11.0.2100.60系统:Microsoft
“DefaultConnection”:“Server=(localdb)\\MSSQLLocalDB;Initial Catalog=ib;Integrated Security=SSPI”,
和一个远程数据库(请参阅appsettings.json)
远程:Microsoft SQL Server Express(64位)11.0.2100.60系统:Microsoft Windows NT 6.1(7601)
可以使用SQL Server Management Studio或Visual Studio SQL Server对象资源管理器连接到这两个数据库。我还有一个正在运行的asp.net Web应用程序,其中我使用相同的(远程)连接字符串
但是有了新的核心Web API,我无法获得连接。不使用下面的设置,也没有与NuGet的Scaffold DBContext的连接
如果你需要更多我的代码,你可以问。非常感谢你的帮助
Scaffold-DbContext "Connection String" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Startup.cs
services.AddDbContext<ibContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
核心错误(远程数据库) 错误:26-定位指定的服务器/实例时出错 脚手架错误(用于本地数据库) 错误:50-LocalDB Instanz中的给定名称无效。:“Der angegebene name Der LocalDB Instanz ist ungültig。” 脚手架错误(远程数据库) 错误:26-定位指定的服务器/实例时出错
编辑: 可能来自哪里?可以连接到远程数据库,只有Core有问题
在我的例子中,可以通过ASP.NET Web API和Windows窗体应用程序访问数据库。但是.Net核心API不起作用 该问题的解决方案是在连接字符串中添加端口号 例如,下面指定的连接字符串是.NET Framework应用程序的示例
data source=SERVER\SQLSERVER2017;initial catalog=DBNAME;persist security info=True;user id=ADMIN;password=ADMIN123;MultipleActiveResultSets=True;App=EntityFramework
我按照链接设置DB实例的端口号
逗号后的端口号是键。
因此,将连接字符串更改为如下内容:
Server=SERVER\\SQLSERVER2017,1433;Database=DBNAME;User Id=ADMIN; Password=ADMIN123;MultipleActiveResultSets=True;
我们遇到了一个类似(或相同?)的问题。NETCore不能处理连接字符串,但.NETFramework可以处理相同的字符串。事实证明,这是因为我们的连接字符串使用的是SQL别名而不是数据库服务器的IP主机名,而.NET Core放弃了SQL别名支持,因为它太过Windowsy/registry-y
将连接字符串更改为使用IP主机名或IP号码解决了此问题
- .NET团队讨论此问题:
- 相关SO问题:例如李>
Server=192.168.1.XXX\\SQL2012代码>。我不知道为什么,但系统无法处理它。我改用另一台服务器(没有实例)
欢迎使用真正的解决方案。等等-那么您正试图同时连接到两个独立的数据库?不,我只想连接远程数据库。但是我尝试了一切:/看起来与这个问题类似:对于一个有用户和密码的远程服务器,您可以尝试使用数据源=
,而不是服务器=
,我使用的是本地主机:1433
,但出于某种原因,它应该是本地主机,1433
System.Data.SqlClient.SqlException (0x80131904): 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: 50 - Local Database Runtime error occurred. Der angegebene Name der LocalDB-Instanz ist ungültig.
) ---> System.ComponentModel.Win32Exception (0x89C5011B): Unknown error (0x89c5011b)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken)
System.Data.SqlClient.SqlException (0x80131904): 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)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken)
data source=SERVER\SQLSERVER2017;initial catalog=DBNAME;persist security info=True;user id=ADMIN;password=ADMIN123;MultipleActiveResultSets=True;App=EntityFramework
Server=SERVER\\SQLSERVER2017,1433;Database=DBNAME;User Id=ADMIN; Password=ADMIN123;MultipleActiveResultSets=True;