Why am I not successfuly importing all rows of an Excel file using the SQL Bulkcopy in C# with VS2010?

I tried using the following code in C# to take advantage of SQL Bulkcopy to copy
an Excel worksheet to a SQL Server 2005 table.

Do you know why it only copied the first 11 rows?

I have attached the Excel file that I am performing a bulk transfer on.

My results are as follows:

1  Null
2  Null
3  Null
4  Null
5  Null
6  Null
7  Book Credits (D)
8  Null
9  Null
10 Process
11 Date
12 Null
13 Null
14 Null
15 Null
16 Null
17 Null
18 Null
19 Null
20 Null
21 Null
22 Null
23 Null
24 Null
25 Null
26 Null
27 Null
28 Null
29 Null
30 Null
31 Null
32 Null

My C# code is as follows:

string sourceConnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=U:\105 Internal  08-28-2013.xls;  
                         Extended Properties=Excel 8.0";
            DataTable sourceData = new DataTable();

            using (OleDbConnection conn = new OleDbConnection(sourceConnString))
                // Get the data from the source table as a SqlDataReader.  
                OleDbCommand command = new OleDbCommand(
                      @"SELECT [ABC #] FROM [Book Credits (D)$]", conn);
                OleDbDataAdapter adapter = new OleDbDataAdapter(command);

            string destConnString = @"Data Source=F,3;Initial Catalog=S;User ID =P;PWD=M;";

            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destConnString))
                bulkCopy.DestinationTableName = "dbo.tbl_SMR_Book_Credits_D";
                // Write from the source to the destination.  

My SQL Server table is as follows:

title: dbo.tbl_SMR_Book_Credits_D

fields            data type
-------           ------------------
f1                 nvarchar(255)
f2                 nvarchar(255)
f3                 nvarchar(255)
f4                 nvarchar(255)
f5                 nvarchar(255)
f6                 nvarchar(255)
f7                 nvarchar(255)
f8                 nvarchar(255)
f9                 nvarchar(255)
f10               nvarchar(255)
f11               nvarchar(255)
f12               nvarchar(255)
f13               nvarchar(255)
f14               nvarchar(255)
Who is Participating?
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
I am not sure that this is your problem, but the documentation for the SqlBulkCopy states : "SqlBulkCopy will fail when bulk loading a DataTable column of type SqlDateTime into a SQL Server column whose type is one of the date/time types added in SQL Server 2008"

Since your copy stops on the 11th row, where you have a date, it might make sense, by reading the documentation, that it could be the culprit someway.

The documentation also states: "for moving data from one table to another, whether on a single server or between servers". Excel is not a server. Have you used SqlBulkCopy with Excel with other data before?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.