【问题标题】:Duplication in csv readingcsv读取中的重复
【发布时间】:2016-12-21 02:08:57
【问题描述】:

我在我的代码中遇到了一些问题,当它读取 SQL 文件时,它会重复条目并且有时会将它们以错误的顺序排列。我一直在单步执行我的代码并检查值,但我找不到任何东西,应该只有 4 个条目。

    private void LoadBtn_Click(object sender, EventArgs e)
    {
        //Opens a browse box to allow the user to select which file, only CSV's allow allowed
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "CSV Files (*.csv)|*.csv";
        openFileDialog1.FilterIndex = 1;

        //empties text box when clicked | loads file location and name to load directory text box at top
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            ConvertedText.Text = string.Empty;
            LoadDirectory.Text = openFileDialog1.FileName.ToString();
        }
        string filename = LoadDirectory.Text;
        string[] Lines = File.ReadAllLines(filename);
        string[] Fields;
        string outfile = "";

        for (int i = 1; i < Lines.Length; i++)
        {
            Fields = Lines[i].Split(new char[] { ',' });
            outfile += "IF EXISITS (SELECT USERID FROM WUSERS WHERE USERID='" + Fields[0] + "')" + Environment.NewLine;
            outfile += "begin" + Environment.NewLine;
            ConvertedText.AppendText(outfile);
        }
        }

【问题讨论】:

    标签: c# csv converter


    【解决方案1】:

    你永远不会在 for 循环中清除 outfile。

    要么声明

    string outfile = "";
    

    在循环内或有

    outfile = "";
    

    在循环的最后

    【讨论】:

    • 您先生绝对是救生员,非常感谢您
    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 2016-02-22
    • 2011-12-04
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多