locked
How to create Xamarin Android App and ASP.net API to Access online SQL Server database RRS feed

  • Question

  • User364100 posted

    Hello, I need to access my online SQL server database using Xamarin android app and ASP.Net Api. I need some code sample to write Android app as well as ASP.Net API.

    Thursday, December 28, 2017 6:07 PM

All replies

  • User354499 posted

    Your question is kinda broad and mostly not Xamarin specific. But, maybe try starting here:

    https://developer.xamarin.com/recipes/android/webservices/consumingservices/callarestwebservice/

    I suggest a Rest Web service as the middle-man for your SQL Database with httpget api methods. Calling this from within Android / Xamarin isn't much different than calling it from anywhere else.

    Thursday, December 28, 2017 9:46 PM
  • User12885 posted

    I concur with knottydev here. On the Azure side, I suggest creating an App Service. You can then use the easy API's feature to script your SQL access and return your data as a JSON object.

    Something like this

    exports.get = function(request, response) {
        var callback = request.query.callback; 
        /* Potential parameters
            Primary: State, country, year, year and month, type
            Secondary: Guest 
        */
        var myQuery  = {published:true};
        if(request.query.state !== undefined){
            myQuery["state"] =  request.query.state ;
        }
        var tables = request.service.tables;
        var table = tables.getTable('Conventions');
        var task = table
            .where(myQuery)
            .orderBy('start_date')
            /*select('name')*/
            .read(
                { success: function(results) {
                    var result;
                    var returnStatus = statusCodes.OK;
                    result = callback + "([";
                    if (results.length !== undefined && results.length > 0) {
    
                        for (var i=0; i<results.length;i++){
    
                            if(i>0){
                                result += ", ";
                            }
                            result +="{ 'name' : '" + results[i].name + "'," +
                                "'type' : '" + results[i].type + "'," +
                                "'city' : '" + results[i].city + "'," +
                                "'state' : '" + results[i].state + "'," +
                                "'country' : '" + results[i].country + "'," +
                                "'region' : '" + results[i].region + "'," +
                                "'facility' : '" + results[i].facility + "'," +
                                "'start' : '" + parseDate(results[i].start_date)  + "'," +
                                "'end' : '" + parseDate(results[i].end_date) + "'," +
                                "'www' : '" + results[i].www + "'," +
                                "'desc' : '" + results[i].description + "'," +
                                "'guests' : '" + results[i].guests + "'," +
                                "'events' : '" + results[i].events + "'}" ;
                        }
                    } else {
                        /* This gets executed even if the the column does not exist, eg te4xt*/
                        result += "{'noitems' : 'no results returned'}";
                        //returnStatus = 501;
                    }
                    result += "])";
                    response.send(returnStatus, result);
                }
            });
            function parseDate(dtStr){
                return (dtStr.getMonth()+1)+"/"+(dtStr.getDate())+"/"+dtStr.getFullYear();
            }
    };
    
    Saturday, December 30, 2017 9:51 PM