.NET Core无法连接到SQL DB - c#

我建立了一个新的.net Core 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 Windows NT 6.1(7601)

可以使用SQL Server Management Studio或Visual Studio SQL Server对象资源管理器连接到两个DB。我也有一个运行中的asp.net Web应用程序,在其中使用了相同的(远程)连接字符串。

但是使用新的Core 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")));

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 istungü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-查找指定的服务器/实例时出错

编辑:

Unable to connect to SQL Server instance remotely是否可能重复出现?可以连接到远程数据库,只有Core出现问题。

.NET Core无法连接到SQL DB - c#
.NET Core无法连接到SQL DB - c#

参考方案

就我而言,可通过ASP.NET Web API和Windows Forms应用程序访问数据库。但是.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

我按照this链接设置数据库实例的端口号。

逗号后的端口号是关键。

因此,将连接字符串更改为如下所示:

Server=SERVER\\SQLSERVER2017,1433;Database=DBNAME;User Id=ADMIN; Password=ADMIN123;MultipleActiveResultSets=True;

解决编译器错误CS1519:类,结构或接口成员声明中的无效令牌',' - c#

我正在使用Web应用程序项目文件的ASP .NET 2.0站点上工作,因此可以编译为dll,而不是像处理较旧样式的Web站点项目一样部署源代码。该站点在网站的根目录中运行时,在我的测试服务器(Windows Server 2003 R2,IIS6)上可以正常运行。但是,我需要在虚拟目录下运行它。切换到该页面时,浏览到站点中的任何页面时都会收到以下错误CS15…

从JAVA调用方法C#.NET - java

我有一个C#.NET项目中创建的dll,我想从Java程序中调用方法。我想知道是否存在实现此目标的机会,然后遇到了JNA和JNI。我应该使用哪一个?有什么建议么?我只需要在使用C#.NET编写的类中调用方法并处理Java程序的结果即可。 参考方案 这取决于您的应用程序,但是您可以将C#DLL放在服务中,例如WCF并以这种方式将功能公开给Java代码。使用ws…

将SQL Server Serilog接收器包装在Async中是否有意义? - c#

我正在尝试最大程度地减少从.NET Core应用程序将日志写入SQL Server的性能影响。过去,我将Serilog File接收器包装在Async接收器中-这极大地提高了性能。SQL Server sink documentation指出它将定期和/或在达到batchPostingLimit时批处理日志条目。我找不到有关它决定发出INSERT命令时发生的…

从ASP.NET 1.1转换为ASP.Net 3.5后,单选按钮停止工作 - javascript

[我在下面以FYI的形式发布我们的问题和解决方案。如果您有Microsoft或代表机构对此问题的正式声明,我会将其标记为答案。否则,我下面的帖子将作为答案]。问题:我们采用了ASP.Net 1.1程序,并将其转换为ASP.Net 3.5。该页面最初正确显示,默认情况下选中一个单选按钮,并显示一个项目列表(“列表A”)。当我们单击另一个单选按钮时,它将正确显示…

jQuery blockUI在下拉列表中选择了索引更改asp.net - c#

JS:$(document).ready(function () { $('#demo2').click(function () { $.blockUI({ css: { border: 'none', padding: '15px', backgroundColor: '#000…