• 另一种文件读取方法(FileHelpers)


    1:前言

         本文介绍一个开源的文件读取类库,给大家提供另外一种文件读取的方法。

         项目名称:FileHelpers

         项目地址:http://www.filehelpers.com/

    2:Example

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using FileHelpers;
    using FileHelpers.DataLink;

    namespace ConsoleApplication
    {

        [DelimitedRecord(
    "\t")]
        
    public class LogInfo
        {
            [FieldConverter(ConverterKind.Date, 
    "yyyy-MM-dd HH:mm:ss.FFF")]
            
    public DateTime DateTime;

            
    public string empty;

            
    public string Method;
            
    public string IsSuccess;
            
    public string ElapsedMilliseconds;
            
    public string TnsName;
            
    public string CommandText;
        }


        
    class Program
        {

            
    protected static string GetInsertSqlCust(object record)
            {
                LogInfo obj 
    = (LogInfo)record;

                
    return string.Format(
                    
    "INSERT INTO Info "
                    
    +"([DateTime]"
                    
    +",[Methord]"
                    
    +",[IsSuccess]"
                    
    +",[ElapsedMilliseconds]"
                    
    +",[TnsName]"
                    
    +",[CommandText])"
                    
    +" VALUES "
                    
    +"('{0}'"
                    
    +",'{1}'"
                    
    +",{2}"
                    
    +",{3}"
                    
    +",'{4}'"
                    
    +",'{5}')",
                    obj.DateTime.ToString().Replace(
    "'""\""),
                    obj.Method.Replace("'""\""),
                    obj.IsSuccess.Replace("'""\""),
                    obj.ElapsedMilliseconds.Replace("'""\""),
                    obj.TnsName.Replace("'""\""),
                    obj.CommandText.Replace("'""\"")
                    );

            }

            
    static void Main(string[] args)
            {
                
                FileHelperEngine engine 
    = new FileHelperEngine(typeof(CustomersTabIgnored3));

                CustomersTabIgnored3[] res 
    = (CustomersTabIgnored3[])engine.ReadFile(@"E:\Server\DACServer\log\Info\2010-04-09 16.log");

                
    foreach (CustomersTabIgnored3 record in res)
                {
                    Console.WriteLine(record.DateTime.ToString());
                    Console.WriteLine(record.Method);
                    Console.WriteLine(record.IsSuccess);
                    Console.WriteLine(record.ElapsedMilliseconds);
                    Console.WriteLine(record.TnsName);
                    Console.WriteLine(record.CommandText);
                }

                Console.ReadLine();
                

                SqlServerStorage storage 
    = new SqlServerStorage(typeof(LogInfo));

                storage.ServerName 
    = "127.0.0.1";
                storage.DatabaseName 
    = "LogInfo";

                storage.InsertSqlCallback 
    = new InsertSqlHandler(GetInsertSqlCust);

                
    try
                {
                    FileDataLink link 
    = new FileDataLink(storage);
                    link.InsertFromFile(
    @"E:\Server\DACServer\log\Info\2010-04-09 16.log");
                }
                
    catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

            }
        }
    }

     3:后记

         该框架是值类型与引用类型的另一种转换方式。还有一个文档数据库(好像是编译不通过,随后我在细致的看看,地址是:http://groups.google.com/group/ravendb/

  • 相关阅读:
    开发必会系列:加密
    开发必会系列:《Java多线程编程实战》读书笔记
    基础教材系列:Linux原理《趣谈linux》极客时间笔记
    安全测试系列:《web安全深度剖析》读书笔记
    基础教材系列:《计算机网络自顶向下方法》读书笔记
    开发必会系列:《spring实战(第4版)》读书笔记
    开发必会系列:《设计模式》读书笔记
    性能测试面试问答:问题定位思路
    linux命令---dstat强大的性能监测工具(通用的系统资源统计工具:可以实时的监控cpu、磁盘、网络、IO、内存等使用情况。)
    Java中System.setProperty()用法
  • 原文地址:https://www.cnblogs.com/tommyli/p/1745100.html
Copyright © 2020-2023  润新知