Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill DataTable from Oracle Database Table - C#

I have successfully built connection string and able to populate table data when the database is Access as:

DataTable results = new DataTable();
using (OleDbConnection thisConnection = new OleDbConnection(connectionname))
            {
                OleDbCommand cmd = new OleDbCommand("SELECT * from TABLE_A", thisConnection);  //EDIT : change table name for Oracle
                thisConnection.Open();
                OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                adapter.Fill(results);
            }

I am new to Oracle though. Can somebody mention what changes to make in above code for Oracle database?

like image 370
user7157732 Avatar asked Oct 16 '25 14:10

user7157732


1 Answers

You can try this;

OracleConnection conn = new OracleConnection("Your Connection string");

//Open the connection to the database
conn.Open();

DataSet dataSet = new DataSet();

OracleCommand cmd = new OracleCommand("your select query");

cmd.CommandType = CommandType.Text;

cmd.Connection = conn;

using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
{
  dataAdapter.SelectCommand = cmd;
  dataAdapter.Fill(dataSet);
}
like image 115
Caner Tezcan Avatar answered Oct 18 '25 05:10

Caner Tezcan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!