[英]Type or namespace name could not be found (missing using directive or assembly reference?)
[英]The type or namespace name 'OleDbConnection' could not be found (are you missing a using directive or an assembly reference?)
我正在尝试从 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,重新安装包并进行重建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.