Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagec#
using Microsoft.Data.SqlClient;
using System.Threading.Tasks;

[FunctionName("DatabaseCleanup")]
public static async Task Run([TimerTrigger("*/15 * * * * *")]TimerInfo myTimer, ILogger log)
{
    // Get the connection string from app settings and use it to create a connection.
    var str = Environment.GetEnvironmentVariable("sqldb_connection");
    using (SqlConnection conn = new SqlConnection(str))
    {
        conn.Open();
        var text = "UPDATE SalesLT.SalesOrderHeader " + "SET [Status] = 5  WHERE ShipDate < GetDate();";

        using (SqlCommand cmd = new SqlCommand(text, conn))
        {            
            var rows = await cmd.ExecuteNonQueryAsync();// Execute the command and log the # rows affected.
            log.LogInformation($"{rows} rows were updated");
        }
    }
}

This function runs every 15 seconds to update the Status column based on the ship date.