I have a requirement where I need to dump my Excel data in SQL Server table. The excel files could be varying (different no. of columns each time) and for each excel source file a new table has to be created everytime in SQL Server.
I tried with SSIS tasks coming across things that Mapping between input/output columns has to be predefined in package. Also, the destination table I am dumping data in has to be present before executing "OLE DB Destination" task.
To overcome some limitations, I did few workarounds:
- Created a sample table in my database with 50 columns (because that is the max. columns I can have at any point of time in my source excel).
- Before package executes I take a copy of that sample table giving it a name as I need to have different tables for each source.
- Passing Excel source file dynamically to package through c# code and SSIS Variables.
Since my initial mapping is between 50 input/output columns in package, when the next excel reaches package with less no. of columns, package execution fails. I am running the package through c# code, also when i independently run this package in BIDS passing SSIS variable value there itself, it fails with invalid column references error. I have to run this package through code and I can't everytime remap it on failure.
I can't paste a snapshot of Package as of my reputation on this forum. Its a simple package with 2 tasks i.e. 'Excel Source' & 'OLE DB Destination'. The only issue I am facing is with dynamic mapping in package. Rest all is working fine.
Please help me out with this. Thanks in Advance!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SqlServer.Dts.Runtime;
namespace SSRSReports
{
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnpkg.Visible = true;
}
protected void btnpkg_Click(object sender, EventArgs e)
{
string newtableName = "DATA_MD0000001_I606423";
string sourceExcel = FileUpload1.PostedFile.FileName;
DataQuery(newtableName);
RunPackage(sourceExcel, newtableName);
}
public void DataQuery(string newtableName)
{
DataClasses1DataContext dc = new DataClasses1DataContext();
dc.ExecuteCommand("select * into" + " " + newtableName + " " + "from DummyTable");
}
public void RunPackage(string SourceExcelPath, string DestinationTableName)
{
string pkgLocation;
Package pkg;
Application app;
DTSExecResult pkgResults;
Variables vars;
lblResults.Visible = false;
pkgLocation =
@"C:\Visual Studio 2008\Projects\DemoProject\DemoProject\Package.dtsx";
app = new Application();
pkg = app.LoadPackage(pkgLocation, null);
vars = pkg.Variables;
vars["SourceExcelFile"].Value = SourceExcelPath;
vars["DestinationTableName"].Value = DestinationTableName;
pkgResults = pkg.Execute(null, vars, null, null, null);
if (pkgResults == DTSExecResult.Success)
lblResults.Text = "Package ran successfully";
else
lblResults.Text = "Package failed";
lblResults.Visible = true;
}
}
}