I've read about as many articles as I can stand. Time to ask for help.
I am upgrading SSIS packages from 2008R2 to 2016. I'm using VS2019 set for 2016. The script task works fine right up to the line of code that does the Connection.Open();
I've tried both providers, same result. I was using a Package Parameter, but commented that out and hard coded the value.
This is the returned Exception:
DTS Script Task has encountered an exception in user code: Project name: ST_6bceaa360e1d4200a203f7c688acd0fd Exception has been thrown by the target of an invocation. at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
Here is the code I'm using:
public void Main()
{
Dts.TaskResult = (int)ScriptResults.Success;
string sFile;
OleDbConnection connection;
string sConnectionString;
int dataloop = 0;
//sFile = Dts.Variables["$Project::InputFilePath01"].Value.ToString();
sFile = @"C:\AccknowledgeData\InvoiceXLS.xls";
if (File.Exists(sFile))
{
if (File.GetAttributes(sFile) == FileAttributes.ReadOnly)
{
//File.SetAttributes(sFile, FileAttributes.Normal);
Dts.TaskResult = (int)ScriptResults.Failure;
return;
}
//sConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFile + ";Extended Properties=Excel 8.0";
sConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source=" + sFile + @";Extended Properties=Excel 8.0;HDR=NO";
using (connection = new OleDbConnection(sConnectionString))
{
connection.Open();
try
{
DataTable tables = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
while (dataloop < tables.Rows.Count)
{
try
{
DataRow table = tables.Rows[dataloop];
OleDbDataAdapter cmdLoadExcel = new System.Data.OleDb.OleDbDataAdapter("select count(*) from [" + table["TABLE_NAME"].ToString() + "]", connection);
DataSet ldExcelDS = new DataSet();
cmdLoadExcel.Fill(ldExcelDS);
if (ldExcelDS.Tables[0].Rows[0].ItemArray[0].ToString() != "0")
{
Dts.Variables["User::Tab"].Value = table["TABLE_NAME"].ToString();
dataloop = tables.Rows.Count;
}
}
finally
{
dataloop++;
}
}
}
catch (Exception e)
{
Dts.TaskResult = (int)ScriptResults.Failure;
Dts.ExecutionValue = "Connection String = " + connection.ConnectionString;
}
finally
{
connection.Close();
}
}
}
}
Thank you in advance for taking a look.
Richard
sConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\";Extended Properties=\"Excel 8.0;HDR={1};IMEX=1\"", sFile);
does that work? I think the issue is you aren't building your connection string properly. billfellows.blogspot.com/2013/04/… I have JET and ACE samples in that blog post. And the other thing is to FireInformation and log what the actual value ofsConnectionString
is so you compare expected to actual. – billinkc