I want to import data from a spreadsheet to a database table. Unfortunately, the data in the spreadsheet is messy and contains ‘\n’ everywhere. I want to remove it before bulkimport.
The bulk copy procedures actually requires 2 steps. i. copy the data from the spreadsheet to a datase. ii. Execute the SqlBulkCopy to copy all data to the database table
If I could not process the data in the SqlBulkCopy class, what is the best way to do it in the DataSet?
//get the data from the spreadsheet to a dataset
DataSet ds = new DataSet();
oledbConn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [sheetname]"), oledbConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
oleda.Fill(ds);
//execute to bulkcopy with the dataset
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(conn))
{
bulkcopy.DestinationTableName = "[DestinationTableName]";
bulkcopy.WriteToServer(ds.Tables[0]);
}
This is the code I come up with to remove newline character which will be run after Fill