How to use SQL against a CSV file

后端 未结 5 1935
隐瞒了意图╮
隐瞒了意图╮ 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:22

    You can use ODBC to run a query against a CSV File :

    // using System.Data.Odbc;
    
    string strConn = @"Driver={Microsoft Text Driver (*.txt; *.csv)};" +
        "Dbq=C:;Extensions=csv,txt";
    
    OdbcConnection objCSV = new OdbcConnection(strConn);
    objCSV.Open();
    
    OdbcCommand oCmd = new OdbcCommand("select column1,column2 " +
        "from THECSVFILE.CSV", objCSV);
    OdbcDataReader oDR = oCmd.ExecuteReader();
    
    while (oDR.Read())
    {
        // Do something
    }
    

提交回复
热议问题