SQLBulkCopy - WriteToServer method not working

c# datatable sqlbulkcopy


I'm trying to export some data from an Excel file to a MDF database. To do that, I'm using Excel Data Reader and SQL Bulk Copy :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Excel;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BulkTest
    class Program
        static void Main(string[] args)
            FileStream fs = File.Open(@"C:\Users\maarab\Desktop\List_Personnel 2013.xlsx", FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(fs);

            DataTable dt = CreateDataTable();

            string cx = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DataTest.mdf;Integrated Security=True;User Instance=True";
            SqlBulkCopy bcp = new SqlBulkCopy(cx, SqlBulkCopyOptions.KeepIdentity);

            bool first = true;

            while (excelReader.Read())
                if (first)
                    first = false;

                    if (String.IsNullOrWhiteSpace(excelReader.GetString(0)))

                        string numNat = excelReader.GetString(2);
                        DateTime birthDate = excelReader.GetDateTime(9);
                        DateTime startDate = excelReader.GetDateTime(1);

                        if (!String.IsNullOrWhiteSpace(numNat))
                            if (numNat.Length == 12)
                                numNat = numNat.Remove(6, 1);

                        if (birthDate.Year < 1753)
                            birthDate = DateTime.Now;

                        if (startDate.Year < 1753)
                            startDate = DateTime.Now;

                        dt.Rows.Add(excelReader.GetString(0), excelReader.GetString(0), numNat, startDate, birthDate);


            bcp.DestinationTableName = "person";
            bcp.BatchSize = 100;

            bcp.ColumnMappings.Add(0, 1);
            bcp.ColumnMappings.Add(1, 2);
            bcp.ColumnMappings.Add(2, 3);
            bcp.ColumnMappings.Add(3, 4);
            bcp.ColumnMappings.Add(4, 5);

                bcp.WriteToServer(dt, DataRowState.Unchanged);
            catch (Exception e)



        public static DataTable CreateDataTable()
            DataTable dt = new DataTable();

            dt.Columns.Add("FirstName", typeof(string));
            dt.Columns.Add("LastName", typeof(string));
            dt.Columns.Add("NumNat", typeof(string));
            dt.Columns.Add("Birthdate", typeof(DateTime));
            dt.Columns.Add("StartDate", typeof(DateTime));

            return dt;

I'm filling a datatable with my Excel file and all goes good. The program is running without problem but unfortunatelly there's no insert into my table. Any idea to solve that?

7/25/2013 12:10:20 PM

Accepted Answer

If you look at your table using Server Explorer you should check carefully the connection used in this window. It happens very often that you have your database file listed in the project files and the connection used by Server Explorer points to this file in the project folder.
Of course, when your program runs, the connection used is the one specified in your app.config.
In this scenario you have no error (because no error exists) but you look with the Server Explorer to the wrong database where no action occured and you can't see the updates.
The solution is easy, you need to reconfigure the connection used by the Server Explorer to point to the effective value of the |DataDirectory| substitution string (Usually BIN\DEBUG or BIN depending on project type)

7/25/2013 12:54:47 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow