Use SQL Server view in NEW Azure App Service

后端 未结 3 1852
渐次进展
渐次进展 2020-12-21 11:25

I am new to Azure App Service.

I\'ve created a view in Azure database to get data across several tables. Now I want to use the data from this view in my Cordova App

相关标签:
3条回答
  • 2020-12-21 12:03

    Things can be so easy sometimes ;-)

    All I need to do is to write the following lines to my table controller:

    var table = require('azure-mobile-apps').table();
    table.databaseTableName = 'ViewName';
    module.exports = table;
    

    Thanks for your help!

    0 讨论(0)
  • 2020-12-21 12:04

    You are pretty much right there. You need to just create a table controller to access the view. Ensure you have the system columns defined (version, updatedAt, createdAt, deleted and id). Also, ensure the right thing happens when you update the view (e.g. with an INSERT, UPDATE, DELETE) as that will tell you what needs to be done with the controller (for example, if you can't insert/update/delete, then make it read-only).

    Reference blog post for you: https://shellmonger.com/2016/04/15/30-days-of-zumo-v2-azure-mobile-apps-day-8-table-controller-basics/

    0 讨论(0)
  • 2020-12-21 12:08

    Had a similar issue but fixed it now, am using a .Net backend thou. 1. Log into azure portal and select the database you want to create the view in Then select the query Editor in the left pane. 2. Use the sql to create the query. 3.Create a table in the asp.net backend that matches your fields in your view. 4.Go to your asp.net backend and create a custom Api and write the following codes:

    public class HelloCustomController : ApiController
    {
        MobileServiceContext context;
        public HelloCustomController()
        {
            context = new MobileServiceContext();
        }
    
        [HttpGet]
        public async Task<List<vwUser>> Get()
        {
            try
            {
                var users = context.Database.SqlQuery<vwUser>("Select * from 
                 dbo.vwUser);
                return await users.ToListAsync();
            }
            catch(Exception ex)
            {
                return new List<vwUser>()
                {
                    new vwUser
                    {
                        UserName=ex.Message
                    }
                };
            }
         }
    

    4.You edit my codes to select from your view. 5. Create a similar class in your mobile app that matches what u created in your backend. 5. You can then access your view by calling it in your mobile app with the following codes:

    var users= await client.InvokeApiAsync<vwUser>("HelloCustom",HttpMethod.Get,null);
    

    Thanks Guys. This is my fisrt answer to this beautiful community that carried me from day 1 of programming till now that am a bit conversant.

    0 讨论(0)
提交回复
热议问题