0

我有一个旧的 Visual FoxPro 程序,我需要用 c# 重写它。在那里,我们使用 VFP 中的游标来读取 .txt 文件并将其加载到临时游标中。

例如在 FoxPro 中看起来像这样:(mb5b 是 mb5b-textfile)

SELECT werk,matnr,ALLTRIM(matnr)+ALLTRIM(werk) as matwerk,sum(zugang) as zugang,sum(abgang) as abgang INTO CURSOR mb5b_temp FROM mb5b GROUP BY werk,matnr

这些游标在 c# 中不存在。(我没有找到这样的东西。)所以我创建了一个 DataTable 并在读取文件时将它插入到 DataTable 中。

DataTable dt_mb5b_temp = new DataTable();
dt_mb5b_temp.Columns.Add("matnr");
dt_mb5b_temp.Columns.Add("werk");
dt_mb5b_temp.Columns.Add("matwerk");
dt_mb5b_temp.Columns.Add("zugang");
dt_mb5b_temp.Columns.Add("abgang");
while ((mb5bline = sr_mb5b.ReadLine()) != null)
{
    DataRow dr = dt_mb5b_temp.NewRow();
    string[] mb5b = mb5bline.Split(new Char[] { '|' });
    dr["matnr"] = mb5b[1].Trim();
    dr["werk"] = mb5b[2].Trim();
    dr["matwerk"] = mb5b[1].Trim() + mb5b[2].Trim();
    dr["zugang"] = mb5b[6].Trim();
    dr["abgang"] = mb5b[7].Trim();                        
}

我想我可以使用DataTable.Select()来使用上面的选择语句,但它不起作用......目前我还没有想到其他解决方案:/

当然,我也可以将它插入数据库 - 然后使用选择,但我尽量避免这种情况(需要两个额外的表,我认为这些插入和选择需要很长时间)。有没有可能让这个工作?

谢谢!

如果您需要更多信息,请告诉。

4

1 回答 1

1

看看这个网站。 http://www.dotnetperls.com/readline

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main()
    {
    const string f = "TextFile1.txt";

    // 1
    // Declare new List.
    List<string> lines = new List<string>();

    // 2
    // Use using StreamReader for disposing.
    using (StreamReader r = new StreamReader(f))
    {
        // 3
        // Use while != null pattern for loop
        string line;
        while ((line = r.ReadLine()) != null)
        {
        // 4
        // Insert logic here.
        // ...
        // "line" is a line in the file. Add it to our List.
        lines.Add(line);
        }
    }

    // 5
    // Print out all the lines.
    foreach (string s in lines)
    {
        Console.WriteLine(s);
    }
    }
}

Output
    (Prints contents of TextFile1.txt)

This is a text file I created,
Just for this article.

按 ienum 分组

    class Pet
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    // Uses method-based query syntax. 
    public static void GroupByEx1()
    {
        // Create a list of pets.
        List<Pet> pets =
            new List<Pet>{ new Pet { Name="Barley", Age=8 },
                           new Pet { Name="Boots", Age=4 },
                           new Pet { Name="Whiskers", Age=1 },
                           new Pet { Name="Daisy", Age=4 } };

        // Group the pets using Age as the key value  
        // and selecting only the pet's Name for each value.
        IEnumerable<IGrouping<int, string>> query =
            pets.GroupBy(pet => pet.Age, pet => pet.Name);

        // Iterate over each IGrouping in the collection. 
        foreach (IGrouping<int, string> petGroup in query)
        {
            // Print the key value of the IGrouping.
            Console.WriteLine(petGroup.Key);
            // Iterate over each value in the  
            // IGrouping and print the value. 
            foreach (string name in petGroup)
                Console.WriteLine("  {0}", name);
        }
    }

    /*
     This code produces the following output:

     8
       Barley
     4
       Boots
       Daisy
     1
       Whiskers
    */
于 2013-09-10T20:40:34.800 回答