• Using Azure Functions get the product from SQL server in Azure, the connection to database is established and then the product details are read, there are two end points getProduct by ID and getAllProducts. If any exception happens then OkObjectResult is returned with “No records found.”
  • Q. Will it reach the connection.Close() since there is a return statement before it.
  • function_json
  • There are some SQL select commands in below.
  • Was theARM_template created automatically here?
  • Q. do the obj directory in project mean anything?

GetProduct.cs

using System;
 
using System.IO;
 
using System.Threading.Tasks;
 
using Microsoft.AspNetCore.Mvc;
 
using Microsoft.Azure.WebJobs;
 
using Microsoft.Azure.WebJobs.Extensions.Http;
 
using Microsoft.AspNetCore.Http;
 
using Microsoft.Extensions.Logging;
 
using Newtonsoft.Json;
 
using System.Data.SqlClient;
 
using System.Collections.Generic;
 
  
 
namespace sqlfunction
 
{
 
    public static class GetProduct
 
    {
 
        [FunctionName("GetProducts")]
 
        public static async Task<IActionResult> RunProducts(
 
            [HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req,
 
            ILogger log)
 
        {
 
            log.LogInformation("Get data from the database");
 
            List<Product> _product_lst = new List<Product>();
 
            string _statement = "SELECT ProductID,ProductName,Quantity from Products";
 
            SqlConnection _connection = GetConnection();
 
  
 
            _connection.Open();
 
  
 
            SqlCommand _sqlcommand = new SqlCommand(_statement, _connection);
 
  
 
            using (SqlDataReader _reader = _sqlcommand.ExecuteReader())
 
            {
 
                while (_reader.Read())
 
                {
 
                    Product _product = new Product()
 
                    {
 
                        ProductID = _reader.GetInt32(0),
 
                        ProductName = _reader.GetString(1),
 
                        Quantity = _reader.GetInt32(2)
 
                    };
 
  
 
                    _product_lst.Add(_product);
 
                }
 
            }
 
            _connection.Close();            
 
  
 
            return new OkObjectResult(_product_lst);
 
        }
 
  
 
        private static SqlConnection GetConnection()
 
        {
 
            string connectionString = "Server=tcp:appserver6000.database.windows.net,1433;Initial Catalog=appdb;Persist Security Info=False;User ID=sqlusr;Password=Azure@123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
 
            return new SqlConnection(connectionString);
 
        }
 
  
 
        [FunctionName("GetProduct")]
 
        public static async Task<IActionResult> RunProduct(
 
            [HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req,
 
            ILogger log)
 
        {
 
  
 
            int ProductID = int.Parse(req.Query["Id"]);
 
  
 
            string _statement = String.Format("SELECT ProductID,ProductName,Quantity from Products WHERE ProductID={0}",ProductID);
 
            SqlConnection _connection = GetConnection();
 
  
 
            _connection.Open();
 
  
 
            SqlCommand _sqlcommand = new SqlCommand(_statement, _connection);
 
            Product _product = new Product();
 
  
 
            try
 
            {
 
                using (SqlDataReader _reader = _sqlcommand.ExecuteReader())
 
                {
 
                    _reader.Read();                    
 
                    _product.ProductID = _reader.GetInt32(0);
 
                    _product.ProductName = _reader.GetString(1);
 
                    _product.Quantity = _reader.GetInt32(2);
 
                    var response = _product;
 
                    return new OkObjectResult(response);
 
                }
 
            }
 
            catch(Exception ex)
 
            {
 
                var response = "No Records found";
 
                return new OkObjectResult(response);
 
            }
 
            _connection.Close();
 
  
 
        }
 
    }
 
}

Product.cs (model)

using System;
 
using System.Collections.Generic;
 
using System.Linq;
 
using System.Text;
 
using System.Threading.Tasks;
 
  
 
namespace sqlfunction
 
{
 
    internal class Product
 
    {
 
        public int ProductID { get; set; }
 
        public string ProductName { get; set; }
 
        public int Quantity { get; set; }
 
    }
 
}