1

In my function app I have an IF statement that launches a main part of code only if two variables are set to 1 and 0. These variables are flags stored in the Azure SQL Table. I have made a simple select query, however the variable values are always -1. How can I solve this problem? Here is my code:

conn.Open();
    int IsDatamartSynced, IsCubeProcessed;
    var select_for_IsDatamartSynced = "SELECT TOP (1) IsDatamartSynced FROM dbo.test";
    var select_for_IsCubeProcessed = "SELECT TOP (1) IsCubeProcessed FROM dbo.test";
    /*var text = "UPDATE SalesLT.SalesOrderHeader " + 
            "SET [Status] = 5  WHERE ShipDate < GetDate();";*/

    using (SqlCommand cmd = new SqlCommand(select_for_IsDatamartSynced, conn))
    {
        IsDatamartSynced = cmd.ExecuteNonQuery();
        log.Info($"in using {IsDatamartSynced}");
    }

    using (SqlCommand cmd = new SqlCommand(select_for_IsCubeProcessed, conn))
    {
        IsCubeProcessed = cmd.ExecuteNonQuery();
        log.Info($"in using {IsCubeProcessed}");
    }

1 Answer 1

3

ExecuteNonQuery returns the number of rows affected for statements that change data, or -1 for other types of queries (including SELECT).

Use ExecuteScalar when you want to retrieve a value:

using (SqlCommand cmd = new SqlCommand(select_for_IsDatamartSynced, conn))
{
    IsDatamartSynced = cmd.ExecuteScalar();
    log.Info($"in using {IsDatamartSynced}");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.