csv 读取重复

Duplication in csv reading

我 运行 在我的代码中遇到了一些问题,当它读取 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);
        }
        }

您永远不会在 for 循环中的遍历之间清除 outfile

要么申报

string outfile = "";

循环内或有

outfile = "";

在循环的最后