下記の環境でC#Windows フォームアプリを作っています。
Visual Studio Professional 2022(64ビット) Version 17.2.4
Microsoft .NET Framework Version 4.8.04084
C#ツール 4.2.0-4.22281.5
次のコードをDebugで実行している際、System.TypeLoadExceptionが発生し
実行途中で終了しました。
using System;
using System.Data;
using Oracle.ManagedDataAccess.Client;
public partial class BItoolUserList : Form
{
public BItoolUserList()
{
InitializeComponent();
}
private void searchBtn_Click(object sender, EventArgs e) { string sql = "SELECT account, password, name, yuko_kubun_code FROM R_ACCOUNT WHERE ROWNUM <= 5"; try { using (OracleConnection conn = new OracleConnection()) { conn.ConnectionString = "User Id=xxxxx; Password=rrrrrr; Data Source=OORRAACCLLEE"; conn.Open(); Console.WriteLine("DB接続開始"); using (OracleCommand cmd = new OracleCommand(sql)) { cmd.Connection = conn; cmd.CommandType = CommandType.Text; using (OracleDataReader reader = cmd.ExecuteReader()) { while(reader.Read()) { Console.WriteLine(reader["account"]); Console.WriteLine(reader["password"]); Console.WriteLine(reader["name"]); Console.WriteLine(reader["yuko_kubun_code"]); } } } } } catch(Exception ex) { Console.WriteLine(ex.Message.ToString()); } }
}
エラーメッセージは次の通りです。
例外がスローされました: 'System.TypeLoadException' (System.Private.CoreLib.dll の中)
型 'System.TypeLoadException' のハンドルされていない例外が System.Private.CoreLib.dll で発生しました
Could not load type 'System.Security.Principal.WindowsImpersonationContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
System.TypeLoadException
HResult=0x80131522
Message=Could not load type 'System.Security.Principal.WindowsImpersonationContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Source=Oracle.ManagedDataAccess
エラーの詳細を確認すると次の通りになっています。
スタック トレース:
場所 OracleInternal.ConnectionPool.PoolManager`3.CreateNewPRThreadFunc(Object state)
場所 System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute() (//src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs):行 979
場所 System.Threading.ThreadPoolWorkQueue.Dispatch() (//src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs):行 790
場所 System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart() (//src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WorkerThread.cs):行 63
場所 System.Threading.Thread.StartCallback() (//src/coreclr/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs):行 106
ODP.NETを入れたときの手順について
(VSの)ツール->NuGetパッケージマネージャー->ソリューションのNuGetパッケージの管理
[参照]タブでOracle.ManagedDataAccess.Coreをインストール
こちらの質問が複数のユーザーから「過去に投稿した質問と同じ内容の質問」という指摘を受けました。

回答1件
あなたの回答
tips
プレビュー