我有一个用 C# 构建的通知应用程序,用于通知数据库中的任何更改。应用程序在后台运行。但问题是在应用程序启动后,如果互联网关闭,应用程序会抛出 SQLException 。我已经使用 try catch 来处理异常。但我希望我的应用程序尝试连接数据库,并在建立连接后返回主代码。
try
{
using (SqlConnection connection =
new SqlConnection(GetConnectionString()))
{
//i want to return here when the connection is reestablished
using (SqlCommand command =
new SqlCommand(GetListenerSQL(), connection))
{
connection.Open();
// Make sure we don't time out before the
// notification request times out.
command.CommandTimeout = NotificationTimeout;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
messageText = System.Text.ASCIIEncoding.ASCII.GetString((byte[])reader.GetValue(13)).ToString();
// Empty queue of messages.
// Application logic could parse
// the queue data and
// change its notification logic.
}
object[] args = { this, EventArgs.Empty };
EventHandler notify =
new EventHandler(OnNotificationComplete);
// Notify the UI thread that a notification
// has occurred.
this.BeginInvoke(notify, args);
}
}
}
catch(SqlException e)
{
}
是否可以在没有 goto 语句的情况下进行。我宁愿避免 goto 语句。
繁星点点滴滴
相关分类