【问题标题】:PlatformNotSupportedException: System.Data.OleDb is not supported on this platformPlatformNotSupportedException:此平台不支持 System.Data.OleDb
【发布时间】:2020-09-10 19:59:15
【问题描述】:

我仍在尝试将 excel 文件上传到我的 SQL 工作台数据库中,而我为导入这些文件而编写的代码可以正常工作,它实际上会导入 excel 文件并将其保存到我指定的文件夹中。但是,当我检查我的数据库时,里面什么都没有,而且我也得到了上面提到的错误。任何意见或建议都会有很大帮助。以下是我的代码。谢谢。

模型数据类在这里

命名空间 air_traffic_weather.Models { 公共类数据 { [关键]

    public int Id { get; set; }

    public string Ident { get; set; }

    public string type { get; set; }

    public string name { get; set; }

    public int latitude_deg { get; set; }

    public int longitude_deg { get; set; }

    public int elevation_ft { get; set; }

    public string continent { get; set; }

    public string iso_country { get; set; }

    public string iso_region { get; set; }
    public string municipality { get; set; }

    public string schedule_service { get; set; }

    public string gps_code { get; set; }

    public string iata_code { get; set; }

    public string local_code { get; set; }

    public string home_link { get; set; }

    public string wikipedia_link { get; set; }

    public string keywords { get; set; }

    public DateTime CreatedAt { get; set; } = DateTime.Now;

    public DateTime Updated { get; set; } = DateTime.Now;
}

}

家庭控制器

    [HttpPost("Index")]
    public IActionResult Index(IFormFile file)
    {


        if (file != null)
        {

            // giving the path where imma store my file that will be uploaded and they will be stored in a folder in made in the Root Path.

            string thePath = Path.Combine(this._hostEnvironment.WebRootPath, "ExcelData");

            // now checking to see if that directory already exists, if it does we don't get but if it ain't then we create one

            if (!Directory.Exists(thePath))
            {
                Directory.CreateDirectory(thePath);
            }

            // now it is time to save the excel file that we gonna upload yeah!!

            string theFileName = Path.GetFileName(file.FileName);
            string theFilePath = Path.Combine(thePath, theFileName);

            using (FileStream theStream = new FileStream(theFilePath, FileMode.Create))
            {
                file.CopyTo(theStream);
            }


            // let's also read the connection string for the excel file.

            string conString = "@Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties= 'Excel 12.0 Xml;HDR=YES;'";
            DataTable thedataTable = new DataTable();
            conString = string.Format(conString, theFilePath);

            OleDbConnection connectToExcel = new OleDbConnection(conString);
            OleDbCommand commandExcel = new OleDbCommand();
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            commandExcel.Connection = connectToExcel;
            // get the first excel sheet name
            connectToExcel.Open();
            DataTable dtExcelSchema;
            dtExcelSchema = connectToExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
            connectToExcel.Close();
            // read data from first sheet
            connectToExcel.Open();
            commandExcel.CommandText = "SELECT * FROM [" + sheetName + "]";
            adapter.SelectCommand = commandExcel;
            adapter.Fill(thedataTable);
            connectToExcel.Close();

            conString = this._configuration.GetConnectionString("cs");
            using (SqlConnection connectionToSql = new SqlConnection(conString))
            {
                using (SqlBulkCopy hugeCopy = new SqlBulkCopy(connectionToSql))
                {

                    // here i will set my DB table name

                    hugeCopy.DestinationTableName = "Datas";

                    // HERE IMMA GO AHEAD AND THE EXCEL COLUMNS WITH THAT OF MY DB 

                    hugeCopy.ColumnMappings.Add("Id", "Id");
                    hugeCopy.ColumnMappings.Add("Ident", "Ident");
                    hugeCopy.ColumnMappings.Add("type", "type");
                    hugeCopy.ColumnMappings.Add("name", "name");
                    hugeCopy.ColumnMappings.Add("latitude_degrees", "latitude_degrees");
                    hugeCopy.ColumnMappings.Add("longitude_degrees", "longitude_degrees");
                    hugeCopy.ColumnMappings.Add("elevation_feet", "elevation_feet");
                    hugeCopy.ColumnMappings.Add("continent", "continent");
                    hugeCopy.ColumnMappings.Add("iso_region", "iso_region");
                    hugeCopy.ColumnMappings.Add("municipality", "municipality");
                    hugeCopy.ColumnMappings.Add("schedule_service", "schedule_service");
                    hugeCopy.ColumnMappings.Add("gps_code", "gps_code");
                    hugeCopy.ColumnMappings.Add("iata_code", "iata_code");
                    hugeCopy.ColumnMappings.Add("local_code", "local_code");
                    hugeCopy.ColumnMappings.Add("home_link", "home_link");
                    hugeCopy.ColumnMappings.Add("wikipedia_link", "wikipedia_link");
                    hugeCopy.ColumnMappings.Add("keywords", "keywords");

                    connectionToSql.Open();
                    hugeCopy.WriteToServer(thedataTable);
                    connectionToSql.Close();



                }
            }

下面的csproj文件

  • 您使用的是 .NET Core 还是框架?
  • 嘿@GlynnHurrell,感谢您与我联系,我正在使用该框架
  • 嗯,您是否通过 NuGet 安装了 System.Data.OleDb? - 如果是这样,请尝试回滚到以前的版本
  • 是的,我确实安装了它,但是我继续重新安装旧版本的 System.Data.OleDb 看看会发生什么
  • 您应该可以从“管理 Nuget 包”中执行此操作 - 选择右侧的版本....选择最新的非预览版(最有可能是 4.8)

标签: c# asp.net asp.net-mvc model-view-controller package.json


【解决方案1】:

.NET Core 不支持 System.Data.OleDb

要从 Excel 文件中读取数据,您必须考虑使用 .NET 库,例如 EPPlus

有很多教程如this one

或者您可以将项目更改为以 .NET Framework 为目标

【讨论】:

  • EPPlus最近换了license,看来商业用途不再免费了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 2019-12-29
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
  • 2017-04-30
相关资源
最近更新 更多