开发者

Checking for an open oracle connection

im trying to write a simple gui form to display if any database connections my app needs are unavilable

    public void tryConnection(List<String> connections, List<String> databaseNames)
    {
        tableLayoutPanel1.RowCount = connections.Count();
        Label label;
        Label sucOrFail;
        String message = "";

        int row = 0;
        for (int i = 0; i < connections.Count(); i++ )
        {
            try
            {
    开发者_JAVA技巧            label = new Label();
                sucOrFail = new Label();
                label.AutoSize = true;
                sucOrFail.AutoSize = true;
                label.Text = databaseNames[i].ToUpper();
                tableLayoutPanel1.Controls.Add(label, 0, row);
                OracleConnection con = new OracleConnection(connections[i]);
                con.Open();

                message = "Success, You Managed to connect to " + databaseNames[i];
                sucOrFail.Text = message;
                sucOrFail.ForeColor = System.Drawing.Color.LawnGreen;
                tableLayoutPanel1.Controls.Add(sucOrFail, 1, row);
                con.Close();
                row++;
            }
            catch (OracleException e)
            {
                Console.WriteLine("failure");
            }
        }

is there anyway i can amend this so that if con.open() fails it doesnt skip in to the catch, because when this happens i loose my place in the loop and cant continue as i would need to.

For example

if (con.open())
{
    message = ......
}
else
{
    message = .....
}


Why not just use an inner try/catch inside the loop:

 for (int i = 0; i < connections.Count(); i++ )
 {
    try {
      con.Open();
      // connection ok
    }
    catch (OracleException e) {
      // couldn't connect
    }
 }


You should have dedicated try catch block around the con.Open().

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜