本文介绍采用ODBC .NET Framework 数据提供程序和System.IO下面的FileStream、StreamReader对象来将一定格式的文本文件转换为DataSet;
将文本文件转换为DataSet的两种方式
摘要:本文介绍采用ODBC .NET Framework 数据提供程序和System.IO下面的FileStream、StreamReader对象来将一定格式的文本文件转换为DataSet;
方式一:利用ODBC .NET Framework 数据提供程序的OdbcDataAdapter对象来填充一个DataSet ;
private DataSet GetDataset(string strFilePath)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (!File.Exists(strFilePath))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
string strFolderPath = Path.GetDirectoryName(strFilePath);
string strCSVFile = Path.GetFileName(strFilePath);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DataSet ds = null;
string strConnection = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + strFolderPath + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
using (OdbcConnection conn = new OdbcConnection(strConnection.Trim()))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
conn.Open();
string strSql = "select * from [" + strCSVFile + "]";
OdbcDataAdapter odbcDAdapter = new OdbcDataAdapter(strSql, conn);
ds = new DataSet();
odbcDAdapter.Fill(ds, "table");
conn.Close();
}
return ds;
}
catch (Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
return ds;
}
方式二:利用FileStream和StreamReader读取文件内容,手动创建一个DataSet;
static DataSet GetDatasetFromTxtFile(String strFilePath,String strSpilter)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
FileStream fs = null;
StreamReader s = null;
DataSet ds = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fs = new FileStream(strFilePath, FileMode.Open);
s = new StreamReader(fs, System.Text.Encoding.Unicode);
ds = new DataSet();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//创建表
ds.Tables.Add("unicode");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//生成列
string[] columns = s.ReadLine().Split(strSpilter.ToCharArray());
foreach (string c in columns)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (c.Length > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] items = c.Split(strSpilter.ToCharArray());
ds.Tables["unicode"].Columns.Add(items[0]);
}
}
//生成行
string AllData = s.ReadToEnd();
string[] rows = AllData.Split("\r\n".ToCharArray());
foreach (string r in rows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (r.Length > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] items = r.Split(strSpilter.ToCharArray());
ds.Tables["unicode"].Rows.Add(items);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch(Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
s.Close();
fs.Close();
}
return ds;
}
三、测试:
TXT文件的格式为:(字段名和字段内容的采用逗号分开)
列名1,列名2,列名3
字段内容1,字段内容2,字段内容3
……
代码为:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
测试程序,在.NET2005下通过
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Data.Odbc;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace UnicodeTest
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//=========读取unicode文件
String str = @"D:\unicode.txt";
String strSpilter = ",";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DataSet ds = GetDatasetFromTxtFile(str, strSpilter);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//测试输出
DataTable dt = ds.Tables["unicode"];
String strCol = string.Empty;
foreach (DataColumn dc in dt.Columns)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strCol += dc.ColumnName + ("\t");
}
Console.WriteLine("{0}{1}", strCol, Environment.NewLine);
foreach (DataRow dr in dt.Rows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("{0}{7}{1}{7}{2}{7}{3}{7}{4}{7}{5}{6}",
dr[0].ToString(), dr[1].ToString(), dr[2].ToString(),
dr[3].ToString(), dr[4].ToString(), dr[5].ToString(),
Environment.NewLine, "\t");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
//======================//
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static DataSet GetDatasetFromTxtFile(String strFilePath,String strSpilter)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileStream fs = null;
StreamReader s = null;
DataSet ds = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fs = new FileStream(strFilePath, FileMode.Open);
s = new StreamReader(fs, System.Text.Encoding.Unicode);
ds = new DataSet();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//创建表
ds.Tables.Add("unicode");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//生成列
string[] columns = s.ReadLine().Split(strSpilter.ToCharArray());
foreach (string c in columns)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (c.Length > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] items = c.Split(strSpilter.ToCharArray());
ds.Tables["unicode"].Columns.Add(items[0]);
}
}
//生成行
string AllData = s.ReadToEnd();
string[] rows = AllData.Split("\r\n".ToCharArray());
foreach (string r in rows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (r.Length > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] items = r.Split(strSpilter.ToCharArray());
ds.Tables["unicode"].Rows.Add(items);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch(Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
s.Close();
fs.Close();
}
return ds;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private DataSet GetDataset(string strFilePath)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!File.Exists(strFilePath))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
string strFolderPath = Path.GetDirectoryName(strFilePath);
string strCSVFile = Path.GetFileName(strFilePath);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DataSet ds = null;
string strConnection = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + strFolderPath + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
using (OdbcConnection conn = new OdbcConnection(strConnection.Trim()))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
conn.Open();
string strSql = "select * from [" + strCSVFile + "]";
OdbcDataAdapter odbcDAdapter = new OdbcDataAdapter(strSql, conn);
ds = new DataSet();
odbcDAdapter.Fill(ds, "table");
conn.Close();
}
return ds;
}
catch (Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
return ds;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
结:
1. 如果文件不是ANSI文件格式而是Unicode格式时,采用方法一得到的是乱码或者空字符(我的操作系统为中文版Server2003),而采用二可以得到更好的控制,因为我们在实例化StreamReader的时候可以指定读取文件的编码格式,从而可以得到我们理想的效果;
2. 得到的DataSet可以得到DataTable来进行本地的相关数据处理也可以转换为XML或者序列化得到一个字节流来进行相关远程处理和实时传输;