C# and Oracle – Exporting Data (Text)

Sometimes it can be useful to export data from a database, so that it can be analysed, or, to import in to another computer system. Text files are one such file format that allows for both of these scenarios. These text files are often called delimited text files because each piece of data is separated by a delimiter.

Below is an example of how C# can be used to export data to a text file called ‘personexport.txt’, from an Oracle database table called ‘person’, which was used in the examples for selectinginserting, updating, deleting, importing (CSV, text, XML and JSON) and exporting data in CSV format. The delimiter used to separate each piece of data in this example is the pipe (|) symbol.

Firstly, a connection to the database is established, the text file path and name are set and a check is made to see if the path actually exists. If it does, a query is executed to extract the data from the database and the text file is opened for writing. The table headers are then added to the text file, followed by the rows of data, one by one.

A rolling seven day backup is also included. This makes a copy of the text file that has just been created, giving it a name that includes the index number for the day of the week, along with the day itself, for example, 'personexport-1-monday.txt', for the backup on a Monday. Here, Sunday is classed as the first day of the week, with an index value of zero. Note that the backup is only done for the first time that this is run in a given day. Backups are then overwritten each week.

Finally, confirmation of a successful export is provided. A ‘try-catch-finally’ block is used to catch any errors that may occur, as well as tidy up at the end, regardless of whether the export is successful or not.

// Database connection variable.
OracleConnection connect = new OracleConnection(
    "Data Source=localhost:1521/Demo;" + 
    "User Id=DemoUN; Password=DemoPW");


    // Connect to database.

catch (Exception e)

    // Confirm unsuccessful connection and stop program execution.
    Console.WriteLine("Database connection unsuccessful.");


// Export path and file.
string exportPath = @"C:\demo\";
string exportText = "personexport.txt";

// Stream writer for text file.
StreamWriter textFile = null;

// Check to see if the file path exists.
if (!Directory.Exists(exportPath))

    // Display a message stating file path does not exist.
    Console.WriteLine("File path does not exist.");

    // Stop program execution.



    // Query text.
    string sqlText = @"
        SELECT id, firstname, lastname, title, dob 
        FROM person 
        ORDER BY id

    // Query text incorporated into SQL command.
    OracleCommand sqlSelect = new OracleCommand(sqlText, connect);

    // Execute SQL and place data in a reader object.
    OracleDataReader reader = sqlSelect.ExecuteReader();

    // Stream writer for text file.
    textFile = new StreamWriter(@exportPath + exportText);

    // Add the headers to the text file.
        reader.GetName(0), reader.GetName(1), reader.GetName(2),
        reader.GetName(3), reader.GetName(4)));

    // Construct text file data rows.
    while (reader.Read())

        // Add line from reader object to new text file.
            reader[0], reader[1], reader[2], reader[3], 


    // Close the file.

    // Today's date.
    DateTime today = DateTime.Now;

    // Construct the backup file name.
    string exportBackupText = exportText.Substring(0, exportText.Length - 4) +
        "-" + (int)today.DayOfWeek + "-" +
        today.DayOfWeek.ToString().ToLower() + ".txt";

    // Check if the backup file does not exist, or if it does, check that
    // today's date is different from the last modified date.
    if (!File.Exists(Path.Combine(exportPath, exportBackupText)) ||
        (File.Exists(Path.Combine(exportPath, exportBackupText)) &&
            Path.Combine(exportPath, exportBackupText)).Date !=

        // Copy the text export.
        File.Copy(Path.Combine(exportPath, exportText),
            Path.Combine(exportPath, exportBackupText), true);


    // Message stating export successful.
    Console.WriteLine("Data export successful.");

catch (Exception e)

    // Message stating export unsuccessful.
    Console.WriteLine("Data export unsuccessful.");


    // Close the database connection and text file.


The text file produced contains the following data.


Further Resources