1
votes

I am very new to C# coding. With constant search I was able to get the below code to select the latest Excel file with a name containing the string 'Country'. Now I need to dynamically select only the first sheet, because every time I'm getting a file the sheet name changes. I have modified code as follows:

public void Main()
    {
        // TODO: Add your code here
        var directory = new     DirectoryInfo(Dts.Variables["User::VarFolderPath"].Value.ToString());


        FileInfo[] files = directory.GetFiles();
        DateTime lastModified = DateTime.MinValue;

        foreach (FileInfo file in files)
        {
            Match m = Regex.Match(file.FullName, "Country");
            if (file.LastWriteTime > lastModified && m.Success)
            {
                lastModified = file.LastWriteTime;
                Dts.Variables["User::VarFileName"].Value = file.ToString();



                string filename = (string)Dts.Variables["User::VarFileName"].Value;
                string sheetName = null;

                string connStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"EXCEL 12.0;HDR=YES;\"", filename);

                var conn = new OleDbConnection(connStr);
                try
                {
                    conn.Open();

                    using (var dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null))
                    {
                        var row0 = dtSheet.Rows[0];
                        sheetName = row0["TABLE_NAME"].ToString();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                }

                if (!String.IsNullOrEmpty(sheetName))
                {
                    bool dummy = true;
                    Dts.Variables["SheetName"].Value = sheetName;
                    Dts.Events.FireInformation(1, "User::SheetName", sheetName, "", 0, ref dummy);
                    Dts.TaskResult = (int)ScriptResults.Success;
                }
                else
                {
                    Dts.Events.FireError(0, "User::SheetName", "No SheetName found!", String.Empty, 0);
                    Dts.TaskResult = (int)ScriptResults.Failure;
                }



            }
        }
MessageBox.Show(Dts.Variables["User::VarFileName"].Value.ToString());
            MessageBox.Show(Dts.Variables["User::SheetName"].Value.ToString());

            Dts.TaskResult = (int)ScriptResults.Success;
        }

But I'm getting the following errors:

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.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
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()
2
What value you are getting in your ["User::VarFileName"] variable?Anuj Tripathi
In read write variable User::VarFileName, I am getting the latest file with name 'Country' which is in the path specified in the read variable User::VarFolderPathkeloth k

2 Answers

1
votes

Assuming that your file path is correct and you have sufficient permission on the folder; modify your code and add full path in connection instead of file name

    string fullPath = file.FullName;
    string connStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};
Extended Properties=\"EXCEL 12.0;HDR=YES;\"", fullPath);

Also, make sure all variable you have mentioned will either be readonly or read write based on the activity you are doing in your script task.

0
votes

one of the errors "Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()" is caused by a Security permission on a folder. when you change the permissions this error will go away. I just can remember the location of the .dll file.