C# Application to Firebase Database - Updating Data Realtime
In this post we will be going through how data can be updated real time to Googles Firebase from a C# application. Please ensure you have created a database project within Firebase and have the link to your database ready. Once you have done this we can begin. A web request will be used to carry out the functionality and the entire process is extremely similar to what we had done to insert data and so If you have read our post on how to insert data to firebase then feel free to scroll down to the full source code where you should notice what we have changed. If this is your first read then please continue as normal.
In the next couple of weeks, we will be posting an article which will go through how Firebase works, this will act as an introduction for those who may not be familiar with it.
We will now proceed to the code, to begin with we will go through the header files being used
The first header we see being used is System, this header file isnt needed but has been added for the sake of this example. In this example we are updating the current date and time in to the database and so we are relying on the system to provide us with this information which is later to be stored. If you are for example updating a fixed variable or a variable that is being received from an input form. This header file can then be removed. It is also not required for the System.Windows.Forms header file to be included either, although assuming you were wanting to progress further with this it would be a good idea to have it in there. All others will be needed for this example. Please also ensure you have installed the newtonsoft.json which can be found by carrying out a search in the solution explorer.
We then move on to declaring some variables which in this example are the current date and time. Within the lines of code at the top seen above we can see that two variables Date which consists of YYYY/MM/DD and the other Time which consists of HH:MM:SS and so a possible entry could then be seen as the following
Local Variables: -
Date = 28/09/18
Time = 21:33:45
The variables Date and Time are then converted in to Json, Googles Firebase database will store its data as part of a Json tree whereby each individual piece of data will be added as an additional node. Please be sure to install newtonsoft.json for this process as well as the next steps to work. The variables Name and Value are the names the time and date will have within our Firebase database. Firebase has no structure and is schemaless and so this is where we will be giving our entries of data within the database their Name. Please also note at this point you are able to create multiple variables to be updated within the database.
Database Columns: -
Name = 28/09/18
Value = 21:33:45
Once we have our relevant variables declared, converted and ready to carry out the update, we then move on to creating a web request to our firebase project. We start by providing a URL, the URL of your database should be made available online simply copy and paste it. Once copy and pasted it is EXTREMELY important you add /.json to the end of your URL. If you were then to copy and paste this with/.json at the end of your URL you will see the Json tree in its entirety and you may find it of interest as it will help you get your head around it all. In the second line we see the request method being set to PATCH, the word PATCH in this case is equivalent to UPDATE for all the viewers who tend to use MySQL or MSSQL. The variables in Json are then encoded and written which results in them essentially being updated in to our Google Firebase database project. Whilst this application is running if you were to look at the database project we can see our data update being carried out live.
Below can be seen the code in its entirety for those who may be interested in exploring for themselves. Please if you have any questions or suggestions click here. Also go ahead and subscribe to our Youtube channel, follow us on Instagram or even subscribe to our mailing list