I need to load data from multiple excel files to single table in sql server. But, I may get different headers in different files. Also, no of columns in excel will be less when compared to table. So, I'm trying to take columns from excel files and comparing against system table in database to get the corresponding column name. I'm using this inside a script task in SSIS package. Please see the code and sample data given below. Getting an error while doing column mapping.
Name EmpId Salary
Anna PD200 200
Julie PD300 300
Name EmpId Sal
Maria PD400 400
Treeza PD500 500
CREATE TABLE [dbo].[testLoad]
(
[Name] [nvarchar](255) NULL,
[EmpId] [nvarchar](255) NULL,
[Salary] [nvarchar](50) NULL
)
public void Main()
{
// TODO: Add your code here
string filepath = Dts.Variables["User::var_File_Path"].Value.ToString();
string tablename = Dts.Variables["User::var_Tbl_Name"].Value.ToString();
string filename = Dts.Variables["User::var_File_Name"].Value.ToString();
string tbl = tablename.Replace("[", "");
tbl = tbl.Replace("]", "");
tbl = tbl.Replace("dbo.", "");
SqlConnection sqlconnection = new SqlConnection();
sqlconnection = (SqlConnection)(Dts.Connections["ADOAUDIT"].AcquireConnection(Dts.Transaction));
string ConStr;
string HDR;
HDR = "YES";
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + filepath + "; Extended Properties=\"EXCEL 12.0 XML; HDR="+HDR+"\";";
OleDbConnection cnn = new OleDbConnection(ConStr);
cnn.Open();
DataTable dtSheet = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetname = "";
string ExcelColumn = "";
string SqlColumn = "";
string SqlColumns = "";
string ExcelCol = "";
string query = "";
string querycol = "";
foreach (DataRow drSheet in dtSheet.Rows)
{
sheetname = drSheet["TABLE_NAME"].ToString();
OleDbCommand oconn = new OleDbCommand(" top 1 * from [" + sheetname + "]", cnn);
OleDbDataAdapter adp = new OleDbDataAdapter(oconn);
DataTable dt = new DataTable();
adp.Fill(dt);
cnn.Close();
for (int i = 0; i < dt.Columns.Count; i++)
{
ExcelCol = dt.Columns[i].ColumnName;
ExcelCol = ExcelCol.Substring(0, 5);
querycol = "select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS " +
"where TABLE_NAME = '" + tbl + "' " +
"and COLUMN_NAME like '" + ExcelCol + "%'";
SqlCommand sqlCommand = new SqlCommand(querycol, sqlconnection);
SqlColumn = (string)sqlCommand.ExecuteScalar();
if (!String.IsNullOrEmpty(SqlColumn))
{
SqlColumns = SqlColumns + "'" + SqlColumn + "',";
ExcelColumn = ExcelColumn + "'" + dt.Columns[i].ColumnName + "',";
}
}
SqlColumns = SqlColumns.TrimEnd(',');
ExcelColumn = ExcelColumn.TrimEnd(',');
query = "select " + ExcelColumn + " from [" + sheetname + "]";
OleDbConnection conn1 = new OleDbConnection(ConStr);
conn1.Open();
OleDbCommand oconn1 = new OleDbCommand(query, conn1);
OleDbDataAdapter adp1 = new OleDbDataAdapter(oconn1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
conn1.Close();
//Load Data from DataTable to SQL Server Table.
using (SqlBulkCopy BC = new SqlBulkCopy(sqlconnection))
{
BC.DestinationTableName = tablename;
foreach (var column in dt1.Columns)
{
BC.ColumnMappings.Add(column.ToString(), SqlColumns.ToString());
}
BC.WriteToServer(dt);
}
sqlconnection.Close();
}
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
Thank You Julie