简体   繁体   中英

The type or namespace name 'OleDbConnection' could not be found (are you missing a using directive or an assembly reference?)

I am trying to parse from excel format to json.Below is the tried code I am getting problem with "OleDbConnection". I tried different solutions but nothing seems to work.

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);
                }
            }
        }
    }


}

Through google I tried to add reference but in my project there is no assembly option and it is showing "no items found" in that to check whether system.data is checked or not.See the image also. 在此处输入图片说明 Can any please tell how to resolve this error?

Important is adding System.Data.dll, because OleDb needs it. But in your code sample you are already doing it.

Please try to remove the nuget package, clean project, close VS, reinstall package and do a rebuild.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Type or namespace name could not be found (missing using directive or assembly reference?) The type or namespace name 'My_Interface_Name' could not be found (are you missing a using directive or an assembly reference?) The type or namespace name 'FacebookSessionClient' could not be found (are you missing a using directive or an assembly reference?) The type or namespace name 'SignInManager' could not be found (are you missing a using directive or an assembly reference?) error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) The type or namespace name 'PrintDocument' could not be found (are you missing a using directive or an assembly reference?) Why this error ? “The type or namespace name 'c' could not be found (are you missing a using directive or an assembly reference?)” The type or namespace name 'ContractClassFor' could not be found (are you missing a using directive or an assembly reference?) Error 1 The type or namespace name 'FPSTimer' could not be found (are you missing a using directive or an assembly reference?) The type or namespace name 'HttpConfiguration' could not be found (are you missing a using directive or an assembly reference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM