最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the row count from an azure database? - Stack Overflow

programmeradmin4浏览0评论

Am working on a windows store javascript application. The application uses data from azure mobile services. Consider the below code:

var itemTable = mobileService.getTable('item');
//item is the table name stored in the azure database

The code fetches the entire table item and saves it to a variable itemTable.

What code will return the no of rows present in itemTable??

Am working on a windows store javascript application. The application uses data from azure mobile services. Consider the below code:

var itemTable = mobileService.getTable('item');
//item is the table name stored in the azure database

The code fetches the entire table item and saves it to a variable itemTable.

What code will return the no of rows present in itemTable??

Share Improve this question edited Mar 25, 2013 at 14:07 SliverNinja - MSFT 31.7k12 gold badges118 silver badges181 bronze badges asked Mar 25, 2013 at 13:43 Amar ZenoAmar Zeno 4121 gold badge6 silver badges23 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

What you're looking for is the includeTotalCount method on the table/query object (unfortunately it's missing from the documentation, I'll file a bug to the product team to have it fixed).

When you call read on the query object, it will return by default 50 (IIRC, the number may be different) elements from it, to prevent a naïve call from returning all elements in a very large table (thus either incurring the outbound bandwidth cost for reserved services, or hitting the quota for free ones). So getting all the elements in the table, and getting the length of the results may not be accurate.

If all you want is the number of elements in the table, you can use the code below: returning zero elements, and the total count.

    var table = client.getTable('tableName');
    table.take(0).includeTotalCount().read().then(function (results) {
        var count = results.totalCount;
        new Windows.UI.Popups.MessageDialog('Total count: ' + count).showAsync();
    });

If you want to query some elements, and also include the total count (i.e., for paging), just add the appropriate take() and skip() calls, and also the includeTotalCount as well.

If anybody es here and interested in how to get the totalCount only on C# (like me), then this is how you do it:

var table =  MobileService.GetTable<T> ();
var query = table.Take(0).IncludeTotalCount();
IList<T> results = await query.ToListAsync ();
long count = ((ITotalCountProvider)results).TotalCount;

Credit goes to this blog post here

You need to execute read() on the table query and then get the length of the results.

var items, numItems;
itemTable.read().then(function(results) { items = results; numItems = items.length; });

If you are only showing a record count and not the entire results - you should just select the ID column to reduce the amount of data transmitted. I don't see a count() method available yet in the JS Query API to fill this need.

var itemTable = mobileService.getTable('item').select('itemID');
发布评论

评论列表(0)

  1. 暂无评论