日期:2014-05-18 浏览次数:20918 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressions; using System.Data.SqlClient; using System.Net; using System.IO; namespace GetQQData { public partial class Form1 : Form { public Form1() { InitializeComponent(); tb_WebAddress.Text = "地址,自己替换"; } //连接数据库字符串 private static string strConn = "server=.;database=QQData;uid=xxx;password=xxx"; private void btn_SaveAs_Click(object sender, EventArgs e) { string QQnum = ""; int result = 0; //查询是否存在 int count= 0; string HtmlStr = tb_HtmlContent.Text; StringBuilder sb = null; MatchCollection mc = GetValues(HtmlStr, "<li>", "</li>"); for (int i = 0; i < mc.Count; i++) { try { string tmpHtml = string.Empty; tmpHtml = mc[i].Value; QQnum = GetValue(tmpHtml, "qq=", "\"><img"); count = SelectQQ(QQnum); if (count == 0) { sb = new StringBuilder(); sb.Append("insert into QQ(qqnum,addtime) values ('"); //sb.Append("QQnum"); //sb.Append("','"); sb.Append(QQnum); sb.Append("','"+DateTime.Now+"')"); result += ExecuteNonQuery(sb.ToString()); } } catch { } } lbl_Over.Text = "保存了"+result+"个QQ"; } //查询是否已经存在 public int SelectQQ(string qqnum) { int result=0; string strSql = "select count(*) from QQ where qqnum='" + qqnum + "'"; using (SqlConnection conn = new SqlConnection(strConn)) { using (SqlCommand cmd = new SqlCommand(strSql, conn)) { try { conn.Open(); result = Convert.ToInt32(cmd.ExecuteScalar()); } catch { result = 1; } finally { cmd.Dispose(); conn.Close(); } } } return result; } /// <summary> /// 正则全文匹配开始标签与结束标签之间的值,并放回数组 /// </summary> /// <remarks> /// 方法名称:GetValues /// 创建人: /// 创建时间:2011-09-06 /// Email:vochely@hotmail.com /// </remarks> /// <param name="strHtml">需要匹配的字符串</param> /// <param name="strStart">开始标签</param> /// <param name="strEnd">结束标签</param> /// <returns>返回开始标签与结束标签之间的值</returns> public static MatchCollection GetValues(string strHtml, string strStart, string strEnd) { MatchCollection result = null; try { Regex rg = new Regex("(?<=(" + strStart + "))[.\\s\\S]*?(?=(" +