从分隔文本文件中填充 DataSet






4.67/5 (27投票s)
2004年4月19日
2分钟阅读

420293
解释了如何使用分隔文本文件中的信息填充数据集。
引言
这段代码允许你从文本文件中读取数据,并用这些数据填充一个 DataSet
。它包含一个 static
函数,该函数
- 打开文件
- 创建一个包含给定名称的
DataTable
的DataSet
- 使用文本文件第一行中的列名填充
DataTable
- 使用数据填充
DataTable
并返回DataSet
背景
从事商业工作的人都知道,虽然分隔符文本文件是数据传输的最低公共分母,但处理这些数据的过程可能很麻烦。这个类试图尽可能地简化这些文件的处理。
Using the Code
使用这段代码很简单。将其包含在你的项目中,并像这样调用它
DataSet ds = TextToDataSet.Convert(
"c:\test.txt", "MyNewTable", "\t");
有必要提供文件的完整路径,所以如果你在 ASP.NET 应用程序中使用这个类,代码可能如下所示
DataSet ds = TextToDataSet.Convert(
Server.MapPath("test.txt"), "MyNewTable", "\t");
最后一个参数是 delimiter
参数。它用于分隔每一列。在所示的例子中,我们传递了水平制表符的转义序列,但你可以传递任何 string
,例如空格 (" ") 或分号 (;)。你可能会发现以下列表有帮助
格式化转义序列
转义序列 | 目的 |
\a | 响铃 (警报) |
\b | 退格 |
\f | 换页 |
\n | 换行 |
\r | 回车 |
\t | 水平制表符 |
\v | 垂直制表符 |
\' | 单引号 |
\" | 双引号 |
\\ | 反斜杠 |
\? | 字面量问号 |
\ooo | 以八进制表示的 ASCII 字符 |
\xhh | 以十六进制表示的 ASCII 字符 |
\xhhhh | - 在宽字符常量或 UNICODE 字符串文字中使用此转义序列时,以十六进制表示的 UNICODE 字符 |
还有很多,但这些是最常见的。
我想现在剩下的就是给你代码了,所以它就在这里
using System;
using System.Data;
using System.IO;
namespace TestTextToDataSet
{
public class TextToDataSet
{
public TextToDataSet()
{ }
/// <summary>
/// Converts a given delimited file into a dataset.
/// Assumes that the first line
/// of the text file contains the column names.
/// </summary>
/// <param name="File">The name of the file to open</param>
/// <param name="TableName">The name of the
/// Table to be made within the DataSet returned</param>
/// <param name="delimiter">The string to delimit by</param>
/// <returns></returns>
public static DataSet Convert(string File,
string TableName, string delimiter)
{
//The DataSet to Return
DataSet result = new DataSet();
//Open the file in a stream reader.
StreamReader s = new StreamReader(File);
//Split the first line into the columns
string[] columns = s.ReadLine().Split(delimiter.ToCharArray());
//Add the new DataTable to the RecordSet
result.Tables.Add(TableName);
//Cycle the colums, adding those that don't exist yet
//and sequencing the one that do.
foreach(string col in columns)
{
bool added = false;
string next = "";
int i = 0;
while(!added)
{
//Build the column name and remove any unwanted characters.
string columnname = col + next;
columnname = columnname.Replace("#","");
columnname = columnname.Replace("'","");
columnname = columnname.Replace("&","");
//See if the column already exists
if(!result.Tables[TableName].Columns.Contains(columnname))
{
//if it doesn't then we add it here and mark it as added
result.Tables[TableName].Columns.Add(columnname);
added = true;
}
else
{
//if it did exist then we increment the sequencer and try again.
i++;
next = "_" + i.ToString();
}
}
}
//Read the rest of the data in the file.
string AllData = s.ReadToEnd();
//Split off each row at the Carriage Return/Line Feed
//Default line ending in most windows exports.
//You may have to edit this to match your particular file.
//This will work for Excel, Access, etc. default exports.
string[] rows = AllData.Split("\r\n".ToCharArray());
//Now add each row to the DataSet
foreach(string r in rows)
{
//Split the row at the delimiter.
string[] items = r.Split(delimiter.ToCharArray());
//Add the item
result.Tables[TableName].Rows.Add(items);
}
//Return the imported data.
return result;
}
}
}
关注点
你可以通过多种不同的方式重载此函数以适应你的项目需求。这只是我使用的一种方式。如果需要更多选项,我会发布一些。享受代码!
历史
- 2004 年 4 月 19 日:初始版本
许可证
这篇文章没有明确的许可证附加到它,但可能包含文章文本或下载文件本身中的使用条款。如有疑问,请通过下面的讨论区联系作者。可以在 这里 找到作者可能使用的许可证列表。