I am working on an ASP.NET MVC4 projet and I am trying to export data from an xlsx file (Excel 2010 file) to my database using SQL Bulk Copy. My Excel file contains only 2 columns: the first contains numbers (from 1 to 25), and the second contains characters (consecutive series "a, b, c")
This is how I try to export the data, but I got an error ". The specified value of type String from the data source cannot be converted to the int type of the 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 * FROM [Sheet1$]", connection); connection.Open(); string dbConnection = ((EntityConnection)db.Connection).StoreConnection.ConnectionString; // Create DbDataReader to Data Worksheet using (DbDataReader dr = command.ExecuteReader()) { using (var bulkCopy = new SqlBulkCopy(dbConnection)) { bulkCopy.DestinationTableName = "bm_test" bulkCopy.WriteToServer(dr); //here I got the error } } return RedirectToAction("Index"); }
Any idea on what causes this error?
Traffy
source share