Oracle DB Connection with OLEDB in C#

This connection save my time to build CRUD app in easy way.You can build simple UI friendly windows forms using that snippet.
Before you run code, make sure you include your libraries

[code]
using System.Data;
using System.Data.OleDb;
[/code]

It is a code snippet that I use to fill my data grids on windows form app.

[code lang=”csharp”]
OleDbTransaction transaction = null;
OleDbDataReader reader;
OleDbCommand command;
private String _ConnectionString = "Provider = OraOLEDB.Oracle;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=[YOUR HOST IP COMES HERE])(PORT=[PORT COMES HERE])))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=[SERVICE NAME COMES HERE])));User Id=[ID COMES HERE];Password=[PASSWORD COMES HERE];";

public void LoadTableDataToDataGrid(DataGridView dataGridView)
{

using (OleDbConnection connection = new OleDbConnection(_ConnectionString))
{

// OleDbTransaction transaction = null;

// Set the Connection to the new OleDbConnection.
// command.Connection = connection;

// Open the connection and execute the transaction.
try
{

command = new OleDbCommand();
command.Connection = connection;
connection.Open();
command.CommandText =
"SELECT * FROM [YOUR TABLE_NAME]";
var reader = command.ExecuteReader();
var data = reader;

var dt = new DataTable();
dt.Load(reader);
dataGridView.AutoGenerateColumns = true;
dataGridView.DataSource = dt;
dataGridView.Refresh();
/* while (reader.Read()) // FOR CHECK YOUR DATA YOU CAN USE THIS METHOD
{
MessageBox.Show(reader.GetValue(0) + " – " + reader.GetValue(1) + " – " + reader.GetValue(2));

}*/
reader.Close();
// Commit the transaction.
// transaction.Commit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
try
{
// Attempt to roll back the transaction.
transaction.Rollback();
}
catch
{
// Do nothing here; transaction is not active.
}
}
// The connection is automatically closed when the
// code exits the using block.
}
}
[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *