0

I want to know how to use console application to store web api data into sql server database, Do anyone or experts know how to store the data into sql server database using console application to do that, please give me an example, big thanks.

My Code in console application:

HttpClient client = new HttpClient();

Console.WriteLine("Calling Api...");

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

var responseTask = client.GetAsync("http://localhost/api/api/Person");

responseTask.Wait();

if (responseTask.IsCompleted)
{
    var result = responseTask.Result;
    if (result.IsSuccessStatusCode)
    {
        var messageTask = result.Content.ReadAsStringAsync();
        messageTask.Wait();
        Console.WriteLine("Message From Api : " + messageTask.Result);
    }

    MessageBox.Show("Get Completed");
}

API Data:

<ArrayOfPerson xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/API.Models"><Person><ID>10</ID><PersonID>1</PersonID><Mobile></Mobile><Name>Chan King </Name><CreateDate>2019-11-03T00:23:00</CreateDate><ModifyDate>2021-11-06T16:04:00</ModifyDate>

How can i get the value and put into sql server database? Like 10 The 10 can put into the sql server database, and I have 10000+ records in the api

sr28
  • 4,728
  • 5
  • 36
  • 67
user19928919
  • 67
  • 1
  • 6
  • 1
    see: [SQL Insert Query Using C#](https://stackoverflow.com/questions/19956533/sql-insert-query-using-c-sharp) – Luuk Sep 15 '22 at 07:22
  • You would like to probably also parse your API response, for example [Read an XML (from a string)](https://stackoverflow.com/questions/8401280/read-a-xml-from-a-string-and-get-some-fields-problems-reading-xml) – T. Dominik Sep 15 '22 at 08:13
  • for a starter, try this https://www.c-sharpcorner.com/article/building-asp-net-web-api-in-net-core-with-entity-framework/ – onedevteam.com Sep 15 '22 at 10:10

1 Answers1

0

Try using ADO.NET SQl client or Entity framework

kittur_riyaz
  • 116
  • 5