C# 如何参考列表<;数据>;

C# 如何参考列表<;数据>;,c#,list,reference,C#,List,Reference,我是C#的新手,我承担了一项小任务。用于读取文本文件并保存到列表的StackOverflow条目对我来说是一个很好的开始。我需要读取一个文本文件并将数据发送到SQL数据库 List=newlist()中的数据一直保持红色 我怎样才能阻止这一切 我是一名PLC工程师,我试图整理PLC无法处理的数据。我只是尝试读取该文件,以便在网格中显示数据,以便稍后填充SQL数据库 文本文件保存在远程Linux机器上。collator是一个WIndows 10面板。SQL可以驻留在Windows面板上,也可以远

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

List=newlist()中的
数据
一直保持红色

我怎样才能阻止这一切

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

文本文件保存在远程Linux机器上。collator是一个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();
}
static void Main(字符串[]args)
{
列表=新列表();
var dd=File.ReadAllLines(@“C:\Users\XXXX\Desktop\test.txt”)
.Skip(1)
其中(s=>s.Length>1).ToList();
foreach(dd中的var项目)
{
var columns=item.Split('\t')。其中(c=>c.Trim()!=string.Empty)。ToList();
if(columns!=null&&columns.Count>0)
{
int-id;
if(int.TryParse(列[0],out id))
{
list.Add(新数据()
{
id=Convert.ToInt32(列[0]),
名称=列[1],
Description=列[2],
数量=转换为32(第[3]列),
Rate=Convert.ToDouble(第[4]列),
折扣=转换为32(第[5]列),
Amount=int.Parse(列[6])
});
}
其他的
{
list.Last().Description+=列[0];
}
}
}
Console.ReadLine();
}

我只是在
上不断收到红色的曲线,我让代码正常工作,并将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);
            }
字符串sLine=”“;
尝试
{
//将使用OpenFileDialog控件选择的文件传递给
//StreamReader构造函数。
System.IO.StreamReader FileStream=新的System.IO.StreamReader(openFileDialog1.FileName);
//以编程方式将行添加到中时,必须将该值设置为false
//DataGridView。如果需要允许用户添加行,则
//填充DataGridView后,可以将该值设置回true
dataGridView1.AllowUserToAddress=false;
//在读取新文本文件之前清除DataGridView
dataGridView1.Rows.Clear();
dataGridView1.Columns.Clear();
//读取文本文件的第一行
sLine=FileStream.ReadLine();
//Split命令根据传递的分隔符将字符串拆分为数组。
//我选择使用分号作为文本分隔符。
//在split命令中,任何字符都可以用作delimeter。
//字符串[]s=sLine.Split(“;”);
字符串[]s=sLine.Split('\t');
//在本例中,我将字段名放在第一行。
//下面的for循环用于创建列并使用中的文本值
//列标题的第一行。

对于(int i=0;我是否使用System.Collections.Generic;
将此添加到代码顶部?
使用System.Collections.Generic;
或者您没有导入该名称空间,或者由于某些原因,
数据
无效您会得到什么错误?什么是
数据
?它定义在哪里?请构建解决方案;错误是什么编译器会显示?名为
Data
的类在哪里?您以前发布过类Class1。因此,请将列表从Data更改为Class1,或者将类名从Class1更改为Data。