Pan Weerasekara
Pan Weerasekara

Reputation: 85

Blob storage id can not inserting to Azure SQL database through Azure function

I have created a function app and the purpose is to insert the blob storage text details into Azure database "Title" column in created, the function call "BlobTriggerCSharp1" and created with c#

#r "System.Configuration"
#r "System.Data"
using System.Configuration;
using System.Data.SqlClient;
using System.Threading.Tasks;
using System;

public static void Run(Stream myBlob, string name, TraceWriter log)
{
    log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
    string detail = ($"{name}");


    var str = ConfigurationManager.ConnectionStrings["sqldb_connection"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(str))
    {
        conn.Open();
        var text = "INSERT INTO PhotoTable(ID,CreatedAt,UpdatedAt,IsDeleted, Url, Title)VALUES (284,SYSDATETIMEOFFSET(),SYSDATETIMEOFFSET(), 'true', 'yrhrh', {name})";

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

so when the text blob uploads the function triggers but it doesn't enter the blob id in to the column in the Azure SQLdatabase ( database columns are defined in the query ID,CreatedAt,UpdatedAt,IsDeleted, Url, Title) as it shows above the parameter i am going to pass to the database is "{name}" ( because Name:{name} is the variable of id in the blob-storage) which is the Blob-storage unique id ( in other words need to transfer the {name} to the Title column in database) , i am so confused what i am missing and don't know my way of going to transfer is wrong or else the datatype is wrong, so confused and stuck, help will be greatly appreciate , thanks

Upvotes: 0

Views: 319

Answers (1)

Mikhail Shilkov
Mikhail Shilkov

Reputation: 35144

You should change your INSERT statement to add parameters to it. Here is an example with Name:

var text = "INSERT INTO PhotoTable(ID,CreatedAt,UpdatedAt,IsDeleted, Url, Title) " +
           "VALUES (284,SYSDATETIMEOFFSET(),SYSDATETIMEOFFSET(), 'true', 'yrhrh', @Name)";

using (SqlCommand cmd = new SqlCommand(text, conn))
{
    cmd.Parameters.AddWithValue("@Name", name);

    // Execute the command and log the # rows affected.
    var rows = cmd.ExecuteNonQueryAsync();
    log.Info($"{rows} rows were updated");
}

Upvotes: 1

Related Questions