Inserting data within Firebase database through use of a C# application.
In this post we’ll be going through the inserting of data via a C# application to Googles firebase database. Before you start, ensure you have created a project within Firebase. We will be connecting to the Firebase database through use of web requests.
To begin with we will go through the header files being used in the project. Please also ensure you have installed the Newtonsoft.json which can be found by carrying out a search in the solution explorer.
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. In the below the two local variables are ‘Date’ which consists of YYYY/MM/DD and ‘Time’ which consists of HH:MM:SS.
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,
}
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. Firebase will store its data as part of a JSON tree, whereby each individual piece of data will be added as an additional node. Using the defaults of Firebase we assign our local variables ‘Date’ to ‘Name’ and ‘Time’ to ‘Value’ (example below).
Firebase Data: –
Name = 28/09/18
Value = 21:33:45
Once we have our relevant variables declared, converted and ready to be inserted 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 important to add “/.JSON” to the end of the URL. Within the web request the method will be set to POST, the word POST in this case is equivalent to INSERT for all who may accustomed to MySQL or MSSQL. The variables in JSON are then encoded and inserted in to the Firebase project.
var request = WebRequest.CreateHttp("https://myfire-Example.firebaseio.com/.json");
request.Method = "POST";
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 for those who may be interested in exploring for themselves.
using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace InsertingDataCsharpToFirebase
{
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 = "POST";
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();
}
}
}
}