繁体   English   中英

.NET Core 无法连接到 SQL DB

[英].NET Core can´t connect to SQL DB

我构建了一个与 SQL DB 连接的新 .net Core Web API。 我在将 API 与我的数据库连接时遇到问题我尝试了本地数据库"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 应用程序,我在其中使用相同的(远程)连接字符串。

但是使用新的 Core Web API 我无法连接。 没有下面的设置,也没有连接来自 NuGet 的 Scaffold-DBContext

如果您需要更多我的代码,您可以询问。 非常感谢您的帮助。

 Scaffold-DbContext "Connection String" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

启动文件

 services.AddDbContext<ibContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

appsettings.json

"ConnectionStrings": {
    "DefaultConnection": "Server=192.168.1.XXX\\SQL2012; Initial Catalog=IBCOREDB_XXX; User ID=XXXX;Password=XXXXX;",
  },

核心错误(远程数据库)

Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.SqlClient.dll
'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.2.4\System.Diagnostics.StackTrace.dll'. Cannot find or open the PDB file.
'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.2.4\System.Reflection.Metadata.dll'. Cannot find or open the PDB file.
Microsoft.EntityFrameworkCore.Database.Connection:Error: An error occurred using the connection to database 'IBCOREDB_XXX' on server '192.168.1.XXX\SQL2012'.

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)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at ...

错误:26 - 定位指定的服务器/实例时出错

脚手架错误(对于本地数据库)

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)

错误:50 - LocalDB-Instanz 中的给定名称无效。:“Der angegebene Name der LocalDB-Instanz ist ungültig。”

脚手架错误(远程数据库)

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)

错误:26 - 定位指定的服务器/实例时出错


编辑:

可能无法远程连接到 SQL Server 实例重复 可以连接到远程数据库,只有 Core 有问题。

Visual Studio SQL Server 对象资源管理器 高级属性

就我而言,数据库可以通过 ASP.NET Web API 和 Windows 窗体应用程序访问。 但是 .Net Core API 不起作用。

问题的解决方法是在连接字符串中添加端口号。

例如,下面指定的连接字符串是 .NET Framework 应用程序的示例。

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;

我们遇到了类似(或相同?)的问题。 .NET Core 不能使用连接字符串,但 .NET Framework 可以很好地使用相同的字符串。 原来是因为我们的连接字符串使用的是 SQL 别名而不是数据库服务器的 IP 主机名,而 .NET Core 放弃了 SQL 别名支持,因为它太 Windows/registry-y。

更改连接字符串以使用 IP 主机名或号码解决了该问题。

这种情况下的问题是实例Server=192.168.1.XXX\\\\SQL2012; . 我不知道为什么,但系统无法处理它。 我改用另一台服务器(没有实例)。

欢迎真正的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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