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