我使用此代码在Azure-Documentdb中创建StoredProcedure。当它被执行时,它不会将任何结果响应给" sproc"内部"如果"条件。但SP已在Azure-DocumentDB中创建, 使用完整代码进行编辑
var isUserExist= false;
//string commandText = m_EtikadoUser.commandText;
var collection = await GetOrCreateCollectionAsync();
try
{
string scriptFileName = @"C:\Users\PC24\Documents\Visual Studio 2013\Projects\Etikado_Web\EtikadoBusinessEntity\StoredPprocedures\GetUserByUserNameAndPassword.js";
string scriptId = Path.GetFileNameWithoutExtension(scriptFileName);
var sproc = new Microsoft.Azure.Documents.StoredProcedure
{
Id = scriptId,
Body = File.ReadAllText(scriptFileName)
};
//var sproc1=await client.creates
var sproc1 = (from sp in client.CreateStoredProcedureQuery(collection.SelfLink)
where sp.Id == sproc.Id
select sp).ToArray().FirstOrDefault();
if (sproc1 == null)
{
sproc = await client.CreateStoredProcedureAsync(collection.SelfLink, sproc);
}
var response = await client.ExecuteStoredProcedureAsync<string>(sproc.SelfLink);//, m_EtikadoUser.Name, m_EtikadoUser.Password);
if (response != null)
{
isUserExist = true;
await client.DeleteStoredProcedureAsync(sproc.SelfLink);
}
//var queryPrint = await client.CreateDocumentQuery(collection.SelfLink, "SELECT * FROM c where c.Name = '" + m_EtikadoUser.Name + "' and c.Password='" + m_EtikadoUser.Password + "'");
//isUserExist = true;
}
catch (Exception ex)
{
}
return isUserExist;
这是我的SP:
function simple(prefix) {
var collection = getContext().getCollection();
// Query documents and take 1st item.
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
//'SELECT * FROM c where c.Name = "' + UserName + '" and c.Password="' + Password + '"',
"SELECT * FROM c",
function (err, feed, options) {
if (err) throw err;
// Check the feed and if it's empty, set the body to 'no docs found',
// Otherwise just take 1st element from the feed.
if (!feed || !feed.length) getContext().getResponse().setBody("no docs found");
else getContext().getResponse().setBody(prefix + JSON.stringify(feed[0]));
});
if (!isAccepted) throw new Error("The query wasn't accepted by the server. Try again/use continuation token between API and script.");
}