Quantcast
Channel: SQL Server Integration Services forum
Viewing all articles
Browse latest Browse all 24688

Find the invalid column from the csv fileHeader in SSIS

$
0
0

Hi All,

I am creating an ssis package where i need to validate file header before loading the data in table. i created a physical table where i am storing all columns as a string and I wrote a script task where i can compare headers ( from table). This is ok but i need an exact column that is invalid in header.

E.g.  If my file has 4 columns in header i.e. Emp_ID,Name,Age,Sal then i can compare all these columns at once but unable to find that 1 column which is invalid. So next time if my file has Emp_ID,Dept_name,Age,Sal then how can i find the exact column ( dept_name_ so that i can send an email specific to this. This will be helpful because we have more than 100 columns in files.

My Script task code:

public void Main()
  {
            string FilFullPath ="";
            string HeaderRow = "";
            //string FooterRow = "";
            Int32 RowCnt = 0;
            //string RowCntFromFooter = "";
            FilFullPath = Dts.Variables["User::VarFolderPath"].Value.ToString() + "\\" +Dts.Variables["User::VarFileName"].Value.ToString();
            //Check if file path is correct
             //MessageBox.Show(FilFullPath);
            try
            {
                string[] lines = System.IO.File.ReadAllLines(FilFullPath);
                // Display the file contents by using a foreach loop.
                foreach (string line in lines)
                {
                    // Use a tab to indent each line of the file.
                    //MessageBox.Show(line);
                    RowCnt += 1;
                    if (RowCnt == 1)
                    {
                        HeaderRow = line;
                    }
                }
                //FooterRow = lines[RowCnt - 1].ToString();
                //RowCntFromFooter = FooterRow.Replace("RowCnt","").Trim();
                //Used only for Debugging�
                //MessageBox.Show("Row Cnt from File Foooter::" + RowCntFromFooter);
                //MessageBox.Show("Row Cnt from File ::" + RowCnt.ToString());
                //MessageBox.Show("HEader Row from File::" + HeaderRow);
                //MessageBox.Show(" Footer Row::" + lines[RowCnt - 1].ToString());
                //MessageBox.Show("Header from Definition table" + Dts.Variables["User::VarHeader"].Value.ToString());
                //Validation
               
                if (Dts.Variables["User::VarHeader"].Value.ToString() != HeaderRow)
                {
                    Dts.Variables["User::VarErrorMessage"].Value = "File Header Row does not Match";
                    //if (RowCnt != Convert.ToInt32(RowCntFromFooter))
                    //{
                    //    Dts.Variables["User::VarErrorMessage"].Value = "Header does not match as well Footer Row Count(" +RowCntFromFooter+ ") does not match with total file Row count(" +RowCnt.ToString() +")";
                    //}
                    Dts.Variables["User::VarValidationFlg"].Value = 0;
                    Dts.Variables["User::VarFileHeader"].Value = HeaderRow;
                }
                if (Dts.Variables["User::VarHeader"].Value.ToString() == HeaderRow) /*&& RowCnt == Convert.ToInt32(RowCntFromFooter))*/
                {
                    Dts.Variables["User::VarErrorMessage"].Value = "No Error : File Header Row  matches";
                    Dts.Variables["User::VarValidationFlg"].Value = 1;
                    Dts.Variables["User::VarFileRowCnt"].Value = RowCnt;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            Dts.TaskResult = (int)ScriptResults.Success;
  }


Shakky


Viewing all articles
Browse latest Browse all 24688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>