Introduction

Advantages of Stored Procedures

Creating First Stored Procedure
    1. Create a new Console app.
    2. Right-click on Project -> Manage NuGet Package.
    3. Search for ‘documentdb’ under the Browse tab.
    4. Select Microsoft.Azure.DocumentDB package and click "Install".
      Azure Cosmos DB
Initialize Document Client Instance
  1. In Program.cs, add a reference to Configuration;
  2. Add a reference to Microsoft.Azure.Documents.Client;
  3. Retrieve the Endpoint URL and Primary Key.
    1. using Microsoft.Azure.Documents;
    2. using Microsoft.Azure.Documents.Client;
    3. using System;
    4. using System.Configuration;
    5. using System.Linq;
    6. using System.Threading.Tasks;
    7. namespace CosmosDBCrudOperationd
    8. {
    9. class Program
    10. {
    11. static void Main(string[] args)
    12. {
    13. Task.Run(async () =>
    14. {
    15. var endpoint = ConfigurationManager.AppSettings["DocDbEndpoint"];
    16. var masterKey = ConfigurationManager.AppSettings["DocDbMasterKey"];
    17. using (var client = new DocumentClient(new Uri(endpoint), masterKey))
    18. {
    19. }
    20. }).Wait();
    21. }
    22. }
    23. }

Execute Stored Procedure

  1. Read Stored Procedure text from spHelloWorld.js.
    1. var sprocBody = File.ReadAllText(@"..\..\StoredProcedures\spHelloWorld.js");
  1. Create SP definition.
    1. var spDefinition = new StoredProcedure
    2. {
    3. Id = "spHelloWorld",
    4. Body = sprocBody
    5. };
  1. Create a new SP.
    1. StoredProcedure sproc = await client.CreateStoredProcedureAsync
    2. (UriFactory.CreateDocumentCollectionUri("dbFamily", "Families"), spDefinition);
    3. Console.WriteLine($"\r\nCreated Store procedure Id:{sproc.Id} ");
    Note - You need to create a database (dbFamily) and Collection (Families) before creating an SP as SPs are stored at Collection level. You can create a Database and Collection using portal or in Code.
  1. Execute Stored Procedure
    1. //Execute Store Procedure
    2. var result = await client.ExecuteStoredProcedureAsync<string>
    3. (UriFactory.CreateStoredProcedureUri("dbFamily", "Families", "spHelloWorld"));
    4. Console.WriteLine($"Executed Store Procedure: response:{result.Response}");
  1. Delete Stored Procedure
    1. //Delete Store Procedure
    2. await client.DeleteStoredProcedureAsync
    3. (UriFactory.CreateStoredProcedureUri("dbFamily", "Families", "spHelloWorld"));
    4. Console.WriteLine("Stored Procedure Deleted Successfully");
Overall Code
  1. using Microsoft.Azure.Documents;
  2. using Microsoft.Azure.Documents.Client;
  3. using System;
  4. using System.Configuration;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. namespace AzureCosmosDbStoredProcedure
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Task.Run(async () =>
  14. {
  15. var endpoint = ConfigurationManager.AppSettings["DocDbEndpoint"];
  16. var masterKey = ConfigurationManager.AppSettings["DocDbMasterKey"];
  17. using (var client = new DocumentClient(new Uri(endpoint), masterKey))
  18. {
  19. var sprocBody = File.ReadAllText(@"..\..\StoredProcedures\spHelloWorld.js");
  20. var spDefinition = new StoredProcedure
  21. {
  22. Id = "spHelloWorld",
  23. Body = sprocBody
  24. };
  25. //Create a Store Procedure
  26. StoredProcedure sproc = await client.CreateStoredProcedureAsync
  27. (UriFactory.CreateDocumentCollectionUri("dbFamily", "Families"), spDefinition);
  28. Console.WriteLine($"\r\nCreated Store procedure Id:{sproc.Id} ");
  29. //Execute Store Procedure
  30. var result = await client.ExecuteStoredProcedureAsync<string>
  31. (UriFactory.CreateStoredProcedureUri("dbFamily", "Families", "spHelloWorld"));
  32. Console.WriteLine($"Executed Store Procedure: response:{result.Response}");
  33. //Delete Store Procedure
  34. await client.DeleteStoredProcedureAsync
  35. (UriFactory.CreateStoredProcedureUri("dbFamily", "Families", "spHelloWorld"));
  36. Console.WriteLine("Stored Procedure Deleted Successfully");
  37. }
  38. }).Wait();
  39. }
  40. }
  41. }