示例#1
0
        private static async Task RunSprocDemoAsync(DocumentClient client)
        {
            var CosmosDBUtils = new CosmosDBUtils(client);

            var conferenceDb = await CosmosDBUtils.GetOrCreateDatabaseAsync("Conferences");

            var conferenceCollection = await CosmosDBUtils.GetOrCreateDocumentCollectionAsync(conferenceDb, "FallConferences");

            var mySproc = new StoredProcedure
            {
                Id   = "createDocs",
                Body = "function(documentToCreate) {" +
                       "var context = getContext();" +
                       "var collection = context.getCollection();" +

                       "var accepted = collection.createDocument(collection.getSelfLink()," +
                       "documentToCreate," +
                       "function (err, documentCreated) {" +
                       "if (err) throw new Error('Error oh ' + documentToCreate.Name + '- ' + err.message);" +
                       "context.getResponse().setBody(documentCreated.id)" +
                       "});" +
                       "if (!accepted) return;" +
                       "}"
            };

            var response = await client.CreateStoredProcedureAsync(conferenceCollection.SelfLink, mySproc);

            try
            {
                if (response.StatusCode == System.Net.HttpStatusCode.Created)
                {
                    dynamic conf = Conference.CreateConference(4, new DateTime(2014, 12, 1));

                    await client.ExecuteStoredProcedureAsync <string>(response.Resource.SelfLink, conf);

                    var linqConferences = from c in client.CreateDocumentQuery <Conference>(conferenceCollection.DocumentsLink)
                                          select c;

                    foreach (var conference in linqConferences)
                    {
                        Console.WriteLine("SPROC - Found Conference " + conference.Name + " on " + conference.StartDate);
                    }
                }
                else
                {
                    Console.WriteLine("The sproc didn't get added!");
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                client.DeleteStoredProcedureAsync(response.Resource.SelfLink).Wait();
                RunDeleteDemoAsync(client).Wait();
            }


            Console.ReadKey();
        }
示例#2
0
        private static async Task RunCreateDemoAsync(DocumentClient client)
        {
            var CosmosDBUtils = new CosmosDBUtils(client);

            var conferenceDb = await CosmosDBUtils.GetOrCreateDatabaseAsync("Conferences");

            var conferenceCollection = await CosmosDBUtils.GetOrCreateDocumentCollectionAsync(conferenceDb, "FallConferences");

            await CosmosDBUtils.CreateDocumentAsync <Conference>(conferenceCollection, Conference.CreateConference(1, new DateTime(2014, 10, 18)));

            await CosmosDBUtils.CreateDocumentAsync <Conference>(conferenceCollection, Conference.CreateConference(2, new DateTime(2014, 11, 18)));

            await CosmosDBUtils.CreateDocumentAsync <Conference>(conferenceCollection, Conference.CreateConference(3, new DateTime(2014, 11, 22)));
        }