我有一个旧的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()来使用上面的选择语句,但它不起作用......此刻我不介意其他解决方案:/
当然我也可以将它插入数据库 - 然后使用select,但我尽量避免这种情况(需要两个额外的表,我认为那些插入和选择将花费很长时间)。 有没有可能让这个工作?
谢谢!
如果您需要更多信息,请告知。
答案 0 :(得分: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
*/