C# 找不到类型或命名空间名称“OleDbConnection”(是否缺少using指令或程序集引用?)

C# 找不到类型或命名空间名称“OleDbConnection”(是否缺少using指令或程序集引用?),c#,asp.net,json,parsing,C#,Asp.net,Json,Parsing,我正在尝试从excel格式解析为json格式。下面是我遇到的OleDbConnection问题的代码。我尝试了不同的解决方案,但似乎没有任何效果 using System; using System.Linq; using System.Data; using System.Data.OleDb; using System.Data.Common; using Newtonsoft.Json; using System.IO; namespace ResumeParsing { cla

我正在尝试从excel格式解析为json格式。下面是我遇到的OleDbConnection问题的代码。我尝试了不同的解决方案,但似乎没有任何效果

using System;
using System.Linq;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using Newtonsoft.Json;
using System.IO;

namespace ResumeParsing
{
    class Program
    {
        static void Main(string[] args)
        {
            var pathToExcel = @"C:\Users\Admin\Desktop\resumeexcelformat.xlsx";
            var sheetName = "Sheet1";
            var destinationPath = @"C:\path\to\save\json\file.json";

            //Use this connection string if you have Office 2007+ drivers installed and 
            //your data is saved in a .xlsx file
            var connectionString = $@"
                Provider=Microsoft.ACE.OLEDB.12.0;
                Data Source={pathToExcel};
                Extended Properties=""Excel 12.0 Xml;HDR=YES""
            ";

            //Creating and opening a data connection to the Excel sheet 
            using (var conn = new OleDbConnection(connectionString))
            {
                conn.Open();

                var cmd = conn.CreateCommand();
                cmd.CommandText = $"SELECT * FROM [{sheetName}$]";

                using (var rdr = cmd.ExecuteReader())
                {

                    //LINQ query - when executed will create anonymous objects for each row
                    var query = rdr.Cast<DbDataRecord>().Select(row => new {
                        Prefix = row[1],
                        FirstName = row[2],
                        MiddleName = row[3],
                        Surname = row[4]
                    });

                    //Generates JSON from the LINQ query
                    var json = JsonConvert.SerializeObject(query);

                    //Write the file to the destination path    
                    File.WriteAllText(destinationPath, json);
                }
            }
        }
    }


}

通过谷歌,我试图添加引用,但在我的项目中没有汇编选项,并且它没有显示在该项目中找到的任何项目,以检查是否选中system.data。请参见图片。请告诉我如何解决这个错误好吗?

重要的是添加System.Data.dll,因为OleDb需要它。 但是在您的代码示例中,您已经在这样做了


请尝试删除nuget软件包,清理项目,关闭VS,重新安装软件包并进行重建。

System.Data.OleDb在.NET Core中不受支持。感谢lan Kemp,还有什么替代方法?如果您只想读取Excel文件,请使用库来读取Excel文件,例如,而不是OleDb。EPPlus的好处是它不需要在机器上安装MS Office。