开发者

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.


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;
        }
    }
}


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

This worked for me on Oracle 11g


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.")
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜