I've managed to build a console application that successfully requests API data and converts it into a JSON object. However, I don't know how to put these DTO files (recently stored API objects) into my SQL server.
So far I have this for the console application:
class Program
{
static void Main(string[] args)
{
getRequestData();
}
public static void getRequestData()
{
var client = new RestClient(URL);
var request = new RestRequest();
var response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string rawResponse = response.Content;
AllRequests.Rootobject result = JsonConvert.DeserializeObject<AllRequests.Rootobject>(rawResponse);
}
}
}
As you can see, the console application program.cs file successfully obtains a get request then converts the API data into an object (A Json object I think). Here is the DTO file (AllRequests) that the data is being saved into.
class AllRequests
{
public class Rootobject
{
public Operation operation { get; set; }
}
public class Operation
{
public Result result { get; set; }
public Detail[] details { get; set; }
}
public class Result
{
public string message { get; set; }
public string status { get; set; }
}
public class Detail
{
public string requester { get; set; }
public string workorderid { get; set; }
public string accountname { get; set; }
}
}
When testing the application in debug mode, the objects are stored correctly and everything is fine. However, I don't know how to save these objects into an SQL database. I do already have an SQL server, however, I'm not sure how to push the already converted data and save it into a retrospective table. So for instance, saving this data into a Request table in my SQL database.
At the moment I only know how to make API calls in this console application. I can't find any data that will assist me with completing the second part of this task, which is storing the data into an SQL database.
Any help would be greatly appreciated, apologise if there is too much unnecessary information.
There are a few methods to make your application interact with a Database. You can setup your application to use Entity Framework (EF) to manage your DB. It is a powerfull and very popular ORM; there are a lot of resources in the web for help.
Personaly, I like to work with Dapper, that is very simple to use in comparison with EF. You just have to provide your connection-string to create your SqlConnection object.
You can find a simple tutorial in this link.