I have an excel worksheet which has about 200 rows and about 200+ columns. I need to unpivot this excel sheet and insert it into a sql server table. I am using a third party excel api which converts excel worksheet to a ADO .Net Datatable.
Original DataTable A
For Unpivot opertation I am traversing through the original Datatable Rows and Columns and assigning the values to another DataTable which has been defined as per the
DataTable B UnPivoted
After getting the new DataTable populated I would use SQlBulkCopy() on the datatable and save all the records to the SQLServer Table.
The code I am using for the Un-Pivot operation
//excelExport is the DataTable which stores the entire excel workSheet
DataTable newDatatable = new DataTable(); //For Un-Pivoting
newDatatable .Columns.Add(new DataColumn("EconomyID"));
newDatatable .Columns.Add(new DataColumn("SystemLanguage"));
newDatatable .Columns.Add(new DataColumn("VariableName"));
newDatatable .Columns.Add(new DataColumn("VariableValue"));
foreach (DataRow dr in excelExport.Rows) //excelExport is the original datatable
{
int colCount = 0;
foreach (DataColumn dc in excelExport.Columns)
{
if (colCount >= 2)
{
DataRow dr2 = newDatatable.NewRow();
dr2["Economy"] = dr[1].ToString();
dr2["SystemLanguageID"] = dr[2].ToString();
dr2["VariableName"] = dc.ColumnName;
dr2["VariableValue"] = dr[dc].ToString();
newDatatable.Rows.Add(dr2);
}
colCount++;
}
}
Now the problem is since my original Datatable A has 200+ columns (and 200 rows). traversing each row and assigning the values to another newDatatable takes 10seconds. So the entire operation take 200 rows * 10 seconds ... basically forever (depending on the columns in excel/datatable); just for the creation of the newDatatable. Is there more efficient way of handling this Un-pivot operation. I know we have SSIS packages which have been built for excel to sqlserver import operation but our requirement is that the client should be able to upload the excel into the system and the process has to be realtime. Any suggestions?