0
votes

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

2 Answers

1
votes

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;
}
0
votes

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.