如何使用C#.NET从foreach循环元素向sql server数据库插入数据? [英] How to insert data into sql server database from the foreach loop elements using C#.NET?

查看:451
本文介绍了如何使用C#.NET从foreach循环元素向sql server数据库插入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网页浏览器控件中从网页中提取了元素。我使用foreach和嵌套循环循环来匹配每个元素。现在我想根据页面上的记录逐个将每个值保存到数据库中。但每次只保存最后一次记录数据多次。请帮忙。



我的代码是:

I have fetched the elements from the web page in a web browser control. I am using foreach and nested loops loop to match each element. Now I want to save each value into database one by one according to records on page. But every time it is saving only last record data multiple times. Please help out.

My code is:

  dynamic div_classes = webBrowser1.Document.Body.GetElementsByTagName("div");
 foreach (HtmlElement d in div_classes)
            {
            dynamic d1 = webBrowser1.Document.Body.GetElementsByTagName("td");
             foreach (HtmlElement td in d1)
                {
                    if (td.GetAttribute("className") == "personName")
                    {
                        personName = td.InnerText;
                        //txtName.Text = personName;
                    }
                }
                 if (d.GetAttribute("id") == "title")
                {
                    persontitle = d.InnerText;
                 
                }
               // multiple values
            }
}





我应该在哪里调用我的函数Save_to_DB()以在循环中将所有值插入数据库?





提前谢谢。



Where should I call my function "Save_to_DB()" to insert all values to database in a loop?


Thanks in advance.

推荐答案

这应该是显而易见的但是你将需要保存到foreach循环内的数据库,以便将每条记录保存到数据库。
This should be obvious but you'll need to save to the database inside your foreach loop so that each record gets saved to the database.


准确地说,你应该这样做



To be precise you should do following

dynamic div_classes = webBrowser1.Document.Body.GetElementsByTagName("div");
 foreach (HtmlElement d in div_classes)
            {
            dynamic d1 = webBrowser1.Document.Body.GetElementsByTagName("td");
             foreach (HtmlElement td in d1)
                {
                    if (td.GetAttribute("className") == "personName")
                    {
                        personName = td.InnerText;
                        //txtName.Text = personName;
                    }
                }
                 if (d.GetAttribute("id") == "title")
                {
                    persontitle = d.InnerText;
                 
                }
               Save_to_DB();
            }
}


感谢您的回答。



我已经把函数插入到同一个地方的数据库中,但我陷入了一个非常奇怪的事情。



每次插入最后的数据页面记录而不是显示每条记录。

多个数据但重复记录的最后一个记录。
Thanks for your answers.

I have already place the function to insert elements to the database at the same place but I am stuck at a very strange thing.

Every time it is inserting the data of last record of the page instead of showing each record.
Multiple data but of last record in repitition.


这篇关于如何使用C#.NET从foreach循环元素向sql server数据库插入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆