DataTable from TextFile?

梦想与她 提交于 2019-11-28 11:39:44

You might need to use this function to read the data into DataTable from the file.

public DataTable GetDataSourceFromFile(string fileName)
{
    DataTable dt = new DataTable("CreditCards");
    string[] columns = null;

    var lines = File.ReadAllLines(fileName);

    // assuming the first row contains the columns information
    if (lines.Count() > 0)
    {
        columns = lines[0].Split(new char[] { ',' });

        foreach (var column in columns)
            dt.Columns.Add(column);
    }

    // reading rest of the data
    for (int i = 1; i < lines.Count(); i++)
    {
        DataRow dr = dt.NewRow();
        string[] values = lines[i].Split(new char[] { ',' });

        for (int j = 0; j < values.Count() && j < columns.Count(); j++)
            dr[j] = values[j];

        dt.Rows.Add(dr);
    }
    return dt;
}

It is definitely possible.
DataTable has a NewRow method so the simplest, brute force method I can see is to read the text file one line at a time, parse the string (split(",") and then populate the fields of the row. Then you need to add the new row the Rows collection of the DataTable.
There may be smarter ways to do this but this seems reasonably straightforward to implement (with no knowledge of your schema).

Ok,try this :

public string[] getColumns(bool ColumnNames)
{
    try {
        StreamReader fileReader = new StreamReader(FileName);
        string line = fileReader.ReadLine;
        fileReader.Close();
        string[] Columns = line.Split(",");
        if (ColumnNames) {
            return Columns;
        }
        int i = 1;
        int c = 0;
        string[] columnsNames = new string[Columns.Count];
        foreach (string column in Columns) {
            columnsNames(c) = "column" + i;
            i += 1;
            c += 1;
        }
        return columnsNames;
    } catch (Exception ex) {
        //log to file    
    }
    return null;
}

AND

public DataTable ReturnData(bool ColumnNames)
{
    try {
        DataTable dt = new DataTable();
        foreach ( columnName in getColumns(ColumnNames)) {
            dt.Columns.Add(columnName);
        }
        StreamReader fileReader = new StreamReader(FileName);
        if (ColumnNames) {
            fileReader.ReadLine();
        }
        string line = fileReader.ReadLine;
        while ((line != null)) {
            line = line.Replace(Strings.Chr(34), "");
            dt.Rows.Add(line.Split(","));
            line = fileReader.ReadLine;
        }
        fileReader.Close();
        return dt;
    } catch (Exception ex) {
        //log to file
    }
    return null;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!