How to use SQL against a CSV file

后端 未结 5 1936
隐瞒了意图╮
隐瞒了意图╮ 2021-01-12 11:04

I would like to use a SQL query on a CSV file using C#. There is something like this for java here. Is there anything like this for c#?

5条回答
  •  孤城傲影
    2021-01-12 11:27

    // need to add
    // using System.Linq;
    
    void Main()
    {
        var path = @"C:\myfile.csv";
        string csv = System.IO.File.ReadAllText( path );
        var array = csv.Split(new[]{","}, StringSplitOptions.RemoveEmptyEntries);
        // Do the mapping with your databinding object
        var personArray = array.Select(p => new Person { Name = p}); 
       // You need to have this DataContext defined somewhere, for instance using LinqToSql
        using(var context = new PersonDataContext()){ 
            context.InsertAllOnSubmit(personArray);
            context.SubmitChanges();
        } 
    }
    
    // Imagine this class is one of linqToSql class
    public class Person{
        public string Name {get;set;}
    }
    

提交回复
热议问题