如何引用List <Data>

时间:2019-09-12 09:02:07

标签: c# list reference

我是C#的新手,我承担了一个小任务。对于我来说,用于读取文本文件并将其保存到列表的StackOverflow条目是一个很好的开始。我需要读取一个文本文件并将数据发送到SQL数据库。

How to Read This Text File and store in a list using C#

Data中的List<Data> list = new List<Data>();一直保持红色。

请问如何停止?

我是一名PLC工程师,我正在尝试整理无法由PLC处理的数据。我只是试图读取文件,以便随后可以在Grid中显示数据,以期稍后填充SQL数据库。

文本文件保存在远程Linux计算机上。整理器是一个Windows 10面板。 SQL可以驻留在Windows面板上,也可以驻留在远程。

static void Main(string[] args)
{
    List<Data> list = new List<Data>();

    var dd = File.ReadAllLines(@"C:\Users\XXXX\Desktop\test.txt")
     .Skip(1)
     .Where(s => s.Length > 1).ToList();

    foreach (var item in dd)
    {
        var columns = item.Split('\t').Where(c => c.Trim() != string.Empty).ToList();

        if (columns != null && columns.Count > 0)
        {
            int id;

            if (int.TryParse(columns[0], out id))
            {
                list.Add(new Data()
                {
                    id = Convert.ToInt32(columns[0]),
                    Name = columns[1],
                    Description = columns[2],
                    Quantity = Convert.ToInt32(columns[3]),
                    Rate = Convert.ToDouble(columns[4]),
                    Discount = Convert.ToInt32(columns[5]),
                    Amount = int.Parse(columns[6])
                });
            }
            else
            {
                list.Last().Description += columns[0];
            }
        }
    }

    Console.ReadLine();
}

我只是不断收到<Data上的红色波浪线。在Visual Studio中

1 个答案:

答案 0 :(得分:0)

我有可用的代码,并且直接将DAT /文本文件读入DatagridView。我现在正在将网格写入SQL。

非常感谢,很抱歉我一直不在现场。

            String sLine = "";

            try
            {
                //Pass the file you selected with the OpenFileDialog control to
                //the StreamReader Constructor.
                System.IO.StreamReader FileStream = new System.IO.StreamReader(openFileDialog1.FileName);
                //You must set the value to false when you are programatically adding rows to
                //a DataGridView.  If you need to allow the user to add rows, you
                //can set the value back to true after you have populated the DataGridView
                dataGridView1.AllowUserToAddRows = false;

                // Clear the DataGridView prior to reading a new text file
                dataGridView1.Rows.Clear();
                dataGridView1.Columns.Clear();

                //Read the first line of the text file
                sLine = FileStream.ReadLine();
                //The Split Command splits a string into an array, based on the delimiter you pass.
                //I chose to use a semi-colon for the text delimiter.
                //Any character can be used as a delimeter in the split command.
                //string[] s = sLine.Split(';');
                string[] s = sLine.Split('\t');

                //In this example, I placed the field names in the first row.
                //The for loop below is used to create the columns and use the text values in
                //the first row for the column headings.
                for (int i = 0; i <= s.Count() - 1; i++)
                {
                    DataGridViewColumn colHold = new DataGridViewTextBoxColumn();
                    colHold.Name = "col" + System.Convert.ToString(i);
                    colHold.HeaderText = s[i].ToString();
                    dataGridView1.Columns.Add(colHold);
                }

                //Read the next line in the text file in order to pass it to the
                //while loop below
                sLine = FileStream.ReadLine();
                //The while loop reads each line of text.
                while (sLine != null)
                {
                    //Adds a new row to the DataGridView for each line of text.
                    dataGridView1.Rows.Add();

                    //This for loop loops through the array in order to retrieve each
                    //line of text.
                    for (int i = 0; i <= s.Count() - 1; i++)
                    {
                        //Splits each line in the text file into a string array
                        //s = sLine.Split(';');
                        s = sLine.Split('\t');
                        //Sets the value of the cell to the value of the text retreived from the text file.
                        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[i].Value = s[i].ToString();
                    }
                    sLine = FileStream.ReadLine();
                }
                //Close the selected text file.
                FileStream.Close();
            }
            catch (Exception err)
            {
                //Display any errors in a Message Box.
                System.Windows.Forms.MessageBox.Show("Error "+ err.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }