SQL Bulk Upload with c# column mapping handling nulls

I have a c# program that uses sql bulk upload to insert records into a table. It works very well but I would like to handle the null values differently. The text file that contains the data has over 300,000 records. Some of those records do not have data, so an empty string is inserted. I'd like to leave the fields null. Perhaps use DNNull? The records also have a date field, which I have had to make nvarchar in the SQL table. If it is a datetime field, it errors out. Is there a way to keep it as datetime and insert the date? Thanks fro any help. My code is below.

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace LoadData
    class Program
        static void Main(string[] args)
            DataTable dt = new DataTable();
            string line = null;
            int i = 0;

            using (StreamReader sr = File.OpenText(@"c:\temp\empdata.txt"))
                while ((line = sr.ReadLine()) != null)
                    string[] data = line.Split(';');
                    if (data.Length > 0)
                        if (i == 0)
                            foreach (var item in data)
                                dt.Columns.Add(new DataColumn());
                        DataRow row = dt.NewRow();
                        row.ItemArray = data;

            using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
                using (SqlBulkCopy copy = new SqlBulkCopy(cn))
                    copy.ColumnMappings.Add(0, 0);
                    copy.ColumnMappings.Add(1, 1);
                    copy.ColumnMappings.Add(2, 2);
                    copy.ColumnMappings.Add(3, 3);
                    copy.ColumnMappings.Add(4, 4);
                    copy.ColumnMappings.Add(5, 5);
                    copy.ColumnMappings.Add(6, 6);
                    copy.ColumnMappings.Add(7, 7);
                    copy.ColumnMappings.Add(8, 8);
                    copy.ColumnMappings.Add(9, 9);
                    copy.ColumnMappings.Add(10, 10);
                    copy.DestinationTableName = "Employees";

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

this may be a solution for dbnull issue:

try this example from expert exchange solution. that's a vb version, convert to C#.

before inserting the myRow.Item(i) = fieldValues(i).ToString, you can check for empty string and convert it to null

myRow.Item(i)= string.IsNullOrWhiteSpace(fieldValues(i).ToString)? DBNull.Value : fieldValues(i).ToString);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.