现在几万条数据要插入到数据库,想做一个多线程,向大家求救。。

xr396464010 2009-12-31 03:22:39
下面这个循环读文件可以获取1万多个页面,现在我要把这些页面的内容全部抓取的数据全部插入到数据库中,
这些已经OK 了, 就是速度太慢要好几个小时而且还说时间超长,结果报错,
希望大家能够帮我做个多线程。。。 谢谢大家。。

#region 读取文件信息
public void ReadFile()
{
string path = "D:\\Product";
string showUrl = string.Empty;
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] filei = dir.GetFiles("*." + "html");
foreach (FileInfo fr in filei)
{
PRODUCT product = new PRODUCT();
//获取文件里面的URL地址
showUrl = "http://www.360buy.com/product/" + fr.Name;
//通过URL地址抓取整个页面
string html = GetPageInfo(showUrl);
//对整个页面进行部分的截取
int firstIndex =html.IndexOf("<div class=\"clear\">");
int lastIndex = html.IndexOf("<div id=\"suit\"></div>");
string myHtml = html.Substring(firstIndex, lastIndex);
//对市场价进行转换
double marchePriceInt = ConvertToint(SplitPrice(myHtml, this.txtFirstPrice.Text, this.txtLastPrice.Text, this.ckMarketPrice));
//获取导航栏的集合添加到实体对象中...
Dictionary<string, string> dis = NavigateTitle(myHtml);
product.Product_DefaultPage = dis["one"];
product.Product_IndexPage = dis["two"];
product.Product_List = dis["three"];
product.Product_Type = dis["four"];


//将获取到的数据封装在实体对象中
product.Product_ID = SplitProductNo(myHtml, this.txtFirstNo.Text, this.txtLastNo.Text, this.ckNO);
product.Product_Name = SplitProductTitle(myHtml, this.txtFirstTitle.Text, this.txtLastTitle.Text, this.ckTitle);
product.WebPrice = SplitJingDong(myHtml, this.txtFirstJingD.Text, this.txtLastJingD.Text, this.ckWebPrice);
product.MarketPrice = marchePriceInt;
product.ProductProperty = SplitProperty(myHtml, this.txtFirstProductProperty.Text, this.txtLastProductProperty.Text, this.ckProperty);
product.Product_Detail = SplitDetailed(myHtml, this.txtFirstDetail.Text, this.txtLastDetail.Text, this.ckDetail);
product.Parameter = SplitParameter(myHtml, this.txtFirstParam.Text, this.txtLastParam.Text, this.ckParameter);
product.ProductBox = SplitBox(myHtml, this.txtFirstBox.Text, this.txtLastBox.Text, this.ckBox);
product.ProductService = SplitService(myHtml, this.txtFirstService.Text, this.txtLastService.Text, this.ckService);
product.PicTitle = product.Product_Name;
product.PicPath = SplitPicPath(myHtml, this.txtFirstPicPath.Text, this.txtLastPicPath.Text, this.ckPicPath);
try
{
//将匹配出来的完整结果插入到数据库中去
ACCESS access = new ACCESS();
access.InsertProduct(product);
}
catch
{
Response.Write("<script>alert('插入数据时出错拉!!');</script>");
}
}
}
...全文
437 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xr396464010 2009-12-31
  • 打赏
  • 举报
回复
大家不要误解
ACCESS access = new ACCESS(); 这是我定义的一个类,而不是ACCESS数据库,
我现在用的就是SQL SERVER 2005 数据库呀!!!!!
xr396464010 2009-12-31
  • 打赏
  • 举报
回复
是呀我有一万多条数据插入的时候老是报错,希望大家帮忙解决下呀!!!
周公 2009-12-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fut20090715 的回复:]
没研究过access数据库的存取速度问题
建议楼主做以下尝试:
1.不存数据库,只程序处理要多久
  如果时间长的话就考虑改进String的处理算法
2.换其他数据库,
  建议采用SQL Server,速度可能会有改观,之后再转换为access,转换的速度很快
[/Quote]
非常同意这种说法。
不要以为使用了多线程就能快,不正确处理会更慢,比如你这种情况。
fut20090715 2009-12-31
  • 打赏
  • 举报
回复
看楼主的意思应该是一个爬虫爬回来的结果处理
考虑分块读取、入库?
fut20090715 2009-12-31
  • 打赏
  • 举报
回复
没研究过access数据库的存取速度问题
建议楼主做以下尝试:
1.不存数据库,只程序处理要多久
如果时间长的话就考虑改进String的处理算法
2.换其他数据库,
建议采用SQL Server,速度可能会有改观,之后再转换为access,转换的速度很快
ztenv 2009-12-31
  • 打赏
  • 举报
回复
哥们,多线程更慢。。。。。多线程需要时间片的切换,降低了效率,

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧