c# - SQL Bulk Copy "The given value of type String from the data source cannot be converted to type datetime of the specified target column" using ASP.NET -


i'm working on asp.net mvc4 projet , i'm trying export data xlsx file (excel 2010 file) database using sql bulk copy. excel file contains 2 columns : first contains numbers (from 1 25) , second contains characters (successive series of "a, b, c")

this how try in order export data got error "the given value of type string data source cannot converted type int of specified target column" :

public actionresult bulk() {         string xconnstr = @"provider=microsoft.ace.oledb.12.0;data source=c:\users\maarab\documents\bulkcopy.xlsx;extended properties=excel 12.0;";     using (oledbconnection connection = new oledbconnection(xconnstr))     {         oledbcommand command = new oledbcommand("select * [sheet1$]", connection);         connection.open();             string dbconnection = ((entityconnection)db.connection).storeconnection.connectionstring;          // create dbdatareader data worksheet         using (dbdatareader dr = command.executereader())         {             using (var bulkcopy = new sqlbulkcopy(dbconnection))             {                     bulkcopy.destinationtablename = "bm_test"                  bulkcopy.writetoserver(dr); //here got error                 }         }          return redirecttoaction("index"); } 

any idea what's causing error?

sqlbulkcopy.writetoserver(datatable) fails confusing messages if column order of datatable differs column order of table definition in database (when causes type or length incompatibility). apparently writetoserver method not map column names.


Comments