Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is what i tried so far! but its not giving me the desired output! i want the out put Same as the CSV file. This is the CSV file.
and i need to write the output to SQL table!
Please Help!

USD,125.49,132
CAD,124.47,135.98
BHD,325.84,686.98
DKK,21.72,23.84
SEK,15.7,16.75
CHF,134.24,147.11
JPY,1.5493,1.6625
KWD,417.01,934.14
ZAR,11.14,28.18
NOK,21.24,23.35
HKD,15.97,17.06
OMR,316.59,670.88
QAR,33.76,71.14
SAR,27.78,58.62
AED,33.02,70.52
NZD,100.63,109.81
SGD,85.37,89.5




C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;


namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            bool isheader = true;
            var reader = new StreamReader(File.OpenRead(@"c:\users\afshandc\documents\visual studio 2012\Projects\ConsoleApplication10\ConsoleApplication10\ex.csv"));
            List<string> headers = new List<string>();

            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');

                if (isheader)
                {
                    isheader = false;
                    headers = values.ToList();
                }
                else
                {
                    int i = 0;
                    for (i = 0; i < values.Length; i++)
                    {
                        Console.Write(string.Format("{0} = {1};", headers[i], values[i]));
                        Console.ReadLine();
                    }

                    Console.ReadLine();

                }

            }



        }

    }

}
Posted
Updated 28-Jun-22 0:54am
v2

1 solution

Your parsing of the file seems correct, only — there are no such cases when hard-coding of the file path name could be useful. I hope this is done just on a temporary basis, for fast testing.

It looks like a problem is generating the output. You need to have two nested loops: one composing a line out of per-column values, and outer loop use to output data line-by-line. And you need to store your data after parsing in some collection. It could be the collection of line data, and each line could be just an array of strings (those you obtained from the Split method).

For composing of a line, use the class System.Text.StringBuilder:
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx[^].

Your code suffers from a number of problems. I mentioned one in the first paragraph above. Another one is this: you should declare a loop variable inside the loop:
C#
for (int index = 0; i index < values.Length; ++index) //...

but you declared it before the loop, which is not good, by a number of reasons. A loop variable should only be defined inside a loop, to prevent the possibility of accessing it.

To locate and fix problems, use the debugger. This is much more efficient than asking questions. :-)

Always use the debugger before asking questions like that. It will greatly reduce the number of questions you may want to ask, and it will improve the quality of your questions.

—SA
 
Share this answer
 
v2
Comments
Hawkeye101 24-Jul-13 3:04am    
Thanks!!
Sergey Alexandrovich Kryukov 24-Jul-13 3:18am    
My pleasure.
Good luck, call again.
—SA
Hawkeye101 24-Jul-13 5:23am    
Need a Help!

How do i write this data into a SQL database....
this is SQL query...

SELECT TOP 1000 [id]
,[code]
,[buying]
,[selling]
FROM [test].[dbo].[Currency]

This is the Code! i need to submit the [0] element to code column,[1] Element to buying column and [2] Element to Selling column.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;


namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
var reader = new StreamReader(File.OpenRead(@"c:\users\afshandc\documents\visual studio 2012\Projects\ConsoleApplication10\ConsoleApplication10\ex.csv"));
List<string> headers = new List<string>();

while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');

Console.WriteLine(values[0].ToString() + "," + values[1].ToString() + "," + values[2].ToString());
}

Console.ReadLine();

}

}

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900