I get a "Specified method is not supported" error when i try to use ExcelDataReader to upload file to database

Barrassment

I've written a method, BulkCopy, to upload my Excel file to SQL Server database table. I am trying to unit test this and it fails each time with "System.NotSupportedException : Specified method is not supported".

If someone could have a look it would be much appreciated.

Kind regards,

Emmett

    public static void BulkCopy(string inputFilePath, string tableName)
    {

        System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
        var stream = File.Open(inputFilePath, FileMode.Open, FileAccess.Read);
        using (var reader = ExcelReaderFactory.CreateReader(stream))
        {
            using (var bulkCopy = new SqlBulkCopy(ConnectionString))
            {

                bulkCopy.EnableStreaming = true;
                bulkCopy.DestinationTableName = tableName;
                reader.Read();
                var cols = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToArray();
                foreach (var col in cols)
                {
                    var column = cols.GetValue(0).ToString();

                    if (column.Trim() == "Column 1")
                    {
                        bulkCopy.ColumnMappings.Add(column, "Column 1");
                    }

                    if (column.Trim() == "Column 2")
                    {
                        bulkCopy.ColumnMappings.Add(column, "Column 2");
                    }

                    if (column.Trim() == "Column 3")
                    {
                        bulkCopy.ColumnMappings.Add(column, "Column 3");
                    }

                //continued for column mappings...

                }

                bulkCopy.WriteToServer(reader);
            }
            Console.WriteLine("Copy data to database done (DataReader).");
        }
    }
Xueli Chen

I tested your code ,the issue is showed in the following code ,you reader is incorrect.

bulkCopy.WriteToServer(reader);

Pass the datatable into bulk , try the below code

public static void BulkCopy(string inputFilePath, string tableName)
    {
        System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
        var stream = System.IO.File.Open(inputFilePath, FileMode.Open, FileAccess.Read);
        IExcelDataReader reader;

        if (inputFilePath.EndsWith(".xls"))
            reader = ExcelReaderFactory.CreateBinaryReader(stream);
        else if (inputFilePath.EndsWith(".xlsx"))
            reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        else
            throw new Exception("The file to be processed is not an Excel file");
        var conf = new ExcelDataSetConfiguration
        {
            ConfigureDataTable = _ => new ExcelDataTableConfiguration
            {
                UseHeaderRow = true
            }
        };
        var dataSet = reader.AsDataSet(conf);

        // Now you can get data from each sheet by its index or its "name"
        var dataTable = dataSet.Tables[0];

        using (var bulkCopy = new SqlBulkCopy(ConnectionString))
            {
                bulkCopy.EnableStreaming = true;
                bulkCopy.DestinationTableName = tableName;
                reader.Read();
                var cols = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToArray();
                foreach (var col in cols)
                {
                    var column =col.ToString();

                    if (column.Trim() == "Column 1")
                    {
                        bulkCopy.ColumnMappings.Add(column, "Column1");
                    }

                    if (column.Trim() == "Column 2")
                    {
                        bulkCopy.ColumnMappings.Add(column, "Column2");
                    }

                    if (column.Trim() == "Column 3")
                    {
                        bulkCopy.ColumnMappings.Add(column, "Column3");
                    }

                    //continued for column mappings...

                }
                bulkCopy.WriteToServer(dataTable);
            }
            Console.WriteLine("Copy data to database done (DataReader).");           
    }

Pass the dataReader into bulk, change your foreach part as shown

               for (var i = 0; i<cols.Count();i++)
                {
                    if (cols[i].ToString().Trim() == "Column 1")
                    {
                        bulkCopy.ColumnMappings.Add(i, "Column1");
                    }

                    if (cols[i].ToString().Trim() == "Column 2")
                    {
                        bulkCopy.ColumnMappings.Add(i, "Column2");
                    }

                    if (cols[i].ToString().Trim() == "Column 3")
                    {
                        bulkCopy.ColumnMappings.Add(i, "Column3");
                    }

                    //continued for column mappings...

                }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why I get an error when I try to upload a picture

I get an Error when I try to use Firebase Auth

When I try to use scanf with 2 arrays I get an error

Why i get this error when i use dev express file upload in asp.net?

When i try to update my json file i get a error

When i try to delete a database it get a mysql error

Error when I try to upload a Chrome extension

Mismatch Exception Error when I try to use nextint() for File IO

I get this error when I try to install

when i use firebase.database().goOnline(); I get an error

I get an error when I try to change my upload image function name Django

I'm trying to use OPENPYXL, I get this TYPE ERROR whenever i try to create an excel file

How to check duplicate and upload the new data when I use PHP to upload the CSV file into MySQL database

Why do I get an error (Notice: Undefined index) when I try to echo a value from my database?

Why I get Error "No such file or directory", when I try to include a header file?

I get file of an unkown type error when I try to open csv file in Pycharm

Error "Message: element not interactable" is displayed when I try upload a file with selenium python

I get the error "error: CL/cl.h: No such file or directory" when I try to build a project on Qt

When i try to use method it give error

I get an error whenever I try to connect database

When i try to use Spring and file.properties, i see error

I get an error when I try to use ROW_NUMBER(), why?

Using React Native, I get an error when I try to use a background image

I get a syntax error when I try to use array map function in Google Scripts. Why?

when I try and use std::filesystem in vs 2019 I get an error

why do i get an error when i try to use my models?

Enctype issue when I try to upload a file with a Coldfusion webservice

When I try and run pyspark.cmd I get the error message "find: 'version': No such file or directory"

I get nullsafety error when I try to add firebase package to pubsec.yaml file