Hi I have an excel where each column belongs to different table in db. I need to insert rows of excel by mapping correct column of excel to correct table and then column, for comparing I am using dictionary . I have written small prog but its not feasible.
ex if i have 1000 rows in excel and 20 column then my total insert going to be 20000 which is not good for performance aspects.
Could you please help me to resolve this issue and get optimal solution.
foreach (var v in res)
{
if (dic.ContainsKey(v.ColumnFromName))
{
dic[v.ColumnFromName].Add(new Tuple<string, string>(v.TableToName,
v.ColumnToName));
}
else
{
dic.Add(v.ColumnFromName, new List<Tuple<string, string>>
{ new Tuple<string, string>(v.TableToName, v.ColumnToName) });
}
}
if (dt.Rows.Count > 0)
{
StringBuilder sb = new StringBuilder();
List<string> ls = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
foreach (DataColumn col in dt.Columns)
{
sb.Clear();
var tbletoname = dic[col.ColumnName].FirstOrDefault().Item1;
Console.WriteLine(col.ColumnName);
var columntoname = dic[col.ColumnName].FirstOrDefault().Item2;
sb.Append("Insert into ");
sb.Append(tbletoname);
sb.Append(" (");
sb.Append(columntoname);
sb.Append(" ) ");
sb.Append("Values");
sb.Append(" ('");
sb.Append(dt.Rows[i][col.ColumnName]);
sb.Append("')");
ls.Add(sb.ToString());
Console.WriteLine(dt.Rows[i][col.ColumnName]);
}
}
}