0

I have an Excel file(.xlsx) and I am trying to upload the content of the file into a Sql server table. Iam using the SQL bulk copy to bulk insert the data. The data gets inserted into the table but I find that the data is not inserted properly.

Here is the sample Excel data-

enter image description here

This is the code for Sql bulk copy:

string fname = Path.GetFileName(fup_addRoute.FileName);
fup_addRoute.SaveAs(Server.MapPath("/Admin/UserRoutes/" + fname));
string path = Server.MapPath("/Admin/UserRoutes/" + fname);
                    
using (OleDbConnection connection = new OleDbConnection(string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path)))
{
      OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
      connection.Open();
      System.Data.Common.DbDataReader dr = command.ExecuteReader();
      SqlBulkCopy bulkInsert = new SqlBulkCopy(con); 
      bulkInsert.DestinationTableName = "routesdata";
      bulkInsert.WriteToServer(dr);
      connection.Close();
      dr.Close();
      bulkInsert.Close();
}

Data after insert:

enter image description here

Only last row gets inserted and the first column value in excel sheet is missing. The xid column in table is an auto-increment column.

This procedure was easy in MySql with 'load data infile' but I just migrated to Sql server. What am I doing wrong in my code. Suggestions please.

3
  • Are you only getting the last row or just not receiving the 1st row? Commented Feb 3, 2013 at 4:13
  • Sorry, I just found that I am only missing the first row. Commented Feb 3, 2013 at 4:22
  • 1
    Try adding HDR=NO to your connection string... Commented Feb 3, 2013 at 4:23

2 Answers 2

3

As a follow-up to my earlier comment, since you're only missing the first row, it sounds like you need to add HDR=No to your connection string.

It should look something like this:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=No';

--EDIT

Please note the added apostrophes.

Good luck.

Sign up to request clarification or add additional context in comments.

8 Comments

Sorry that generates an exception after adding HDR=No; Could not find installable ISAM.
stackoverflow.com/a/644365/1073631 -- see if that doesn't help -- I just copied and pasted :)
+1 that solved the issue of missing the first row. However I still have the issue with auto increment.
Can you elaborate? Not sure I'm understanding your other issue.
|
2

I'm actually a bit surprised you are not reporting an exception.

At any rate, I suspect that (at least part of) your problem is that you need to specify the column mappings.

From http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopycolumnmapping.aspx

If mappings are not defined—that is, the ColumnMappings collection is empty—the columns are mapped implicitly based on ordinal position. For this to work, source and target schemas must match. If they do not, an InvalidOperationException will be thrown.

From what I can see your Excel file and your database tables do not match on all columns (e.g. the auto-increment). So I would try specifying your column mappings.

3 Comments

just a guess that first roa i
Sorry about top comment. It was a mistake. Sorry
Great, your solution solved the issue with auto increment field. Thanks. Oh now who's answer do I accept, sdegges solved one and you solved the other issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.