在C#中读取CSV并写入数据库 [英] Reading CSV in C# and writing to database

查看:89
本文介绍了在C#中读取CSV并写入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止所尝试的!但它没有给我想要的输出!我想要输出相同的CSV文件。这是CSV文件。

我需要将输出写入SQL表格!

请帮助!



 USD,125.49,132 
CAD,124.47,135.98
BHD,325.84,686.98
DKK,21.72,23.84
SEK,15.7, 16.75
瑞士法郎,134.24,147.11
日元,1.5493,1.6625
KWD,417.01,934.14
ZAR,11.14,28.18
挪威克朗,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









 使用系统; 
使用 System.Collections.Generic;
使用 System.IO;
使用 System.Linq;
使用 System.Text;


命名空间 ConsoleApplication10
{
class 程序
{
静态 void Main( string [] args)
{
bool isheader = true ;
var reader = new StreamReader(File.OpenRead(@& quot; c:\\ \\ users\afshandc\documents\visual studio 2012 \Projects\ConsoleApplication10 \ConsoleApplication10 \exc.cv& quot;));
List& lt; string& gt; headers = new List& lt; string& gt;();

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& lt; values.Length; i ++)
{
Console.Write( string .Format(& quot; { 0 } = { 1 };& quot;,headers [i],values [i]));
Console.ReadLine();
}

Console.ReadLine();

}

}



}

}

}

解决方案

您的文件解析似乎是正确的 - 在文件路径的硬编码时没有这种情况名字可能有用。我希望这只是暂时的,快速测试。



看起来问题在于产生输出。您需要有两个嵌套循环:一个用每列值组成一行,外循环用于逐行输出数据。并且您需要在解析某些集合后存储数据。它可能是行数据的集合,每行可能只是一个字符串数组(您从 Split 方法获得的那些)。



要编写一行,请使用类 System.Text.StringBuilder

http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx [ ^ ]。



你的代码遇到了很多问题。我在上面第一段提到了一个。另一个是:你应该在循环中声明一个循环变量:

  for  int  index =  0 ; i索引< 值。长度; ++索引) //   ...  



,但你在循环之前声明它,这是不好的,原因有很多。循环变量只应在循环内定义,以防止访问它。



要找到并修复问题,请使用调试器。这比提问更有效。 :-)



始终使用调试器之前提出类似的问题。它将大大减少您可能要问的问题数量,并且会提高您的问题质量。



-SA

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





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(@&quot;c:\users\afshandc\documents\visual studio 2012\Projects\ConsoleApplication10\ConsoleApplication10\ex.csv&quot;));
            List&lt;string&gt; headers = new List&lt;string&gt;();

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

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

                    Console.ReadLine();

                }

            }



        }

    }

}

解决方案

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:

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


这篇关于在C#中读取CSV并写入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆