c# ADODB.Recordset实例调用Fields报错

代码:

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ADODB;

namespace ConsoleApp1
{
    internal class Programre
    {
        static ADODB.Recordset recordsetInstance=new ADODB.Recordset();
        static Connection connectionInstance=new Connection();  
        static void Main(string[] args)
        {
            string databaseFilePath = "D:\\database.mdb";
            string tableName = "table";
            string columnName = "columnName";
            dynamic columnValue;
            connectionInstance.ConnectionString = $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={databaseFilePath}";
            connectionInstance.Open();
            recordsetInstance.Open($"SELECT * FROM {tableName}", connectionInstance, CursorTypeEnum.adOpenForwardOnly, LockTypeEnum.adLockReadOnly);
            columnValue = recordsetInstance.Fields[columnName].Value;
            Console.WriteLine(columnValue.ToString());
            connectionInstance.Close();
            Console.ReadLine();
        }
    }
}

报错信息:

System.TypeLoadException
  HResult=0x80131522
  Message=未能从程序集“ConsoleApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中加载类型“ADODB.FieldsToInternalFieldsMarshaler”。
  Source=mscorlib
  StackTrace:
   at System.StubHelpers.StubHelpers.CreateCustomMarshalerHelper(IntPtr pMD, Int32 paramToken, IntPtr hndManagedType)
   at ADODB._Recordset.get_Fields()
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\hahah\Desktop\test\ConsoleApp1\Program.cs:line 20

解决方式:

代码不变,更换“Microsoft ActiveX Data Objects Libray“COM组件版本为6.0解决。(原本VS给我自动引用的是2.x;更换方式为进入项目的引用窗体,选择COM节点,勾选对应COM组件即可。)

c# ADODB.Recordset实例调用Fields报错_第1张图片

你可能感兴趣的:(c#,数据库,microsoft)