How to convert empty string to null while reading CSV records?

1.9k views Asked by At

I am reading CSV files and dynamically creating database tables based on headers of CSV files.

But getting problem while reading csv data and dumping in to relevant tables. The problem comes when I have a column with decimal datatype and CSV fields contains blank so it is treated as string and getting below error.

Error :

Failed to convert parameter value from a String to a Decimal.

I am using CSV Helper and through class map, maybe I can achieve this but I am dynamically creating structure based on CSV files so I can't define any fixed class.

Here is 1 answer I found which might be helpful in case of class map: Using CsvHelper can I translate white space to a nullable?

Sample Record :REcord

Problem occurs when sqlbulk copy tries to dump those data in SQL Server database table.

1

There are 1 answers

1
NicoRiff On

I think that you might need to do column mapping. If you are using SqlBulkCopy you can use some of what is described on this post:

Mapping columns in a DataTable to a SQL table with SqlBulkCopy