When we are working with the CSV files and that CSV files we are getting from the different sources. Sometimes we are getting the less number of columns from some sources. In this case, our packages get failed.
Below is the example.
This is the correct file. But sometimes we are getting files with missing columns like below.
In this file we are getting two missing columns (
TotalDiscount and DeliveryCharge). In this case our package is getting failed.
To avoid package failure First of all we need to check
the required number of columns. If files not having correct columns in this
case we cannot process that file. We will log these files and send it to the
Respective team.
In the processing directory, we are getting the files.
Now we are designing the package to handle this case.
SCR_File_Validation
Script task is used the validate the files.
If file is
having 5 columns then that is correct file.
Note : we
are assuming that first line is the columns name.
Taking some variables to store the store the file name and columns count.
We are writing the below code in the script task.
public void Main() { // TODO: Add your code here //User::File_Path using (var reader = new StreamReader(Dts.Variables["User::File_Path"].Value.ToString())) {
var line = reader.ReadLine(); var Columns = line.Split(','); int i = Columns.Length;
Dts.Variables["User::Number_of_columns"].Value = i; } Dts.TaskResult
= (int)ScriptResults.Success; } |
Based on the column counts we are redirecting the flow.
If column counts are 5 then we are loading the data using Data Flow Task. If not in this case we can load that file and send that information to the respective team.
As of now if we are not getting the expected number of
column we are showing the message.
public void Main() { // TODO: Add your code here MessageBox.Show(Dts.Variables["User::File_Path"].Value.ToString() + " Is not a valid
file. Columns are missing in this file"); Dts.TaskResult
= (int)ScriptResults.Success; } |
Now our package is ready to test.
See the records in the table.
Running the package now.
Package executed successfully.
See the records in the table.
Got the expected result.
No comments:
Post a Comment
If you have any doubt, please let me know.