-->

20 February 2020

ADO.NET OracleConnection

  Asp.Net CS By Example       20 February 2020
 ADO.NET OracleConnection 

 In this post we are getting more details about OracleConnection Class.We use an object of the OracleConnection class to connect to a Oracle Database.

 Although this class is specific to Oracle Database, many of the properties and methods in this class are the same as those for the SqlConnection,OleDbConnection and OdbcConnection classes. If a property or method is specific to OracleConnection, it says so in the Description column of the tables shown in this section.

OracleConnection Properties:

Sr.No Property Type Description
1) ConnectionString string Gets or sets the string used to open a database.
2) ConnectionTimeout int Gets the number of seconds to wait while trying to establish a connection to a database.
3) Database string Gets the name of the current database.
4) DataSource string Gets the name of the database.
5) ServerVersion string Gets a string containing the version of Oracle Database.
6) State ConnectionState Gets the current state of the connection.

OracleConnection Methods:

Sr.No Method Return Type Description
1) BeginTransaction() OracleConnection Begins a database transaction.
2) ChangeDatabase() void Changes the current database for an open connection.
3) Close() void Closes the connection to the database.
4) CreateCommand() OracleCommand Creates and returns a command object.
5) Open() void Opens a database connection with the property settings specified by the ConnectionString property.

 Now here we look sample code how to use Connection Class of Ado.Net. We have make connection to oracle database using OracleConnection Class in Console Project.
  Code: Program.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;

namespace CsApp_Connection
{
    class  Program
    {
        public static void Main()
        {
            string connectionString = "data source=LearnAsp;user id=LearnAsp;"
            + "password=LearnAsp; Pooling=true; Max pool size=200; Min pool size=0;";

            Console.WriteLine("\tADO.NET Connection Object Example"");
            OracleConnection myConn = new OracleConnection(connectionString);

             try
            {
                // Open connection
                myConn.Open();
                 Console.WriteLine("\tConnection opened.");
            }
            catch (OracleException ex)
            {
                // Display error
                Console.WriteLine("\tError: " + ex.Message + ex.StackTrace);
            }

            finally
            {
                // Close connection 
                myConn.Close();
                 Console.WriteLine("\tConnection closed.");
            }
            myConn.Close();
        }
    }
} 

Output
Ado.Net Connection Exmaple
logoblog

Thanks for reading ADO.NET OracleConnection

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.