I have written this code in C# to connect to an Oracle Database, omitting the tnsnames.ora file, then place results from a query into a CSV File.
Is there more efficient way to do this? (Such as reading in the data into a DataReader rather than a DataTable)
Please review and tell me what you would change.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Oracle.ManagedDataAccess.Client;
using System.Configuration;
using System.Data;
using FileHelpers;
using System.IO;
namespace oracle_test
{
class Program
{
static void Main(string[] args)
{
OraTest ot = new OraTest();
ot.Connect();
}
class OraTest
{
public void Connect()
{
var constr = new OracleConnectionStringBuilder()
{
DataSource = @"(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.100.100.10)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = ONE10)))",
UserID = "100",
Password = "100",
}.ConnectionString;
using (var con = new OracleConnection(constr))
{
string prfbalqry = "select * from ati where be = 0";
OracleCommand cmd = new OracleCommand(prfbalqry, con);
cmd.CommandType = System.Data.CommandType.Text;
con.Open();
DataTable pbResults = new DataTable();
OracleDataAdapter oda = new OracleDataAdapter(cmd);
oda.Fill(pbResults);
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = pbResults.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in pbResults.Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText(@"C:\Financial Reporting\Output Path\pbresults.csv", sb.ToString());
}
}
}
}
}