次の方法で共有


SqlClient トラブルシューティング ガイド

ADO.NET のダウンロード

SQL Server への接続時に発生する例外

接続の確立に失敗する理由はさまざまです。 その問題の多くを分析して解決するためのガイドとして使用できるトラブルシューティングのヒントを以下に示します。

ネイティブ SNI (サーバー ネットワーク インターフェイス) ライブラリを読み込めない

.NET Framework アプリケーションの問題

Stacktrace observed:

TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNILoadHandle' threw an exception.
DllNotFoundException: Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNILoadHandle' threw an exception.
DllNotFoundException: Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x86.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

SNI は、Windows 上での実行時にさまざまなネットワーク操作を行うために SqlClient が依存するネイティブ C++ ライブラリです。 MSBuild プロジェクト SDK を使用してビルドされた .NET Framework アプリケーションの場合、ネイティブ DLL は復元コマンドを使用して管理されません。 そのため、 .targets ファイルは、必要な "コピー" 操作を定義する Microsoft.Data.SqlClient.SNI NuGet パッケージに含まれます。

含まれる .targets ファイルは、 Microsoft.Data.SqlClient ライブラリに直接依存関係が作成されると、自動的に参照されます。 推移的 (間接) 参照が行われるシナリオでは、必要に応じて "コピー" 操作を実行できるように、この .targets ファイルを手動で参照する必要があります。

推奨される解決策:.targets ファイルがアプリケーションのプロジェクト ファイルで参照されていることを確認して、"コピー" 操作が実行されていることを確認します。 プロジェクト ファイルの編集の例については、この関連する GitHub の問題を参照してください。

これらのターゲットには、Microsoft のよく知られている、一般的に使用されているターゲットのみが含まれます。 外部ツールまたはアプリケーションでバイナリをコピーするカスタム ターゲットが定義されている場合は、ネイティブ SNI DLL が Microsoft.Data.SqlClient.dll バイナリと共にコピーされ、クライアント アプリケーションの実行時に使用可能になるように、ツール 保守管理者が新しいターゲットを定義する必要があります。

.NET Core アプリケーションの問題

Stacktrace observed:

System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.TdsParser' threw an exception.
---> System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNILoadHandle' threw an exception.
---> System.DllNotFoundException: Unable to load shared library 'Microsoft.Data.SqlClient.SNI.dll' or one of its dependencies.

Note

このエラーは、Windows アプリケーションでのみ発生する可能性があります。 これが Unix 環境で発生する場合は、ご自分のアプリケーションが Windows ではなく Unix ランタイムを適切にターゲットとするように構築されていることを確認する必要があります。

SNI は、Windows 上での実行時にさまざまなネットワーク操作を行うために SqlClient が依存するネイティブ C++ ライブラリです。 Microsoft.Data.SqlClient では、.NET Core でのこのライブラリの読み込み/アンロードは管理されません。

推奨される解決策: ネイティブ ランタイム ライブラリが .NET Core プロセスに読み込まれるファイルシステムに "実行" アクセス許可が付与されていることを確認します。 それでも問題が解決しない場合は、dotnet/runtime リポジトリにイシューを報告して、さらにサポートを求めることができます。

ネイティブ SNI (pdb が見つからない) エラー

Stacktrace observed:

An assembly specified in the application dependencies manifest (sql2csv.deps.json) was not found:
  package: 'Microsoft.Data.SqlClient.SNI.runtime', version: '2.0.0'
  path: 'runtimes/win-x64/native/Microsoft.Data.SqlClient.SNI.pdb'

推奨される解決策: クライアント アプリケーションでバージョン v2.1.0 以上の Microsoft.Data.SqlClient パッケージが参照されていることを確認します。 EF Core を使用する場合は、依存関係をオーバーライドするために、このパッケージ バージョンの Microsoft.Data.SqlClient への参照を直接追加します。

ホスト名の解決エラー

Stacktrace observed:

Microsoft.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: TCP Provider, error: 0 - No such host is known.)
Microsoft.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: TCP Provider, error: 35 - An internal exception was caught)
Microsoft.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: TCP Provider, error: 35 - An internal exception was caught)
 ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 0xFFFDFFFF): Name does not resolve

Possible reasons

  • TCP および名前付きパイプ プロトコルが SQL Server 上で有効になっていない

    推奨される解決策: SQL Server 構成マネージャー コンソールから、TCP および名前付きパイプ プロトコルを SQL Server 上で有効にします。

  • ホスト名が不明

    推奨される解決策: 接続が開始されているクライアントから、ホスト名がサーバーの IP アドレスに解決されることを確認します。

Login-phase errors

Stacktraces observed:

Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the pre-login handshake.
(provider: SSL Provider, error: 31 - Encryption(ssl/tls) handshake failed)
System.IO.EndOfStreamException: End of stream reached
A connection was successfully established with the server, but then an error occurred during the login process.
(provider: SSL Provider, error: 0 - The target principal name is incorrect.)
Microsoft.Data.SqlClient.SqlException (0x80131904): Connection Timeout Expired. The timeout period elapsed during the post-login phase. The connection could have timed out while waiting for server to complete the login process and respond; Or it could have timed out while attempting to create multiple active connections.
The duration spent while attempting to connect to this server was - [Pre-Login] initialization=837; handshake=394; [Login] initialization=3; authentication=15; [Post-Login] complete=1027;
---> System.ComponentModel.Win32Exception (258): Unknown error 258
at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)

考えられる原因と解決策

  • SQL Server で TLS 1.2 がサポートされていない

    通常、このエラーは、TLS 1.2 がサポートされている最低限の TLS プロトコルである Docker イメージ コンテナー、Unix クライアント、または Windows クライアントなどのクライアント環境で発生します。

    推奨される解決策: サポートされているバージョンの SQL Server に最新の更新プログラムをインストールし、サーバーで TLS 1.2 プロトコルが有効になっていることを確認します。

    Note

    さまざまなバージョンのでサポートされている SQL Server バージョンの一覧については、Microsoft.Data.SqlClientを参照してください。

    安全でないソリューション: TLS 1.0 を使用して接続するように Docker イメージまたはクライアント環境で TLS/SSL 設定を構成します。

    MinProtocol = TLSv1
    CipherString = DEFAULT@SECLEVEL=1
    

    Note

    TLS 1.0 または TLS 1.1 を使用して Windows/Linux 環境から Microsoft.Data.SqlClient v2.0 以降で接続する場合、接続を確立するときにターゲット SQL Server とクライアントが TLS バージョン 1.2 の最小値をネゴシエートできない場合、セキュリティ警告メッセージがスローされます。

    Security Warning: The negotiated <TLS1.0 | TLS1.1> is an insecure protocol and is supported for backward compatibility only. The recommended protocol version is TLS 1.2 and later.

  • SQL Server による暗号化の強制

    ターゲット サーバーが、[強制的に暗号化] プロパティがオンになっている Azure SQL インスタンスまたはオンプレミスの SQL Server である場合、暗号化された接続が確立され、それに対してクライアントによってサーバーとの信頼関係が確立される必要があります。

    推奨される解決策: この問題を解決するには、次の 2 つのオプションを使用できます。

    • クライアント環境にターゲット SQL Server の TLS 証明書をインストールします。 暗号化が必要な場合は検証されます。
    • (安全性が低下) 接続文字列で "TrustServerCertificate=true" プロパティを設定する。

    安全でないソリューション: SQL Server の [強制的に暗号化] 設定を無効にします。

  • SHA-256 以上で署名されていない TLS 証明書。

    推奨される解決策: 少なくとも SHA-256 ハッシュ アルゴリズムでハッシュが署名されているサーバーの新しい TLS 証明書を生成します。

  • Linux と .NET 5 以降での厳密に制限された暗号スイート

    .NET 5 で導入された Linux クライアントに関する破壊的変更では、許可されている暗号スイートの厳密に制限された一覧が既定で使用されます。 CipherSuitePolicy値を指定するか、OpenSSL 構成ファイルを変更して、レガシ クライアントを受け入れる (またはレガシ サーバーに接続する) には、既定の暗号スイートの一覧を展開する必要がある場合があります。

    推奨されるアクションについては、 Linux 上の .NET の既定の TLS 暗号スイートに関 するページを参照してください。

    Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)
    ---> System.ComponentModel.Win32Exception (0x80090325): The certificate chain was issued by an authority that is not trusted.
    
  • SQL Server による暗号化の強制

    ターゲット サーバーが、[強制的に暗号化] プロパティがオンになっていて自己署名証明書を持つオンプレミスの SQL Server である場合、暗号化された接続が確立され、それに対してクライアントによってサーバーとの信頼関係が確立される必要があります。

    推奨される解決策: この問題を解決するには、次の 2 つのオプションを使用できます。

    • クライアント環境にターゲット SQL Server の TLS 証明書をインストールします。 暗号化が必要な場合は検証されます。
    • (安全性が低下) 接続文字列で "TrustServerCertificate=true" プロパティを設定する。

    安全でないソリューション: SQL Server の [強制的に暗号化] 設定を無効にします。

接続プール不足のエラー

スタック トレースが観察されました:

System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.
This may have occurred because all pooled connections were in use and max pool size was reached.

考えられる原因と解決策

クライアント アプリケーションによって、特定の時点で接続プールがアクティブに保持できるよりも多くの接続が開かれています。

推奨される解決策: [最大プール サイズ] 接続プロパティをより高い値に設定し、使用されていない接続を適切なタイミングで閉じます。

Contact Support

このガイドで接続の問題が解決しない場合は、 dotnet/sqlclient リポジトリに既存の問題を表示し、必要に応じて新しい問題を開く可能性があります。