使用C#中的文本文件对数据进行分组

时间:2016-01-31 17:11:11

标签: c# file

我目前正在开发一个小项目,该项目根据条件从'txt'文件返回文本,然后在将其导出到数据库之前对其进行分组。在文本文件中我有:

c:\ test \ 123

其他行...

c:\ test \ 124

问题:“错误描述”。 (此错误适用于目录124)

问题:“错误描述”。 (此错误适用于目录124)

C:\测试\ 125

...

我希望在将问题导入数据库时​​将“问题”分组到相关目录中。到目前为止,我已尝试使用'foreach'返回行包含/以目录或问题开头的行。虽然这会按顺序传递值,但用户不清楚问题属于哪个目录。理想情况下我会追求:

目录(第1栏)问题(第2栏)

c:\ test \ 123 ||空

c:\ test \ 124 ||问题:“错误描述”。

c:\ test \ 124 ||问题:“错误描述”。

c:\ test \ 125 ||空

非常感谢您提供的任何帮助。我上周一直在绞尽脑汁!

(现行代码)

        var lines = File.ReadAllLines(filename);

        foreach (var line in File.ReadLines(filename))
        {
            String stringTest = line;

            if (stringTest.Contains(directory))
            {
                String test = stringTest;

                var csb = new SqlConnectionStringBuilder();
                csb.DataSource = host;
                csb.InitialCatalog = catalog;
                csb.UserID = user;
                csb.Password = pass;

                using (var sc = new SqlConnection(csb.ConnectionString))
                using (var cmd = sc.CreateCommand())
                {
                    sc.Open();
                    cmd.CommandText = "DELETE FROM table";
                    cmd.CommandText = "INSERT INTO table (ID, Directory) values (NEWID(), @val)";
                    cmd.Parameters.AddWithValue("@VAL", test);
                    cmd.ExecuteNonQuery();
                    sc.Close();
                }
            }

            if (stringTest.Contains(problem))
            {

同样的问题......

1 个答案:

答案 0 :(得分:1)

这是一个解决方案:

假设您有以下类来保存结果项:

public class ResultItem
{
    public string Directory { get; set; }
    public string Problem { get; set; }
}

您可以执行以下操作:

var lines = File.ReadAllLines(filename);

string current_directory = null;

List<ResultItem> results = new List<ResultItem>();

//maintain the number of results added for the current directory
int problems_added_for_current_directory = 0;

foreach (var line in lines)
{
    if (line.StartsWith("c:\\test"))
    {
        //If we are changing to a new directory
        //And we didn't add any items for current directory
        //Add a null result item
        if (current_directory != null && problems_added_for_current_directory == 0)
        {
            results.Add(new ResultItem
            {
                Directory = current_directory,
                Problem = null
            });                            
        }

        current_directory = line;

        problems_added_for_current_directory = 0;
    }
    else if (line.StartsWith("Problem"))
    {
        results.Add(new ResultItem
        {
            Directory = current_directory,
            Problem = line
        });

        problems_added_for_current_directory++;
    }
}

//If we are done looping
//And we didn't add any items for current (last) directory
//Add a null result item
if (current_directory != null && problems_added_for_current_directory == 0)
{
    results.Add(new ResultItem
    {
        Directory = current_directory,
        Problem = null
    });
}