using System.Data.SqlClient; using System.Threading;
public static void Run(TimerInfo myTimer, TraceWriter log) { log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
int retryCount = 3; //设置重试次数为3次
while (retryCount > 0)
{
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
//执行数据库操作
//...
}
break; //成功连接数据库并执行操作,退出重试循环
}
catch (SqlException e)
{
log.Error($"Database communication link failure: {e.Message}");
retryCount--;
if (retryCount == 0)
{
log.Error("Database connection failed after 3 retries.");
//发送警报等操作
}
Thread.Sleep(1000); //等待1秒后进行下一次重试
}
}
}