Search code examples
c#oracle-databaselistcsvdatareader

OracleDataReader fetching multiple results to csv


i have following problem:

Im want to use the OracleDataReader to fetch multiple recordsets and write it to a csv file. My plan was, that i use toe OracleDataReader and write the results into a list (comma separated). After that, i want to write the list into a csv file.

Im searching since 3 days and i didnt find any solution.

        OracleCommand command2 = new OracleCommand(sqlquery2, con);
        OracleDataReader reader2 = command2.ExecuteReader();
        {
            while (reader2.Read())
            {

                item = new ListViewItem(new string[] 
                { 
                    reader2.GetValue(0).ToString(),
                    reader2.GetValue(1).ToString(),
                    reader2.GetValue(2).ToString() 
                });


                listView1.Items.Add(item);
                File.AppendAllText(csvpath, item.ToString());
            }
        }

Solution

  • Do you mean something like this?

    var command = new OracleCommand(sql, con);
    var reader = command.ExecuteReader();
    var builder = new StringBuilder();
    builder.AppendLine("Header1,Header2");
    while (reader.Read())
    {
        builder.AppendLine($"{reader2.GetValue(0)},{reader2.GetValue(1)}");
    }
    File.WriteAllText(builder.ToString());