2020.09.01.

Xamarin.Forms Azure Easy Table Accsess

By bence960206

The easiest way to establish a connection to the database, store, update, delete or retrieve data, if we connect our app with Azure. This requires an Azure users, it can even be free as well (30-day trial version is for everyone and does not then not to Pay back, or will be after I’d buy a new one if you do not like it.) First, we look to be Easy Table to create and then how to communicate with the database.

After logging onto the so-called Dashboard navigating the website. Here on the left you can see three lines / the services. If you only see three lines and icons, click on it. Click on the New button. Enter Mobile App and Create it.

App name: Our app’s name, but just do not have it, actually give a name for the service.

Subscription: Subscription to choose from, what kind of subscription you want to start any subscription “paying” for it. Here, if the subscription is free, you have one, select this option.

Resource Group: You can type anything to this, the point is that this service is related with the app, use what you have written as Resource Group.

App Service Plan: Let’s create a new one, give it a name, choose the continent, which was closest to us (do not have) and select the F1 Free-tier pricing offer.

Application Insights remain OFF. Pin Dashboard was to recommend that you be checked, it is easier to achieve and can be removed at any time.

With this we have created. When you are done, click on to it. Select from the list on the left is the Easy Tables. We realize that we can not. This is because there is no database. Let’s go to the list on the left of the Data Connections in. Here you can add a new one. Go to SQL Database from and Create New Database from. Let’s give him a name, for example, keep the default proba pricing tier. Go to the Target Server, and create a new server from. Here you enter a unique server name, eg xamarinasdserver. Create a user name and password so that we can reach the server. Location may also be possible in the next, but it is not important. Then when you’re done with them, select the left column of the Connection string. Here, the above-specified user name and password to enter. When everything is ready, let’s get out of Easy Table away and create one. Give it any name, eg Users. Let’s get him up on the board and go to Manage Schema on. Here you can add fields, if you want to add something, say the name and the value name should be the String field.

We are ready to sign, now I just need to create an application, which is filled with data. Create a Xamarin.Forms Portable Application. Right click on the Solution and go to Manage NuGet Package for Solution. Here you can download the Azure.MobileServiceClient and use it for all projects. Before we implement any function, go to the iOS project and add this line before we are calling the global :: Xamarin.Forms.Forms.Init (); function:

CurrentPlatform.Init(); -> using Microsoft.WindowsAzure.MobileServices;

Then we can begin to function. Now we are not going to use MVC or MVVM etc., just simply create a class and its methods provided, thereby achieving the database.

Create a User class and create a property:

public class Users

{

public string name { get; set; }

}

Next, create the database reached functions.

Create another class: DatabaseFunctions.cs

Be a static variable:

public static MobileServiceClient MobileService =

new MobileServiceClient(“your azure mobile servic url”);

Let’s look at a query, insert, delete and update functions.

Insert:

public async Task<bool> putUser(Users user)

{

try

{

await MobileService.GetTable<Users>().InsertAsync(user);

return true;

}

catch (Exception e)

{

Debug.WriteLine(e.Message);

return false;

}

}

Query:

public async Task<ObservableCollection<Users> getUsers()

{

try

{

IEnumerable< Users > users =

await MobileService.GetTable<Users >().ReadAsync();

ObservableCollection<Users> returnableUsers =

new ObservableCollection<Users>(users);

return returnableUsers;

}

catch (Exception e)

{

Debug.WriteLine(e.Message);

return new ObservableCollection<Users>();

}

}

Delete:

public async Task<bool> deleteUser(Users user)

{

try

{

await MobileService.GetTable<Users>().DeleteAsync(user);

return true;

}

catch (Exception e)

{

Debug.WriteLine(e.Message);

return false;

}

}

Update: Delete the old user and then put the new.