【问题标题】:Call DocumentDb stored procedure from .Net with parameters.使用参数从 .Net 调用 DocumentDb 存储过程。
【发布时间】:2016-04-14 14:30:54
【问题描述】:

调用 sproc 可以正常工作,但我似乎无法将参数传递给它。它在 Azure 门户中按预期工作,但在从 .net 调用时却不能。

documentClient = new DocumentClient(new Uri(endpointUrl), authorizationKeyAdmin);
var sproc = documentClient.CreateStoredProcedureQuery(_companyCollection.StoredProceduresLink, "select * from root r where r.id = \"testSproc\"");
var result = documentClient.ExecuteStoredProcedureAsync<string>("dbs/GypNAB==/colls/GxpNAKrZMwA=/sprocs/GxpNAKrZMwxxxxAAAAAAAgA==/", sproc, "MyParameter");

存储过程并没有真正改变

function sample(rrid) {
    var collection = getContext().getCollection();
    // Query documents and take 1st item.
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        'SELECT top 1 * FROM  c where c.RRID = "'+rrid+'"',
        function (err, feed, options) {
            if (err) throw err;

            // Check the feed and if empty, set the body to 'no docs found', 
            // else take 1st element from feed
            if (!feed || !feed.length) getContext().getResponse().setBody(rrid + 'no docs found!');
            else getContext().getResponse().setBody(JSON.stringify(feed[0]));
        });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

从 .net 调用时的输出是“[object Object]no docs found!”我希望 [Object Object] 是我的参数值。如果我删除参数限制,我会得到一个正确的结果集。此外,如果我使用 azure 中的参数运行它,我会得到正确的结果集。

提前谢谢。

【问题讨论】:

    标签: .net stored-procedures azure-cosmosdb


    【解决方案1】:

    ExecuteStoredProcedureAsync() (sproc) 的第二个参数映射到存储过程中的第一个参数 - 这就是为什么 rrid 在输出中显示为 [object Object]

    尝试运行:

    var result = documentClient.ExecuteStoredProcedureAsync<string>("dbs/GypNAB==/colls/GxpNAKrZMwA=/sprocs/GxpNAKrZMwxxxxAAAAAAAgA==/",  "MyParameter");
    

    【讨论】:

    • 呃,谢谢。我猜我的代码是基于一个不好的例子。也许你可以解释一下,我已经看到了一些先从 sproc name 中选择 * 的例子。这应该用于获取存储过程 ID 还是这样做的目的是什么?
    • 发布 DocumentDB 时,它要求系统生成的_self 链接引用所有资源...这可能是旧样本的基础。大约半年前,通过用户定义的id 路由 - 这使得通过 id 查询资源变得多余。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 2013-06-06
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多