Wednesday, November 17, 2021

Web.Config Base64 Encoding With Simple Salt (and decoding)

Title: Base64 Encoding With Simple Salt (and decoding)


Console App-

...App.config

  <appSettings>

    <add key="SoapHeader" value="http://mySvc.com:7147/ws/evoCorp/Header" />

    <add key="SoapLines" value="http://mySvc.com:7147/ws/evoCorp/Lines" />

    <add key="SoapPosted" value="http://mySvc.com:7147/ws/evoCorp/Posted" />

<add key="domainName" value="myDomain" />

<add key="domainUser" value="myServiceUser"/> 

<add key="domainHash" value="myServiceAuth"/>

<add key="domainMode" value="test"/>

  </appSettings>


...Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
 
namespace ConsoleBase64x
{
    internal class Program
    {
        static int Main(string[] args)
        {   
            //"8>4D>?]aX+#+A2k" EncodeWithoutSalt = OD40RD4/XWFYKyMrQTJr
            //"8>4D>?]aX+#+A2k" EncodeWithSalt = MTU4PjREPj9dYVgrIytBMms=
            Console.WriteLine("Enter the setting name in the app.config to encrypt the setting's value. Ex: domainHash. Then press {enter}.");
            var sKeyName = Console.ReadLine();
            if (string.IsNullOrEmpty(sKeyName))
            {
               Console.WriteLine("The setting name in the app.config was not found or was empty. The program will now exit.");
               Console.Read();
                return -1; 
            }
            var sHashword = SettingsConfigHelper.GetAppSetting(sKeyName);
            Console.WriteLine("original: " + sHashword);
            var sHashword64encoded = SettingsConfigHelper.Base64EncodeWithSalt(sHashword);
            Console.WriteLine("Base64EncodeWithSalt: " + sHashword64encoded);
            Console.WriteLine("Highlight the value above, copy it to notepad, and Press any key to decode back to original value.");
            Console.Read();
            var sHashwordDecoded = SettingsConfigHelper.Base64DecodeWithSalt(sHashword64encoded);
            Console.WriteLine("Base64DecodeWithSalt: " + sHashwordDecoded);
            Console.WriteLine("If this value is the same as the original value in the App.Config, you can use it in the Control Console Web.Config.");
            Console.WriteLine("Press any key to exit.");
            Console.Read();
            Console.Read();
            return 0;
 
            //byte[] imageBytes = SettingsConfigHelper.FromBase64Bytes(sHashword);
            //var fstrm = new FileStream(@"C:\winnt_copy.bmp", FileMode.CreateNew, FileAccess.Write);
            //BinaryWriter writer = new BinaryWriter(fstrm);
            //Console.Write(imageBytes);
            //writer.Close( );
        }
 
    }
}

...SettingsConfigHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleBase64x
{
     public static class SettingsConfigHelper
    {
 
        /* GET CONFIG KEY VALUE */
        public static string GetAppSetting(string sKey) 
        {
            if (sKey == null)
            {
                return null;
            }
            var sValue = System.Configuration.ConfigurationManager.AppSettings[sKey];
            return sValue ?? string.Empty;
        }
        /* ENCODE STRING */
        public static string Base64Encode(string plainText) 
        {
            if (plainText == null)
            {
                return null;
            }
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
        /* DECODE STRING */
        public static string Base64Decode(string base64EncodedData) 
        {
            if (base64EncodedData == null)
            {
                return null;
            }
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.ASCII.GetString(base64EncodedBytes);
        }
 
        /* ENCODE STRING with salt */
        public static string Base64EncodeWithSalt(string plainText) 
        {
            if (plainText == null)
            {
                return null;
            }
            var iLen = plainText.Length;
            var sLen = "00";
            if (iLen < 10) 
            {
                sLen = string.Concat("0", iLen.ToString());
            }
            else
            {
                sLen = iLen.ToString();
            }
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(sLen + plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
        /* DECODE STRING with salt*/
        public static string Base64DecodeWithSalt(string base64EncodedData) 
        {
            if (base64EncodedData == null)
            {
                return null;
            }
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            var sValue = System.Text.Encoding.ASCII.GetString(base64EncodedBytes);
            sValue = sValue.Substring(2);
            return sValue;
        }
 
 
        /* Decoding an encoded base64 string by using static Convert.FromBase64CharArray method. 
         * This method returns a byte[] that contains the decoded elements of the string.
         * If you receive a file via email, such as an image file (.bmp), that has previously been converted to a string, 
         * to convert it back into its original bitmap file, you could do something like the following:
         */
        public static byte[] Base64DecodeString(string inputStr) 
        {
            byte[] decodedByteArray = Convert.FromBase64CharArray(inputStr.ToCharArray(), 0, inputStr.Length);
            return (decodedByteArray);
        }
 
        /* Convert a Base64 encoded string into a byte[] use Convert.FromBase64String 
         * Base64 is always ascii text. So just do Encoding.ASCII.GetString(base64arr)
         */
        public static byte[] FromBase64Bytes(this byte[] base64Bytes)
        {
            string base64String = Encoding.UTF8.GetString(base64Bytes, 0, base64Bytes.Length);
            return Convert.FromBase64String(base64String);
        }
 
    }
}









XPCMD_SHELL Move & Load

 XPCMD_SHELL Move & Load Data from Directory

USE [myETL]

GO

/****** Object:  StoredProcedure [dbo].[LoadMyRxData]    Script Date: 10/22/2021 ******/

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[LoadMyRxData]') AND type in (N'P', N'PC'))

DROP PROCEDURE [dbo].[LoadMyRxData]

GO

/****** Object:  StoredProcedure [dbo].[LoadMyRxData]    Script Date: 10/22/2021 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE procedure [dbo].[LoadMyRxData]

AS

exec xp_cmdshell 'move \\DatabaseServer07\FTP\DataReporting\*PatDetails* \\DatabaseServer01\F\docs\DbName\MyRxData\'--,no_output

--Load Raw files

declare @dir table(itm varchar(500),ID int identity)

declare @pth varchar(1000) = '\\DatabaseServer01\f\docs\DbName\MyRxData\'

declare @cmd varchar(max)

declare @fl varchar(1000)

delete from @dir

insert @dir(itm)

exec xp_cmdshell 'dir /b \\DatabaseServer01\f\docs\DbName\MyRxData\*PatDetails*dat'


while (select COUNT(*) from @dir where isnull(itm,'File Not Found')<>'File Not Found')>0

begin


select top 1 @fl = itm from @dir where itm is not null order by itm

if(OBJECT_ID('tempdb..##FL_MyRxData') is not null) drop table ##FL_MyRxData

create table ##FL_MyRxData(dta varchar(max))

set @cmd='bulk insert ##FL_MyRxData from '''+@pth+@fl+''' with(RowTerminator=''0x0a'')'

exec(@cmd)

if(OBJECT_ID('tempdb..##PharmRx') is not null) drop table ##PharmRx

select top 1 @cmd= dta from ##FL_MyRxData where CHARINDEX('CLIENT_ID',dta)<>0

set @cmd=REPLACE(@cmd,' ','')

set @cmd=REPLACE(@cmd,'&','')

set @cmd=REPLACE(@cmd,'/','')

set @cmd=REPLACE(@cmd,',','')

set @cmd=REPLACE(@cmd,'|',' varchar(1000),')

if(RIGHT(@cmd,1)<>'|') set @cmd = @cmd+' varchar(1000)'

set @cmd = 'create table ##PharmRx('+@cmd+')'

exec(@cmd)

set @cmd='bulk insert ##PharmRx from '''+@pth+@fl+''' with(RowTerminator=''0x0a'',fieldterminator=''|'')'

exec(@cmd)

delete from ##PharmRx where CLIENT_ID='CLIENT_ID'

alter table ##PharmRx add FLENAME varchar(500)

update ##PharmRx set FLENAME=@fl


--prevent loading duplicate records

delete from ##PharmRx where

CLIENT_ID+PHARMACY+RX_NBR+FILL_NBR+CLAIM_STATUS+ADJUD_DT

in (select CLIENT_ID+PHARMACY+RX_NBR+FILL_NBR+CLAIM_STATUS+ADJUD_DT from DbName.dbo.PharmacyRx) 


--Load the data

declare @ts datetime=getdate()


insert DbName.dbo.PharmacyRx

(CLIENT_ID,

HRSA_ID,

ENTITY,

CLIENT,

CLINIC,

DEPARTMENT,

LAST_NAME,

FIRST_NAME,

DOB,

ID,

CLAIM_TYPE,

RX_NBR,

FILL_NBR,

ADJUD_DT,

PHARMACY,

PHARMACY_LOCATION,

PRESCRIBER_NAME,

PHARMACY_NPI,

GROUP_ID,

GROUP_NAME,

PRESCRIBER_NPI,

E_RX_FLAG,

SOURCE_TYPE,

THERAPEUTIC_CLASS_DESC,

CLASS_IND,

DRG_TYP,

NDC,

DRG_LBL_NM,

QTY_DISP,

DOS,

UNIT_COST_340B,

COST_340B,

AWP_PRICE,

AWP_COST,

RX_WRITTEN_DT,

FILL_SOLD_DTTM,

CLAIM_STATUS,

PLN_AR_AMT,

COPAY_AMT,

SALES_TAX,

ADMN_FEE,

DISP_FEE,

THRDPTY_FEE,

CLIENT_FEE,

TOT_DUE_WAG,

BillingMonth340B,

FLENAME,

LoadDT)

select

CLIENT_ID,

HRSA_ID,

ENTITY,

CLIENT,

CLINIC,

DEPARTMENT,

LAST_NAME,

FIRST_NAME,

DOB,

ID,

CLAIM_TYPE,

RX_NBR,

FILL_NBR,

ADJUD_DT,

PHARMACY,

PHARMACY_LOCATION,

PRESCRIBER_NAME,

PHARMACY_NPI,

GROUP_ID,

GROUP_NAME,

PRESCRIBER_NPI,

E_RX_FLAG,

SOURCE_TYPE,

THERAPEUTIC_CLASS_DESC,

CLASS_IND,

DRG_TYP,

NDC,

DRG_LBL_NM,

QTY_DISP,

DOS,

UNIT_COST_340B,

COST_340B,

AWP_PRICE,

AWP_COST,

RX_WRITTEN_DT,

FILL_SOLD_DTTM,

CLAIM_STATUS,

PLN_AR_AMT,

COPAY_AMT,

SALES_TAX,

ADMN_FEE,

DISP_FEE,

THRDPTY_FEE,

CLIENT_FEE,

TOT_DUE_WAG,

Billing_Month_340B,

FLENAME,

@ts LoadDT

from

##PharmRx cc


truncate table ##PharmRx 


--move the file to the archive

set @cmd='exec xp_cmdshell ''move "'+@pth+@fl+'"'

set @cmd=@cmd+' '+@pth+'archive\'''

exec(@cmd)

--find next file

delete from @dir

insert @dir(itm)

exec xp_cmdshell 'dir /b \\DatabaseServer01\f\docs\DbName\PharmRX\*PatDetails*dat'

end

select FleName,LoadDT,count(*) from DbName.dbo.PharmacyRx group by FleName,LoadDT order by 2 desc

GO



SSIS ScriptTask Main_Script_Executable.cs

Purpose: SSIS Package Main Script Executable with DB Connectivity Verification and Error logging

#region Namespaces
using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.VisualBasic.FileIO;
using System.IO;
using System.Diagnostics;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading;

#endregion

namespace CP_AutoTerminations
{
/// ScriptMain entry point class. 
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
private DataTable _dErrorsTable;
        private string _messageData;
private string _fileNameOut;
//private ConnectionManager _connectMgr;   
public DataTable ErrorsTable 
{
get {
if (_dErrorsTable == null) _dErrorsTable = new DataTable();
if (_dErrorsTable.Columns.Count == 0) _dErrorsTable.Columns.Add(new DataColumn("Errors"));
return _dErrorsTable; } 
set => _dErrorsTable = value;
}
public string MessageData
{
get { return _messageData ?? "" ; }
set => _messageData = value;
}
public string FileNameOut
{
            get { return _fileNameOut; }
set => _fileNameOut = value;
}
/// <summary>
/// This method is called when this script task executes in the control flow.
/// and sets the value of Dts.TaskResult to indicate success or failure.
/// </summary>
public void Main()
        {
            DataTable csvData = new DataTable();
            StringBuilder sb = new StringBuilder();
try
{
//SetLogFile(false);
                //string fileNameOut = this.FileNameOut;
                StringWriter sWriter;
                string sQuery;
string sQuerySuffix;
int rsNumber = 0;

this.MessageData = string.Empty;
string fileNameIn = (string)ReadVariable("User::InputDirPath").ToString();
                //string fileNameEx = (string)ReadVariable("User::OutputExPath").ToString();
csvData = GetDataTableFromCsvFile(fileNameIn);

                if (csvData == null || Dts.TaskResult == 1 || ErrorsTable.Rows?.Count > 0)
{
                    //if error occurred, InputDirPath has changed, reset it here, before the return;
                    SetLogFile(true);
if (ErrorsTable.Rows.Count > 0)
{
                        foreach (DataRow row in ErrorsTable.Rows)
                        {
                            this.MessageData += " - " + row.ItemArray[0].ToString();
                        }

WriteVariable("User::OutputData", this.MessageData);
                    }
return; // EXIT EXIT EXIT
}

                // debug //
                //using (StreamWriter w = File.AppendText(this.FileNameOut))
                //{
                //    var msg = $"Acquiring Db Connection: " + Dts.Connections["DatabaseConnection"].ConnectionString;
                //    LogEntry(msg, w);
                //}

ConnectionManager cm;
                OleDbConnection sqlConn;
cm = Dts.Connections["DatabaseConnection"];
                sqlConn = (OleDbConnection)cm.AcquireConnection(Dts.Transaction);
                //sqlConn.ConnectionTimeout = 0;
                Thread.Sleep(1000);
                //string fileXmlOut = fileNameOut;

                // debug //
                //if (sqlConn != null)
                //{
                //    using (StreamWriter w = File.AppendText(this.FileNameOut))
                //    {
                //        var msg = "Acquired Db Connection.";
                //        LogEntry(msg, w);
                //    }
                //}

                //Read from Datatable and process each row by Execute sproc 
foreach (DataRow cvrRow in csvData.Rows)
                {
                    // RESET FILE NAME, each
                    //this.FileNameOut = fileNameOut;

                    try
                    {

                        if ((string) cvrRow.ItemArray[0].ToString().ToLower() == "shortname") continue;
                            
                        sQuerySuffix = "@name='[0]', @Acc='[1]', @NPI='[2]', @TermAll=0";
                        sQuerySuffix = sQuerySuffix.Replace("[0]", (string) cvrRow.ItemArray[0]);
                        sQuerySuffix = sQuerySuffix.Replace("[1]", (string) cvrRow.ItemArray[1]);
                        sQuerySuffix = sQuerySuffix.Replace("[2]", (string) cvrRow.ItemArray[2]);
                        //sQuerySuffix = sQuerySuffix.Replace("[3]",  cvrRow.ItemArray[3].ToString().Trim());
                        sQuerySuffix = sQuerySuffix.Replace("'NULL'", "NULL");

                        // example [Invoice_Process].dbo.[spInvManualRunTerm] @name=N'UAMS1',@Acc=NULL,@NPI=NULL, @TermAll=0;
                        sQuery = string.Concat(@"EXECUTE [InvoiceDB].dbo.[spInvManualRunTerm] ", sQuerySuffix, ";");
                        //MessageBox.Show(query);
                        // debug
                        //using (StreamWriter w = File.AppendText(this.FileNameOut))
                        //{
                        //    LogEntry(sQuery, w);
                        //}

                        var dResultTable = (new DataSet("AutoTermsResult")).Tables.Add("Logging");
                        sWriter = new StringWriter();
                        ++rsNumber;
                        using (var cmd = new OleDbCommand(sQuery, sqlConn))
                        {
                            Thread.Sleep(1000);
                            using (var da = new OleDbDataAdapter(cmd))
                            {
                                da.Fill(dResultTable);
                                dResultTable.WriteXml(sWriter);
                                Thread.Sleep(1000);
                            }
                        }

                        if (dResultTable?.Rows?.Count > 0)
                        {
                            sb = new StringBuilder();
                            sb.AppendLine(string.Join(",", cvrRow.ItemArray));

                            this.MessageData = string.Concat(@"Term Results for:" , sb.ToString(), Environment.NewLine , sWriter.ToString(), Environment.NewLine);
                           
                            if (this.MessageData.Contains("Exception") || this.MessageData.Contains("Errors"))
                            {
                                --rsNumber;
                                this.ErrorsTable.Rows.Add(new object[] { this.MessageData });
                            }
                            else  // write good xml output
                            {
                                using (StreamWriter w = File.AppendText(this.FileNameOut))
                                {
                                    LogEntry(this.MessageData, w);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        --rsNumber;
                        this.ErrorsTable.Rows.Add(new object[] { ex.Message });
                        this.LogResults(ex.Message, true);  
                              //<< CHANGES THE LOGFILE DIRECTORY to /EXCEPTION
                    }
                }

                WriteVariable("User::OutputData", this.MessageData);

                sqlConn?.Close();
                Dts.ExecutionValue = rsNumber;
Dts.TaskResult = (int)ScriptResults.Success;
            }
catch (Exception ex)
{
                //catch possible null EX, it has happened.
                if (ex.Equals(null)) ex = new Exception("InvManualTerm Package.Main. Unknown exception in script task main function.");
WriteVariable("User::OutputData", ex.Message);
ThrowAndFail(ex.Message);
}
finally
{
if (Dts.TaskResult == 1 || ErrorsTable.Rows?.Count > 0)
{
                    if (ErrorsTable.Rows?.Count > 0)
                    {
                        //RESET MessageData
                        this.MessageData = "Parameters: ";
                        if (csvData?.Rows.Count > 0)
                        {
                            foreach (DataRow dr in csvData.Rows)
                            {
                                sb.AppendLine(string.Join(",", dr.ItemArray));
                            }
                        }
                        else
                        {
                            sb.AppendLine("null");
                        }

                        foreach (DataRow dr in ErrorsTable?.Rows)
                        {
                            sb.AppendLine(dr.ItemArray[0].ToString());
                        }
                        this.MessageData += sb.ToString();
                        WriteVariable("User::OutputData", this.MessageData);
}

this.LogResults(this.MessageData, true); 
                       //<< CHANGES THE LOGFILE DIRECTORY to /EXCEPTION
                    SetParmsDestination(true);
                    //Dts.TaskResult = (int) ScriptResults.Failure;
                }
                else
                {
                    SetParmsDestination(false);
                    //Dts.TaskResult = (int) ScriptResults.Success;
                }
 
}
            Dts.TaskResult = (int) ScriptResults.Success;  
           //let it succeed either way and send email notification
}

public void SetLogFile(bool isError)
{         
bool bFixedName = false;
                string sDir;
this.FileNameOut = ReadVariable("User::OutputDirPath").ToString();
if (string.IsNullOrEmpty(this.FileNameOut))
{
bFixedName = true;
this.FileNameOut = string.Concat(@"\\server01\Backup\Terms\InvTermsParameters_", DateTime.Now.ToString("yyyyMMddHHmm"), ".log");
                    sDir  = FileNameOut.Substring(0,FileNameOut.LastIndexOf('\\'));
                    //sDir = Path.GetDirectoryName(this.FileNameOut); //this.FileNameOut.Substring(0,this.FileNameOut.LastIndexOf(@"\"))
                    if (!string.IsNullOrEmpty(sDir) && !Directory.Exists(sDir))
                    {
                        try
                        {
                            Directory.CreateDirectory(sDir);
                        }
                        catch(Exception ex)
                        {
                            bFixedName = false;
                            this.MessageData += Environment.NewLine + ex.ToString();
                        }
                    }
}

if (!this.FileNameOut.Contains(DateTime.Now.ToString("yyyy")))
{
bFixedName = true;
this.FileNameOut = this.FileNameOut.Replace(".log", string.Concat("_", DateTime.Now.ToString("yyyyMMddHHmm"), ".log"));
// fileNameOut format ex: \\Server01\Backup\Terms\InvTermsOutput_2020042901.log

}

                if (isError.Equals(true))
                {
                    bFixedName = true;
                    sDir = this.FileNameOut.Replace(@"\BACKUP\", @"\EXCEPTIONS\");
                    sDir  = sDir.Substring(0,sDir.LastIndexOf('\\'));
                    if (!string.IsNullOrEmpty(sDir) && !Directory.Exists(sDir))
                    {
                        try
                        {
                            Directory.CreateDirectory(sDir);
                        }
                        catch(Exception ex)
                        {
                            this.MessageData += Environment.NewLine + ex.ToString();
                        }
                    }

                    if (!string.IsNullOrEmpty(sDir) && Directory.Exists(sDir))
                    {
                        this.FileNameOut = this.FileNameOut.Replace(@"\BACKUP\", @"\EXCEPTIONS\");
                    } //else don't change it, logs will dump in the BACKUP folder

                    WriteVariable("User::OutputExPath", this.FileNameOut);
                    //this.FileNameOut = ReadVariable("User::OutputExPath").ToString();
                    // fileNameOut format ex: \\Server01\Incoming\Terms\InvTermsOutput_2020042901.log
                    //  EX. UNC: \\Server01\EXCEPTIONS\Terms\InvTermsOutput_2020042901.log
                    using (StreamWriter w = File.AppendText(this.FileNameOut))
                    {
                        LogEntry("SetLogFile.isError=True", w);
                    }
                }

                if (bFixedName.Equals(true))
{ 
WriteVariable("User::OutputDirPath", this.FileNameOut);
}

}
//Example:  object myVarValue = "Abcd"; WriteVariable("User::MyVar", myVarValue);

        private void SetParmsDestination(bool IsError)
        {
            if (string.IsNullOrEmpty(this.FileNameOut))
            {
                SetLogFile(IsError);
            }
            // get base input file variable, and move it, if previous attempt has failed
            string fileParmsIncoming = (string)ReadVariable("$Package::InputFilePath").ToString();
            if (File.Exists(fileParmsIncoming)) //then move it
            {
                //prepare destination
                string fileDestination = this.FileNameOut.Replace(".log", ".csv");
                if (IsError.Equals(true))
                {
                    fileDestination = fileDestination.Replace(@"\BACKUP\", @"\EXCEPTIONS\");
                }

                if (File.Exists(fileDestination)) //then rename it
                {
                    fileDestination = fileDestination.Replace(".csv", string.Concat("_", DateTime.Now.ToString("yyyyMMddHHmm"), ".csv"));
                }

                File.Move(fileParmsIncoming, fileDestination);
                Thread.Sleep(1000);
            }
        }

private void WriteVariable(string varName, object varValue)
{
try
{
Variables vars = null;
Dts.VariableDispenser.LockForWrite(varName);
Dts.VariableDispenser.GetVariables(ref vars);
try
{
vars[varName].Value = varValue;
}
finally
{
vars.Unlock();
}
}
catch (Exception ex)
{
throw;
}
}
//Example:  object myVarValue = ReadVariable("User::MyVar");
private object ReadVariable(string varName)
{
var result = new object();
try
{
result = null;
Variables vars = null;
Dts.VariableDispenser.LockForRead(varName);
Dts.VariableDispenser.GetVariables(ref vars);
try
{
result = vars[varName].Value;
}
finally
{
vars.Unlock();
}
}
catch (Exception)
            {
throw;
}
 
return result;

}
private DataTable GetDataTableFromCsvFile(string csvFilePath)
{
            string sError;
DataTable csvData = (new DataSet("AutoTermsResult")).Tables.Add("Logging");
            try
{
                using (TextFieldParser csvReader = new TextFieldParser(csvFilePath))
                {
                    csvReader.SetDelimiters(new string[] {","});
                    csvReader.HasFieldsEnclosedInQuotes = false;
                    string[] colFields = csvReader.ReadFields();
                    if (colFields != null)
                    {
                        foreach (var column in colFields)
                        {
                            DataColumn dc = new DataColumn(column);
                            dc.DefaultValue = "";
                            dc.AllowDBNull = false;
                            csvData.Columns.Add(dc);
                        }

                        while (!csvReader.EndOfData)
                        {
                            string[] fieldData = csvReader.ReadFields();
                            if (fieldData == null) continue;
                            //Making empty value as null
                            int j = 0;
                            int i = 0;
                            for (i = 0; i < fieldData.Length; i++)
                            {
                                var s = fieldData[i].Trim();
                                if (string.IsNullOrEmpty(s))
                                {
                                    s = "NULL";
                                    j++;
                                }
                                else
                                {
                                    s= fieldData[i].Trim();
                                }
                                fieldData[i] = s;
                            }
                            csvData.Rows.Add(fieldData);
                            // VALIDATION: NO VALUES, ALL NULL VALUES, MISSING FIELDS
                            if (i==0 || j > 0 || fieldData.Length < 3)
                            {
                                sError = "CSV input file failure. DATA VALUES VALIDATION. Data values are required for name,Acc,NPI,TermAll.";
                                ThrowAndFail(sError);
                                return csvData;
                            }
                        }
                    }
                }

                // COLUMN COUNT VALIDATION
                if (csvData.Columns?.Count < 4)
                {
                    sError = "CSV input file failure. COLUMN COUNT VALIDATION. Header columns required: name,Acc,NPI,TermAll.";
                    ThrowAndFail(sError);
                }
                // COLUMN 1 VALIDATION
if (csvData.Columns[0].ColumnName != "name")
{
sError = "CSV input file failure. COLUMN 1 VALIDATION. Column1 is Name.";
ThrowAndFail(sError);
}
                // COLUMN 2 VALIDATION
if (csvData.Columns[1].ColumnName != "Acc")
{
                    sError = "CSV input file failure. COLUMN 2 VALIDATION. Column2 is Acc.";
ThrowAndFail(sError);
}
                //COLUMN 3 VALIDATION
if (csvData.Columns[2].ColumnName != "NPI")
{
                    sError = "CSV input file failure. COLUMN 3 VALIDATION. Column3 is NPI.";
ThrowAndFail(sError);
}
                //COLUMN 4 VALIDATION
if (csvData.Columns[3].ColumnName != "TermAll")
{
                    sError = "CSV input file failure. COLUMN 4 VALIDATION. Column4 is TermAll.";
ThrowAndFail(sError);
}
                // ROW COUNT VALIDATION
                if (csvData.Rows.Equals(null) || csvData.Rows.Count < 1)
                {
                    sError = "CSV input file failure. ROW COUNT VALIDATION. Column Header row and at least 1 data row are required.";
                    ThrowAndFail(sError);
                }

                //StringWriter sWriter = new StringWriter();
                //csvData.WriteXml(sWriter);
                //using (StreamWriter w = File.AppendText(this.FileNameOut))
                //{
                //    LogEntry(sWriter.ToString(), w);
                //}
            }
catch (Exception ex)
{
WriteVariable("User::OutputData", ex.Message);
ThrowAndFail(ex.Message);
                //Dts.TaskResult = (int)ScriptResults.Failure;
//throw ex;
}
return csvData;
}

private void ThrowAndFail(string message)
{
this.ErrorsTable.Rows.Add(new object[] { message });
Dts.Events.FireError(0, "InvManualRunTerm Package.Error", message + Environment.NewLine, string.Empty, 0);
//Dts.TaskResult = (int)ScriptResults.Failure;
}

#region ScriptResults declaration
/// <summary>
/// This enum provides shorthand class for setting the result of the script.
/// </summary>
        private enum ScriptResults
{
Success = DTSExecResult.Success,
Failure = DTSExecResult.Failure
};
#endregion

public void LogResults(string fileData, bool isError)
{   
//<?xml version = "1.0" encoding = "UTF-8"?>
bool errorOccurred = false; //in event all logging methods fail
try
{
SetLogFile(isError);

if (String.IsNullOrEmpty(fileData) || fileData.ToLower().Contains("system.object"))
{
fileData = "OutputData value is empty. Nothing to log.";
WriteVariable("User::OutputData", fileData);
}

using (StreamWriter w = File.AppendText(this.FileNameOut))
{
LogEntry(fileData, w);
}

//using (StreamReader r = File.OpenText(this.FileNameOut))
//{
// DumpLogToConsole(r);
//}

}
catch (Exception ex)
{
errorOccurred = true; // possible network logging issue. try to save to event log.
ThrowAndFail(ex.Message);
WriteVariable("User::OutputData", ex.Message);
using (EventLog eventLog = new EventLog("Application"))
{
eventLog.Source = "Application";
eventLog.WriteEntry(string.Concat("InvManualTerm Package.Error. ", Environment.NewLine, ex.Message.ToString()), EventLogEntryType.Error, 101, 1);
}
//Dts.TaskResult = (int)ScriptResults.Failure;
}
finally
{

if (errorOccurred) // fire event to store in SQL DTS log
{
Dts.Events.FireError(18, "InvManualTerm Package.Error", "An Exception was caught during logging write and output data flush. Check server event application log for details.", "", 0);
//Dts.TaskResult = (int)ScriptResults.Failure;
}
}
}

public static void LogEntry(string logMessage, TextWriter w)
{
            w.Write(Environment.NewLine + "ScriptTaskMain: " + $"{DateTime.Now:yyyyMMdd} : {DateTime.Now.ToLongTimeString()}");
            w.WriteLine($" :{logMessage}");
        }

public static void DumpLogToConsole(StreamReader r)
{
string line;
while ((line = r.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}





public partial class  CheckConnection
{
private ConnectionManager connectionManager;
//"OLEDB", "ADO.NET:SQL", "ADO.NET:OLEDB"
public void CreateOLEConnection(Package p, string csType)
{
connectionManager = p.Connections.Add(csType);
if (csType == "") 
//Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security Info=False;User ID="";Initial Catalog=myDatabase;Data Source=Server01
{  //Server=MYSERVER;Provider=SQLOLEDB.1;Pwd= xxxxxxx;User ID= xxxxx;Initial Catalog=mySource;OLE DB Services=-2"
connectionManager.ConnectionString = @"Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=myDatabase;Data Source=Server01;";
}
if (csType == "OLEDB") 
{
connectionManager.ConnectionString = @"Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=myDatabase;Data Source=Server01;Auto Translate=False;";
}
connectionManager.Name = "SSIS Connection Manager";
connectionManager.Description = "DB connection";
}

}
}