【发布时间】:2023-03-18 23:11:02
【问题描述】:
我正在尝试编写一个简单的 gui 表单来显示我的应用程序需要的任何数据库连接是否不可用
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
{
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");
}
}
无论如何我都可以修改它,这样如果 con.open() 失败它就不会跳到 catch 中,因为当这种情况发生时,我会失去我在循环中的位置并且不能继续我需要的。
例如
if (con.open())
{
message = ......
}
else
{
message = .....
}
【问题讨论】: