-->

20 March 2020

ADO.NET Displaying Connection Information

  Asp.Net CS By Example       20 March 2020
 ADO.NET Displaying Connection Information 

 In this post, we'll see how to write a program to display connection information. Connections have several properties that provide information about the connection. Most of these properties are read-only, since their purpose is to display rather than set information.

 To Known more about ado.net click here.


 Now here we look sample code how to use display Connection Class properties 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.");
                  // Display connection properties
                Console.WriteLine("Connection Properties:");
                Console.WriteLine("\tConnection String: {0}",myConn.ConnectionString);
                Console.WriteLine("\tDatabase: {0}", myConn.Database);
                Console.WriteLine("\tDataSource: {0}", myConn.DataSource);
                Console.WriteLine("\tServerVersion: {0}", myConn.ServerVersion);
                Console.WriteLine("\tState: {0}", myConn.State);
            }
            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 Displaying Connection Information
logoblog

Thanks for reading ADO.NET Displaying Connection Information

Previous
« Prev Post

No comments:

Post a Comment

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