C# 读取文件,如果(streamwriter=…)写入不同的表
我在将文件读入datagridviews(更具体地说是3)时遇到了一个小问题 我的文本文件看起来像:C# 读取文件,如果(streamwriter=…)写入不同的表,c#,datatable,streamreader,C#,Datatable,Streamreader,我在将文件读入datagridviews(更具体地说是3)时遇到了一个小问题 我的文本文件看起来像: @Table K1;K2;K3 .... .. @Table K1;K4 .. .. @Table K1;K5 运行此代码后,数据仅显示在gridview1中。变量tableno不想增加。有人有主意吗?您可以通过使用StreamReader的作用域在中创建while循环,并在完成表后停止关闭读取器(读取器将由using语句为您关闭,因此这是不必要的)。现在的问题是,如果你看到下一个表的开始或旧
@Table
K1;K2;K3
....
..
@Table
K1;K4
..
..
@Table
K1;K5
运行此代码后,数据仅显示在gridview1中。变量tableno不想增加。有人有主意吗?您可以通过使用
StreamReader
的作用域在中创建while循环,并在完成表后停止关闭读取器(读取器将由using
语句为您关闭,因此这是不必要的)。现在的问题是,如果你看到下一个表的开始或旧表的结束(答案和问题不同…),你就打破了该表的阅读循环,但是没有什么可以将流更改为“向上”并开始处理下一个表,它只会在该点退出该方法
然而,你应该从根本上改变你的方法,因为它是非常重复的。现在你有3张桌子,但是如果你有N张桌子呢?从维护、测试和可扩展性的角度来看,重复代码是不好的做法。例如,如果文件格式发生更改,则必须在3个位置对其进行更改,并测试3倍于只编写一次的测试
下面是一个示例,说明如何编写它来处理任意数量的表,并重新使用相同的逻辑来读取每个表:
private List<DataTable> ReadDataTables(string fileName)
{
List<DataTable> tables = new List<DataTable>();
DataTable currentTable = null;
using (StreamReader reader = new StreamReader(fileName))
{
while (!reader.EndOfStream)
{
var currentLine = reader.ReadLine(); // get the next line from the file
if (String.IsNullOrWhiteSpace(currentLine)) continue; // ignore blank rows
// skip rows marking end of table, reset the current table
if (currentLine.StartsWith("@EndTable", StringComparison.OrdinalIgnoreCase))
{
currentTable = null;
continue;
}
// initialize a new table
if (currentLine.StartsWith("@Table", StringComparison.OrdinalIgnoreCase))
{
currentTable = new DataTable();
tables.Add(currentTable);
// initialise the columns
var columns = reader.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var column in columns)
currentTable.Columns.Add(column, typeof(String));
continue;
}
if (currentTable == null) continue; // file is not in the format we were expecting
// add data row
var dataRow = currentTable.NewRow();
dataRow.ItemArray = currentLine.Split(new char[] { ';' });
currentTable.Rows.Add(dataRow);
}
}
return tables;
}
你能试着正确缩进你的代码吗。我想它在复制粘贴上有点乱了。该死的,那太好了。谢谢:)为了结束另一个问题。。。我有一个变量:var tables=newdatatable[4]{Table1,Table2,Table3,Table4};我在合并表函数中使用它。此函数要求变量中的所有表都有主键列。但可能有一种情况,我只使用f.e.:表1和表2。我如何编辑这个变量(当表的数量不匹配时会出现错误),以便在那里添加或删除对象?最好问另一个问题,因为它听起来不相关,在注释中这样做也不太清楚。我不确定这是否是您要求的,但您可以创建一个新数组,如var tablesWithPrimaryKeys=tables.Where(a=>a.PrimaryKey.Any()).ToArray()
以仅选择具有主键的表。
private List<DataTable> ReadDataTables(string fileName)
{
List<DataTable> tables = new List<DataTable>();
DataTable currentTable = null;
using (StreamReader reader = new StreamReader(fileName))
{
while (!reader.EndOfStream)
{
var currentLine = reader.ReadLine(); // get the next line from the file
if (String.IsNullOrWhiteSpace(currentLine)) continue; // ignore blank rows
// skip rows marking end of table, reset the current table
if (currentLine.StartsWith("@EndTable", StringComparison.OrdinalIgnoreCase))
{
currentTable = null;
continue;
}
// initialize a new table
if (currentLine.StartsWith("@Table", StringComparison.OrdinalIgnoreCase))
{
currentTable = new DataTable();
tables.Add(currentTable);
// initialise the columns
var columns = reader.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var column in columns)
currentTable.Columns.Add(column, typeof(String));
continue;
}
if (currentTable == null) continue; // file is not in the format we were expecting
// add data row
var dataRow = currentTable.NewRow();
dataRow.ItemArray = currentLine.Split(new char[] { ';' });
currentTable.Rows.Add(dataRow);
}
}
return tables;
}
var tables = ReadDataTables(fileName);
dataGridView1.DataSource = tables[0];
dataGridView2.DataSource = tables[1];
dataGridView3.DataSource = tables[2];