0

这是对@Tim Schmelter 今天下午早些时候向我指出正确方向的一些实验代码的看法。其中大部分与之前的工作几乎完全相同,但它会invalidCastException在最后一行或倒数第二行抛出 a,这取决于我尝试的方式。我不明白为什么会这样。

Boolean test = false;
string filePathStudent = System.IO.Path.GetFullPath("StudentInfo.txt");
DataTable studentDataTable = new DataTable();

studentDataTable.Columns.Add("Id", typeof(int));
studentDataTable.Columns.Add("StudentID");
studentDataTable.Columns.Add("FirstName");
studentDataTable.Columns.Add("LastName");
studentDataTable.Columns.Add("StreetAdd");
studentDataTable.Columns.Add("City");
studentDataTable.Columns.Add("State");
studentDataTable.Columns.Add("Zip");
studentDataTable.Columns.Add("Choice1");
studentDataTable.Columns.Add("CreditHrs1");
studentDataTable.Columns.Add("Choice2");
studentDataTable.Columns.Add("CreditHrs2");
studentDataTable.Columns.Add("Choice3");
studentDataTable.Columns.Add("CreditHrs3");
studentDataTable.Columns.Add("Choice4");
studentDataTable.Columns.Add("CreditHrs4");
studentDataTable.Columns.Add("Choice5");
studentDataTable.Columns.Add("CreditHrs5");
studentDataTable.Columns.Add("Choice6");
studentDataTable.Columns.Add("CreditHrs6");

foreach (string line in File.ReadLines(filePathStudent))
{
    DataRow row = studentDataTable.Rows.Add();
    string[] fields = line.Split(new[] { (char)9 });
    int id;
    if (fields.Length == 19 && int.TryParse(fields[0], out id))
    {
        row.SetField("Id", id);
        row.SetField("StudentID", fields[1]);
        row.SetField("FirstName", fields[2]);
        row.SetField("LastName", fields[3]);
        row.SetField("StreetAdd", fields[4]);
        row.SetField("City", fields[5]);
        row.SetField("State", fields[6]);
        row.SetField("Zip", fields[7]);
        row.SetField("Choice1", fields[8]);
        row.SetField("CreditHrs1", fields[9]);
        row.SetField("Choice2", fields[10]);
        row.SetField("CreditHrs2", fields[11]);
        row.SetField("Choice3", fields[12]);
        row.SetField("CreditHrs3", fields[13]);
        row.SetField("Choice4", fields[14]);
        row.SetField("CreditHrs4", fields[15]);
        row.SetField("Choice5", fields[16]);
        row.SetField("CreditHrs5", fields[17]);
        row.SetField("Choice6", fields[18]);
        row.SetField("CreditHrs6", fields[19]);
    }
}

using (StreamReader reader = new StreamReader(filePathStudent))
{
    String line1 = reader.ReadLine();
    if (line1 == null)
        maxIDStdTable = 0;
    else
        test = true;

    reader.Dispose();
    reader.Close();
}

if(test)
    int maxIDStdTable = studentDataTable.AsEnumerable().Max(r => r.Field<int>("Id"));
    //int maxIDStdTable = (int)studentDataTable.Compute("Max(Id)", "");
4

2 回答 2

0

它可能不是最好的解决方案,但它确实有效。

            string filePathStudent = System.IO.Path.GetFullPath("StudentInfo.txt");
        DataTable studentDataTable = new DataTable();

        studentDataTable.Columns.Add("Id", typeof(Int32));
        studentDataTable.Columns.Add("StudentID");
        studentDataTable.Columns.Add("FirstName");
        studentDataTable.Columns.Add("LastName");
        studentDataTable.Columns.Add("StreetAdd");
        studentDataTable.Columns.Add("City");
        studentDataTable.Columns.Add("State");
        studentDataTable.Columns.Add("Zip");
        studentDataTable.Columns.Add("Choice1");
        studentDataTable.Columns.Add("CreditHrs1");
        studentDataTable.Columns.Add("Choice2");
        studentDataTable.Columns.Add("CreditHrs2");
        studentDataTable.Columns.Add("Choice3");
        studentDataTable.Columns.Add("CreditHrs3");
        studentDataTable.Columns.Add("Choice4");
        studentDataTable.Columns.Add("CreditHrs4");
        studentDataTable.Columns.Add("Choice5");
        studentDataTable.Columns.Add("CreditHrs5");
        studentDataTable.Columns.Add("Choice6");
        studentDataTable.Columns.Add("CreditHrs6");

        // Read in a file line-by-line, and store it
        var txtFileLine = File.ReadAllLines(filePathStudent).ToList();

        //Reads line splits data to colums at tab (ASCII value 9)
        txtFileLine.ForEach(line => studentDataTable.Rows.Add(line.Split((char)9)));


        List<int> rowsForColumn1 = studentDataTable.AsEnumerable().Select(x => x.Field<int>(0)).ToList();

        //Tests for empty Datatable
        foreach (DataRow row in studentDataTable.Rows)
        {
            if (row.IsNull("Id"))
                break;
            else
                //get max value from "Id" row.
                maxIDStdTable = rowsForColumn1.Max();
        }
    }
于 2013-04-21T03:36:15.880 回答
0

您犯了两个错误: 1)您使用DataTable.NewRow()
创建了新的 2)设置后您必须将其添加到with 中。 更新您的代码并尝试: DataRow
DataRowDataTableDataTable.Rows.Add(youDataRow)

 foreach (string line in File.ReadLines(filePathStudent))
    {
        DataRow row = studentDataTable.NewRow();
        string[] fields = line.Split(new[] { (char)9 });
        int id;
        if (fields.Length == 19 && int.TryParse(fields[0], out id))
        {
            row["Id"]= id;
            row["StudentID"]= fields[1];
            row["FirstName"]= fields[2];
            row[LastName"]= fields[3];
            row["StreetAdd"]= fields[4];
            row["City"]=fields[5];
            row["State"]= fields[6];
            row["Zip"]=fields[7];
            row["Choice1"]= fields[8];
            row["CreditHrs1"]= fields[9];
            row["Choice2"]= fields[10];
            row["CreditHrs2"]= fields[11];
            row[("Choice3"]= fields[12];
            row["CreditHrs3"]=, fields[13];
            row["Choice4"]= fields[14];
            row["CreditHrs4"]= fields[15];
            row["Choice5"]= fields[16];
            row["CreditHrs5"]= fields[17];
            row["Choice6"]= fields[18];
            row["CreditHrs6"] =fields[19];
        }
       studentDataTable.Rows.Add(row);

    }
于 2013-04-20T04:10:56.730 回答