check connection from C# app to oracle 10g database

空扰寡人 提交于 2020-01-17 02:40:07

问题


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.


回答1:


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



回答2:


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

This worked for me on Oracle 11g




回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/7475233/check-connection-from-c-sharp-app-to-oracle-10g-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!