How to get the row count from an azure database?

做~自己de王妃 提交于 2019-12-01 18:00:55

问题


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??


回答1:


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.




回答2:


If anybody comes 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




回答3:


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');


来源:https://stackoverflow.com/questions/15616486/how-to-get-the-row-count-from-an-azure-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!