Updating data within Firebase database through use of a C# application.
In this post we will cover updating data in Firebase using a C# application. Do ensure you have created a database project within Firebase and have the link to your database ready. A web request will be used to carry out the functionality and the process is similar to what we had done to insert data.
We will now proceed to the code, to begin with we will go through the header files being used.
using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
We then move on to declaring some variables which in this example are the current date and time.
DateTime date = DateTime.Now;
string Date = date.ToString("yyyy:MM:dd");
string Time = date.ToString("HH:mm:ss");
var json = Newtonsoft.Json.JsonConvert.SerializeObject(new
{
Name = Time,
Value = Date,
})
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. The default firebase variables “Name” and “Value” are then passed “Time” and “Date”. Note that Firebase has no structure and is schemaless and so if you would like for added structure or more than 2 variables, it would be ideal to declare this information here.
Database Columns: –
Name = 28/09/18
Value = 21:33:45
Once we have our relevant variables declared, we then move on to creating a web request to firebase. We start by providing the URL of our database project. Ensure “/.json” is added to the end of the URL. 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 updated within the database.
var request = WebRequest.CreateHttp("https://myfire-Example.firebaseio.com/.json");
request.Method = "PATCH";
request.ContentType = "application/json";
var buffer = Encoding.UTF8.GetBytes(json);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
var response = request.GetResponse();
json = (new StreamReader(response.GetResponseStream())).ReadToEnd();
Below can be seen the code in its entirety.
using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace UpdatingDataCsharpToFirebase
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
while (true)
{
DateTime date = DateTime.Now;
string Date = date.ToString("yyyy:MM:dd");
string Time = date.ToString("HH:mm:ss");
var json = Newtonsoft.Json.JsonConvert.SerializeObject(new
{
Name = Time,
Value = Date,
});
var request = WebRequest.CreateHttp("https://myfire-Example.firebaseio.com/.json");
request.Method = "PATCH";
request.ContentType = "application/json";
var buffer = Encoding.UTF8.GetBytes(json);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
var response = request.GetResponse();
json = (new StreamReader(response.GetResponseStream())).ReadToEnd();
}
}
}
}