1

How to use console application to do API data import into SQL Server database? I want to know how to import API data as xml into SQL Server database. My API has more than 10000+ records and I want to import all of them into a SQL Server database or import as xml string. I am using console application to write the code.

This is the code for my console application:

class Program
{
    static void Main(string[] args)
    {
        HttpClient client = new HttpClient();
        Console.WriteLine("Calling Api...");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

        var responseTask = client.GetAsync("http://localhost/webapi/api/user");
        Person person = null;
        responseTask.Wait();

        if (responseTask.IsCompleted)
        {
            var result = responseTask.Result;

            if (result.IsSuccessStatusCode)
            {
                var messageTask = result.Content.ReadAsAsync<Person[]>();
                messageTask.Wait();
                
                Console.WriteLine("Message From Api : " + messageTask.Result);

                using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
                {
                    con.Open();

                    using (con)
                    {
                        string sqlquery = "INSERT INTO tblPerson (ID, Name, Mobile, Birthday) VALUES (@id, @name, @mobile, @birthday)";

                        using (SqlCommand cmd = new SqlCommand(sqlquery, con))
                        {
                            cmd.Parameters.AddWithValue("@id", person.ID);
                            cmd.Parameters.AddWithValue("@name", person.Name);
                            cmd.Parameters.AddWithValue("@mobile", person.Mobile);
                            cmd.Parameters.AddWithValue("@birthday", person.Birthday);
                            
                            cmd.ExecuteNonQuery();
                        }
                        con.Close();
                    }
                }
            }
        }
    }
}

Class:

public class Person
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Mobile { get; set; }
    public DateTime Birthday { get; set; }
}

My code cannot store API data into SQL Server database, I don't know why

4
  • 3
    Please Describe the Errors/Exceptions that are occuring Commented Sep 16, 2022 at 7:29
  • so i need to import api existing data to sql server database through console application, this is what i want to do all the time Commented Sep 16, 2022 at 7:48
  • .Result can cause a deadlock (although in a console app that is unlikely). Instead use await. What's the second using (con) for? Commented Sep 16, 2022 at 8:20
  • using (con) is for connection to the database which is for inserting api data Commented Sep 16, 2022 at 8:34

1 Answer 1

4

Looking at your SQL query, you specify 4 columns with the following parameter names;

  • @ID
  • @Name
  • @Mobile
  • @Birthday

But in your parameters you have specified none of these, but instead a totally different parameter name.

cmd.Parameters.AddWithValue("@premisesid", person.ID);
cmd.Parameters.AddWithValue("@premisesid", person.Name);
cmd.Parameters.AddWithValue("@premisesid", person.Mobile);
cmd.Parameters.AddWithValue("@premisesid", person.Birthday);
                                

So you need to change these to use the correct parameter names

cmd.Parameters.AddWithValue("@ID", person.ID);
cmd.Parameters.AddWithValue("@Name", person.Name);
cmd.Parameters.AddWithValue("@Mobile", person.Mobile);
cmd.Parameters.AddWithValue("@Birthday", person.Birthday);
Sign up to request clarification or add additional context in comments.

2 Comments

Good catch! :-)
AddWithValue is Evil instead specify data types and lengths explicitly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.