簡體   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