Click here to Skip to main content
15,888,454 members
Home / Discussions / C#
   

C#

 
AnswerRe: integrate payment gate away in asp.net website Pin
OriginalGriff25-Jul-15 4:13
mveOriginalGriff25-Jul-15 4:13 
AnswerRe: integrate payment gate away in asp.net website Pin
F-ES Sitecore25-Jul-15 9:20
professionalF-ES Sitecore25-Jul-15 9:20 
AnswerRe: integrate payment gate away in asp.net website Pin
George Tourtsinakis27-Jul-15 8:53
George Tourtsinakis27-Jul-15 8:53 
QuestionHello i need help for menustrip change theme Pin
Member 1186131324-Jul-15 7:02
Member 1186131324-Jul-15 7:02 
AnswerRe: Hello i need help for menustrip change theme Pin
OriginalGriff24-Jul-15 8:11
mveOriginalGriff24-Jul-15 8:11 
AnswerRe: Hello i need help for menustrip change theme Pin
Afzaal Ahmad Zeeshan24-Jul-15 8:11
professionalAfzaal Ahmad Zeeshan24-Jul-15 8:11 
QuestionText to Audio in C#? Pin
Shailendra Singh Chauhan24-Jul-15 3:06
Shailendra Singh Chauhan24-Jul-15 3:06 
AnswerRe: Text to Audio in C#? Pin
Wendelius24-Jul-15 3:40
mentorWendelius24-Jul-15 3:40 
GeneralRe: Text to Audio in C#? Pin
Shailendra Singh Chauhan24-Jul-15 6:03
Shailendra Singh Chauhan24-Jul-15 6:03 
AnswerRe: Text to Audio in C#? Pin
Dave Kreskowiak24-Jul-15 3:48
mveDave Kreskowiak24-Jul-15 3:48 
GeneralRe: Text to Audio in C#? Pin
Shailendra Singh Chauhan24-Jul-15 6:02
Shailendra Singh Chauhan24-Jul-15 6:02 
AnswerRe: Text to Audio in C#? Pin
Afzaal Ahmad Zeeshan24-Jul-15 8:10
professionalAfzaal Ahmad Zeeshan24-Jul-15 8:10 
GeneralRe: Text to Audio in C#? Pin
Shailendra Singh Chauhan24-Jul-15 17:08
Shailendra Singh Chauhan24-Jul-15 17:08 
Questionchange focous when enter texbox Pin
Cenator23-Jul-15 23:14
Cenator23-Jul-15 23:14 
AnswerRe: change focous when enter texbox Pin
OriginalGriff23-Jul-15 23:26
mveOriginalGriff23-Jul-15 23:26 
GeneralRe: change focous when enter texbox Pin
Cenator23-Jul-15 23:54
Cenator23-Jul-15 23:54 
GeneralRe: change focous when enter texbox Pin
Dave Kreskowiak24-Jul-15 2:12
mveDave Kreskowiak24-Jul-15 2:12 
QuestionRe: change focous when enter texbox Pin
Cenator24-Jul-15 2:46
Cenator24-Jul-15 2:46 
AnswerRe: change focous when enter texbox Pin
Dave Kreskowiak24-Jul-15 3:37
mveDave Kreskowiak24-Jul-15 3:37 
GeneralRe: change focous when enter texbox Pin
Cenator24-Jul-15 3:54
Cenator24-Jul-15 3:54 
QuestionChanging IP Address with program. Pin
Member 1129387623-Jul-15 22:14
Member 1129387623-Jul-15 22:14 
AnswerRe: Changing IP Address with program. Pin
OriginalGriff23-Jul-15 23:24
mveOriginalGriff23-Jul-15 23:24 
QuestionC# program for my SSIS script task help Pin
czaar99923-Jul-15 21:16
czaar99923-Jul-15 21:16 
AnswerRe: C# program for my SSIS script task help Pin
Richard Deeming24-Jul-15 2:32
mveRichard Deeming24-Jul-15 2:32 
Something like this would work to parse the files:
C#
public sealed class FlatFileLine
{
    private sealed class MetaData
    {
        public readonly int? SampleID;
        public readonly int? RepNo;
        public readonly int? Protein;
        public readonly int? TS;
        public readonly int? Fat;
        
        public MetaData(string[] fields)
        {
            for (int index = 0; index < fields.Length; index++)
            {
                string field = fields[index].Trim();
                if (string.Equals(field, "sampleID", StringComparison.OrdinalIgnoreCase))
                {
                    SampleID = index;
                }
                else if (string.Equals(field, "rep#", StringComparison.OrdinalIgnoreCase))
                {
                    RepNo = index;
                }
                else if (string.Equals(field, "protein", StringComparison.OrdinalIgnoreCase))
                {
                    Protein = index;
                }
                else if (string.Equals(field, "TS", StringComparison.OrdinalIgnoreCase))
                {
                    TS = index;
                }
                else if (string.Equals(field, "Fat", StringComparison.OrdinalIgnoreCase))
                {
                    Fat = index;
                }
            }
        }
    }
    
    public string SampleID { get; private set; }
    public string RepNo { get; private set; }
    public string Protein { get; private set; }
    public string TS { get; private set; }
    public string Fat { get; private set; }
    
    public static IEnumerable<FlatFileLine> ParseFile(TextReader reader, string delimiter)
    {
        if (reader == null) throw new ArgumentNullException("reader");
        return ParseLines(ReadLines(reader, delimiter));
    }
    
    private static IEnumerable<string[]> ReadLines(TextReader reader, string delimiter)
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            yield return line.Split(new[] { delimiter }, StringSplitOptions.None);
        }
    }
    
    private static IEnumerable<FlatFileLine> ParseLines(IEnumerable<string[]> lines)
    {
        MetaData columns = null;
        foreach (string[] fields in lines)
        {
            if (columns == null)
            {
                columns = new MetaData(fields);
            }
            else
            {
                yield return new FlatFileLine(fields, columns);
            }
        }
    }
    
    private FlatFileLine(string[] fields, MetaData columns)
    {
        if (columns.SampleID < fields.Length)
        {
            SampleID = fields[columns.SampleID.Value];
        }
        if (columns.RepNo < fields.Length)
        {
            RepNo = fields[columns.RepNo.Value];
        }
        if (columns.Protein < fields.Length)
        {
            Protein = fields[columns.Protein.Value];
        }
        if (columns.TS < fields.Length)
        {
            TS = fields[columns.TS.Value];
        }
        if (columns.Fat < fields.Length)
        {
            Fat = fields[columns.Fat.Value];
        }
    }
}

With that in place, your Main method would look something like this:
C#
public void Main()
{
    string delimiter = Dts.Variables["User::Delimiter"].Value.ToString();
    string TableName = Dts.Variables["User::TableName"].Value.ToString();
    string SourceDirectory = Dts.Variables["User::SourceFolder"].Value.ToString();
    
    string query = @"INSERT INTO [" + TableName + "] (sampleID, [rep#], protein, TS, Fat) VALUES (@sampleID, @RepNo, @protein, @TS, @Fat)";
    
    using (SqlConnection connection = Dts.Connections["BIReports"].AcquireConnection(Dts.Transaction))
    using (SqlCommand command = new SqlCommand(query, connection))
    {
        foreach (string fileName = Directory.EnumerateFiles(SourceDirectory))
        {
            using (StreamReader reader = File.OpenText(fileName))
            {
                foreach (FlatFileLine line in FlatFileLine.ParseFile(reader, delimiter))
                {
                    command.Parameters.Clear();
                    command.Parameters.AddWithValue("@sampleID", line.SampleID ?? string.Empty);
                    command.Parameters.AddWithValue("@RepNo", line.RepNo ?? string.Empty);
                    command.Parameters.AddWithValue("@protein", line.Protein ?? string.Empty);
                    command.Parameters.AddWithValue("@TS", line.TS ?? string.Empty);
                    command.Parameters.AddWithValue("@Fat", line.Fat ?? string.Empty);
                    command.ExecuteNonQuery();
                }
            }
        }
    }
    
    Dts.TaskResult = (int)ScriptResults.Success;
}


NB: "rep#" is a bad name for a column in a SQL database, because of the "#" character. If possible, you should consider changing the name to something that only uses alpha-numeric characters - for example, "RepNo".



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: C# program for my SSIS script task help Pin
czaar99924-Jul-15 21:42
czaar99924-Jul-15 21:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.