0

I'm just new to Azure Function ,I just gone through the TimerTrigger for Sql Connection and BlobTrigger for Azure Blob.I tired with the demo works fine for me. Next i tried to do with combination of both this.

When a file uploaded/Added in the Specific Blob Container.I should write the blob name in my Azure SQL Database table.

How could i achieve this in a Single Azure function ?

Also i'm having a doubt that if we create a azure function for a blob trigger,then this function will always be running in the background ? I mean it will consume the Background running cost ?

I'm thinking that azure function for a blob trigger will consume the cost only during it's run. Isn't it ?

Could somebody help me with this

2 Answers 2

1

How could i achieve this in a Single Azure function ?

You could achieve it using blob trigger. You will get blob name from the function parameter [name]. Then you could save this value to your Azure SQL database. Sample code below is for your reference.

public static void Run(Stream myBlob, string name, TraceWriter log)
{
    var str = "connection string of your sql server";
     using (SqlConnection conn = new SqlConnection(str))
     {
        conn.Open();
        var text = "insert into mytable(id, blobname) values(@id, @blobname)";

        using (SqlCommand cmd = new SqlCommand(text, conn))
        {
            cmd.Parameters.AddWithValue("id", 1);
            cmd.Parameters.AddWithValue("blobname", name);
            // Execute the command and log the # rows affected.
            var rows = cmd.ExecuteNonQuery();
            log.Info($"{rows} rows were updated");
        }
     }
}

I'm thinking that azure function for a blob trigger will consume the cost only during it's run. Isn't it?

You will need to choose hosting plan when creating an Azure function.

enter image description here

If you choose App Service Plan, you will need to pay for the App Service Plan which is depends on the tier you chosen. If you choose Consumption plan, your function is billed based on two things. Resource consumption and executions.

Resource consumption is calculated by multiplying average memory size in Gigabytes by the time in seconds it takes to execute the function. You need to pay for the CPU and Memory consumed by your function. Executions means the requests count which are handled by your function. Please note that Consumption plan pricing also includes a monthly free grant of 1 million requests and 400,000 GB-s of resource consumption per month.

We can also call the Sp (like Exec spname) in the place of Insert Command?Right ?

Yes, we could call the sp by setting CommandType to StoredProcedure. Code below is for your reference.

using (SqlCommand cmd = new SqlCommand("StoredProcedure Name", conn))
{
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Amor -MSFT.Just a edit request in the line 3 Var str=ConfigurationManager.ConnectionStrings["Connections String"].ConnectionString;
-MSFT We can also call the Sp (like Exec spname) in the place of Insert Command?Right ?
If we want to give param for our Sp ? The Updated Answer contains the Sp name alone without a Param
Add parameters to SP will be same as adding parameters to SQL query. Please use cmd.Parameters.AddWithValue method.
0

Sure, you should use Blob Trigger for your scenario.

If you use Consumption Plan, you will only be changed per event execution. No background cost will apply.

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.