Oracle connectivity in C#

  • Thread starter Thread starter New Devil
  • Start date Start date
N

New Devil

can anyone send a code for the oracle connectivity wid C#.. would be
really tahnkful ..
Desert Desperado 8)
 
The following code explains how to connect to oracle db and display fetched data into a datagrid :o)

1. Create a winforms project and add a datagrid control on it and call it as datagrid1
2. Add a button control on the form and call it button1.
3. Add a reference to System.Data.OracleClient.dll

Then add following lines near the using statements
using System.Data;
using System.Data.OracleClient;
Here is the code for fetchind data from oracle table and displaying it in the datagrid when user clicks on the button.

private void button1_Click(object sender, System.EventArgs e)
{
try
{
System.Data.OracleClient.OracleConnection objConn =
new System.Data.OracleClient.OracleConnection("Data Source=<your db service name>;User ID='<user name>'; password='<password>'" );
//note that user id and password values are specified in single quotes
objConn.Open();

String strCommand;
strCommand ="Select * from <table name>";
//Create new command object

System.Data.OracleClient.OracleCommand objCommand = new System.Data.OracleClient.OracleCommand(strCommand,objConn);

DataSet ds=new DataSet() ;
System.Data.OracleClient.OracleDataAdapter objDa = new System.Data.OracleClient.OracleDataAdapter (objCommand);

objDa.Fill(ds);
dataGrid1.DataSource = ds;
}
catch(System.Exception ex)
{
MessageBox.Show (ex.Message);
}
}

HTH,
Ashutosh

New Devil said:
can anyone send a code for the oracle connectivity wid C#.. would be
really tahnkful ..
Desert Desperado 8)
 
Back
Top