How would you revise the code to use a loop to read in all the CSV files from a directory, one CSV file at a time in C# using VS2010?

I am trying to write a method for my first ASP.Net web application using C# with VS2010.

In the following method, how would you revise the code to use a loop to read in all the CSV files from a directory, one CSV file at a time. Currenly my method only reads and processes 1 CSV file.

        protected void Button3_Click(object sender, EventArgs e)
        {  
            //String[] oFiles1 = Directory.GetFiles(@"U:\CSVFiles\", "*.csv");
           
            string fileName = @"U:\CSVFiles\101 Internal  08-28-2013_Book Credits (D).csv";
           
            FileInfo info = new FileInfo(fileName);
            string tableName = info.Name;
           
            List<string> rows = new List<string>();
           
            StreamReader reader = new StreamReader(fileName);
            string record = reader.ReadLine();
            while (record != null)
            {
                rows.Add(record);
                record = reader.ReadLine();
            }
                     
            List<string[]> rowObjects = new List<string[]>();

            int maxColsCount = 0;
            foreach (string s in rows)
            {
                string[] convertedRow = s.Split(new char[] { ',' });
                if (convertedRow.Length > maxColsCount)
                    maxColsCount = convertedRow.Length;
                rowObjects.Add(convertedRow);
            }        
        }
zimmer9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Firstly put the part that loads the data into the database in its own function
protected void UploadToDB(string fileName)
{
         FileInfo info = new FileInfo(fileName);
            string tableName = info.Name;
           
            List<string> rows = new List<string>();
           
            StreamReader reader = new StreamReader(fileName);
            string record = reader.ReadLine();
            while (record != null)
            {
                rows.Add(record);
                record = reader.ReadLine();
            }
                     
            List<string[]> rowObjects = new List<string[]>();

            int maxColsCount = 0;
            foreach (string s in rows)
            {
                string[] convertedRow = s.Split(new char[] { ',' });
                if (convertedRow.Length > maxColsCount)
                    maxColsCount = convertedRow.Length;
                rowObjects.Add(convertedRow);
            }        
}

Open in new window

Then change your button click to loop through the files in the folder and call this function
protected void Button3_Click(object sender, EventArgs e)
{  
    string filepath = @"U:\CSVFiles\";
    DirectoryInfo d = new DirectoryInfo(filepath);
    foreach (var file in d.GetFiles("*.csv"))
    {
          UploadToDB(file.FullName);
    }
}

Open in new window

I have not tested the code but that should be basically what you want.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.