Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a generic list in "people" with 300 data,
How can I load the list into a table (INSERT INTO People (codPersona,tipoPersona,nombre,apPaterno,apMaterno,nombreCompleto) values (“ ”))
of my database "DBrarg" in SQL server or in ACCESS.
Help me!!



private void button1_Click(object sender, EventArgs e)
        {
            int codSchool = 7;
            List<AlumnosModel> people = new List<AlumnosModel>();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://rarg.org/Services/students.svc/People/"+codSchool);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream stream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(stream))
            {
                var json = reader.ReadToEnd();
                people = JsonConvert.DeserializeObject<List<AlumnosModel>>(json);
            }
            dataGridView1.DataSource = people;
        }


What I have tried:

private void button1_Click(object sender, EventArgs e)
        {
            int codSchool = 7;
            List<AlumnosModel> people = new List<AlumnosModel>();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://rarg.org/Services/students.svc/People/"+codSchool);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream stream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(stream))
            {
                var json = reader.ReadToEnd();
                people = JsonConvert.DeserializeObject<List<AlumnosModel>>(json);
            }
            dataGridView1.DataSource = people;
        }
Posted
Updated 22-Aug-17 8:03am

1 solution

Well, one way is to convert your List to a DataTable: Converting a List to a DataTable[^] and then use the SqlBulkCopy Class (System.Data.SqlClient)[^] to insert the data.
 
Share this answer
 
Comments
Rodrigo Alex Rodriguez 23-Aug-17 8:16am    
More specific, realize that I'm just starting to program,,, please
OriginalGriff 23-Aug-17 8:25am    
Follow the two links, and read what they say. Then do the two tasks separately.
First use the code from the first link to build your datatable from your List.
That should be trivial, if your AlumnosModel class is written properly.
Rodrigo Alex Rodriguez 24-Aug-17 8:44am    
I already solved it, how do I publish it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900