zoukankan      html  css  js  c++  java
  • 将文本文件转换为DataSet的两种方式

     

    将文本文件转换为DataSet的两种方式

    摘要:本文介绍采用ODBC .NET Framework 数据提供程序和System.IO下面的FileStream、StreamReader对象来将一定格式的文本文件转换为DataSet;

     

    方式一:利用ODBC .NET Framework 数据提供程序的OdbcDataAdapter对象来填充一个DataSet

     private DataSet GetDataset(string strFilePath)
            
    {
                
    if (!File.Exists(strFilePath))
                
    {
                    
    return null;
                }

                
    string strFolderPath = Path.GetDirectoryName(strFilePath);
                
    string strCSVFile = Path.GetFileName(strFilePath);

                DataSet ds 
    = null;
                
    string strConnection = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + strFolderPath + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
                
    try
                
    {
                    
    using (OdbcConnection conn = new OdbcConnection(strConnection.Trim()))
                    
    {
                        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)
                
    {
                    
    throw e;
                }

                
    return ds;
            }


    方式二:利用FileStreamStreamReader读取文件内容,手动创建一个DataSet;

            static DataSet GetDatasetFromTxtFile(String strFilePath,String strSpilter)
            
    {
                FileStream fs 
    = null;
                StreamReader s 
    = null;
                DataSet ds 
    = null;

                
    try
                
    {
                    fs 
    = new FileStream(strFilePath, FileMode.Open);
                    s 
    = new StreamReader(fs, System.Text.Encoding.Unicode);
                    ds 
    = new DataSet();

                    
    //创建表
                    ds.Tables.Add("unicode");

                    
    //生成列
                    string[] columns = s.ReadLine().Split(strSpilter.ToCharArray());
                    
    foreach (string c in columns)
                    
    {
                        
    if (c.Length > 0)
                        
    {
                            
    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)
                    
    {
                        
    if (r.Length > 0)
                        
    {
                            
    string[] items = r.Split(strSpilter.ToCharArray());
                            ds.Tables[
    "unicode"].Rows.Add(items);
                        }

                    }


                }

                
    catch(Exception e)
                
    {
                    
    throw e;
                }

                
    finally
                
    {
                    s.Close();
                    fs.Close();
                }
                
                
    return ds;
            }



    三、测试:
     

    TXT文件的格式为:(字段名和字段内容的采用逗号分开)

    列名1,列名2,列名3

    字段内容1,字段内容2,字段内容3

    ……

    代码为:

    测试程序,在.NET2005下通过

      结:

    1.      如果文件不是ANSI文件格式而是Unicode格式时,采用方法一得到的是乱码或者空字符(我的操作系统为中文版Server2003),而采用二可以得到更好的控制,因为我们在实例化StreamReader的时候可以指定读取文件的编码格式,从而可以得到我们理想的效果;

    2.      得到的DataSet可以得到DataTable来进行本地的相关数据处理也可以转换为XML或者序列化得到一个字节流来进行相关远程处理和实时传输;





    作者彭立云
    本文版权归作者所有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    如何用PostMan
    LINQ笔记-LINQ操作DataTable
    EF Core利用Transaction对数据进行回滚保护
    php 替换模板中的 PHP源码标签字符方法
    php读取文件使用redis的pipeline(管道)导入大批量数据
    Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境搭建教程
    php 版本升高后 会出现 之Deprecated: Function ereg_replace() is deprecated的解决方法
    IIS + FastCGI+php(从5.2升级到5.3)
    Nginx 出现 _STORAGE_WRITE_ERROR_:./Runtime/Cache/Home/
    nginx 环境不支持thinkPHP
  • 原文地址:https://www.cnblogs.com/hanchan/p/1244692.html
Copyright © 2011-2022 走看看