user572844
user572844

Reputation:

check connection from C# app to oracle 10g database

How can I check connection from C# app to oracle 10g database?

For example I have Oracle server on machine with IP 10.50.65.2. I maintain IP, Port in app.config.

I move app to another pc connected to another network. I need check if it is possible create correct connection with database server.

If it is not possible create db connection I need show only simple message please modify db connection data in app.config.

Upvotes: 0

Views: 13158

Answers (4)

BrandonFlynn-NB
BrandonFlynn-NB

Reputation: 400

if(conn.State == ConnectionState.Open)
{
    conn.Close();
}

This worked for me on Oracle 11g

Upvotes: 1

mcauthorn
mcauthorn

Reputation: 598

The oracle Connection has a .Open

so you would do something like this

 if (conn.State  == OracleConnection.Open)
    {  
           //connection is already open
    }
    else 
    {
        //connection is in another state and can be used
    }

Mind you I am assuming that you know your state since you are using a static ip and such defined in your app.config. Otherwise I would say Chris's Answer will work if you do not have a singleton holding your connection to the DB.

Upvotes: 0

MNGwinn
MNGwinn

Reputation: 2394

Why don't you just try to create the connection and catch the exception if it fails?

Perhaps something like this:

public bool CheckConnection()
{
    string connectionString = ""; //Get from configuraiton.
    using(var conn = new OracleConnection(connectionString))
    {
        try
        {
            conn.Open();
            return true;
        }
        catch
        {
            return false;
        }
    }
}

Upvotes: 5

Michael Holman
Michael Holman

Reputation: 961

try something like this maybe?

OracleConnection conn = new OracleConnction(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
try{
    conn.Open();
catch(Exception){
    System.Console.Out.WriteLine("Please modify db connection data in app.config.")
}

Upvotes: 0

Related Questions