//排序和Button按钮事件 private void dgv1_CellContentClick(object sender, DataGridViewCellEventArgs e) { //注释: if (dgv1.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex > -1) { wanggong_goods og = (wanggong_goods)dgv1.CurrentRow.DataBoundItem; if (og != null) { //IList<wanggong_goods> list = wanggong_goodsManager.Search(1, 99999, "", new DateTime(), new DateTime(), ""); //dgv1.DataSource = Manager.BindingSort_wanggong_goods(list); Manager.OpenProgram(og.wgurl); } } else if (e.RowIndex == -1) { try { dgv1.Sort(dgv1.Columns[e.ColumnIndex], ListSortDirection.Descending); } catch (Exception ex) { Debug.WriteLine("排序出错=========================" + ex.Message); } } }
/// <summary> /// 根据店铺商品搜索页面获取商品基本信息 /// </summary> /// <param name="DateCaiJi">采集时间</param> /// <param name="webBrowser1">浏览器</param> /// <returns>List<Entity.wanggong_goods></returns> List <Entity.wanggong_goods> SearchPage_GetWangGongGoods(DateTime DateCaiJi, WebBrowser webBrowser1) { List <wanggong_goods> list = new List <wanggong_goods>(); Browser.jsstr = " getClassName('photo').length; "; string res = Browser.JS_WebBrowser(webBrowser1); Debug.WriteLine("本页面商品数量为:" + res);//输出 int list_count1 = 0; //如果没有商品,则返回空 if (!int.TryParse(res, out list_count1)) { Debug.WriteLine("没有获取到商品");//输出 return(list); } else { //遍历每个商品 for (int i = 0; i < list_count1; i++) { //进度条+1 toolStripProgressBar1.PerformStep(); wanggong_goods wg = new wanggong_goods(); string tbid = CaiJi.SearchPage_GetTBID(i, webBrowser1); IList <wanggong_goods> list_temp = BLL2.wanggong_goodsManager.Search(1, 10, tbid, 0, new DateTime(), new DateTime(), ""); if (list_temp.Count > 0) { wg = list_temp[0]; } wg.wgcollectdate = DateCaiJi; wg.wgimg = CaiJi.SearchPage_GetImgUrl(i, webBrowser1); wg.wgprice2 = CaiJi.SearchPage_GetPrice(i, webBrowser1); wg.wgtittle = CaiJi.SearchPage_GetTitle(i, webBrowser1); wg.wgurl = CaiJi.SearchPage_GetGoodsUrl(i, webBrowser1); wg.wgxiaoliang = CaiJi.SearchPage_GetSales(i, webBrowser1); wg.wgTBid = CaiJi.SearchPage_GetTBID(i, webBrowser1); //下载图片 string imgurl = CaiJi.GetImgSizeByUrl(wg.wgimg, 150); string imgname = !string.IsNullOrEmpty(wg.wgremark) ? wg.wgremark : DateTime.Now.ToString("yyyyMMddhhmmss") + Manager.RandomNumber(1000, 99999).ToString() + ".jpg"; Manager.DownloadFile(imgurl, Manager.imgsite_wanggong, imgname, 1000); wg.wgremark = imgname; list.Add(wg); dgv1.DataSource = Manager.BindingSort_wanggong_goods(list); } } return(list); }
/// <summary> /// 查询全部数据 /// </summary> /// <returns>IList</returns> /*查看是否为视图*/ public IList <wanggong_goods> SearchAll() { DBHelper.sqlstr = "select * from wanggong_goods "; List <wanggong_goods> list = new List <wanggong_goods>(); MySqlDataReader reader = DBHelper.ExecuteReader(); while (reader.Read()) { wanggong_goods Obj = GetByReader(reader); list.Add(Obj); } reader.Close(); return(list); }
/// <summary> /// 根据wgid,查询一条数据 /// </summary> /// <param name="wgid">编号</param> /// <returns></returns> /*查看是否为视图*/ public wanggong_goods SearchByID(int wgid) { DBHelper.sqlstr = "select * from wanggong_goods where wgid = @wgid"; MySqlParameter[] param = new MySqlParameter[] { new MySqlParameter("@wgid", wgid) }; MySqlDataReader reader = DBHelper.ExecuteReader(param); wanggong_goods Obj = null; if (reader.Read()) { Obj = GetByReader(reader); } reader.Close(); return(Obj); }
/// <summary> /// 从一个MySqlDataReader里读数据 /// </summary> /// <param name="Reader">MySqlDataReader</param> /// <returns>wanggong_goodsExample</returns> public static wanggong_goods GetByReader(MySqlDataReader Reader) { wanggong_goods wanggong_goodsExample = new wanggong_goods(); wanggong_goodsExample.wgid = Reader["wgid"] == DBNull.Value ? 0 : (int)Reader["wgid"]; wanggong_goodsExample.wid = Reader["wid"] == DBNull.Value ? 0 : (int)Reader["wid"]; wanggong_goodsExample.wgcollectdate = Reader["wgcollectdate"] == DBNull.Value ? new DateTime() : Convert.ToDateTime(Reader["wgcollectdate"]); wanggong_goodsExample.wgTBid = Reader["wgTBid"] == DBNull.Value ? null : Reader["wgTBid"].ToString(); wanggong_goodsExample.wgurl = Reader["wgurl"] == DBNull.Value ? null : Reader["wgurl"].ToString(); wanggong_goodsExample.wgtittle = Reader["wgtittle"] == DBNull.Value ? null : Reader["wgtittle"].ToString(); wanggong_goodsExample.wgprice1 = Reader["wgprice1"] == DBNull.Value ? null : Reader["wgprice1"].ToString(); wanggong_goodsExample.wgprice2 = Reader["wgprice2"] == DBNull.Value ? null : Reader["wgprice2"].ToString(); wanggong_goodsExample.wgimg = Reader["wgimg"] == DBNull.Value ? null : Reader["wgimg"].ToString(); wanggong_goodsExample.wgxiaoliang = Reader["wgxiaoliang"] == DBNull.Value ? null : Reader["wgxiaoliang"].ToString(); wanggong_goodsExample.wgUPdate = Reader["wgUPdate"] == DBNull.Value ? null : Reader["wgUPdate"].ToString(); wanggong_goodsExample.wgDowndate = Reader["wgDowndate"] == DBNull.Value ? null : Reader["wgDowndate"].ToString(); wanggong_goodsExample.wgremark = Reader["wgremark"] == DBNull.Value ? null : Reader["wgremark"].ToString(); wanggong_goodsExample.wgremark1 = Reader["wgremark1"] == DBNull.Value ? null : Reader["wgremark1"].ToString(); wanggong_goodsExample.wgremark2 = Reader["wgremark2"] == DBNull.Value ? null : Reader["wgremark2"].ToString(); wanggong_goodsExample.wgremark3 = Reader["wgremark3"] == DBNull.Value ? null : Reader["wgremark3"].ToString(); return(wanggong_goodsExample); }
/// <summary> /// 模糊搜索 /// </summary> /// <param name="key">关键词</param> /// <param name="state">状态</param> /// <param name="id">int字段</param> /// <param name="startdate">起始时间</param> /// <param name="enddate">结束时间</param> /// <param name="orderby">排序</param> /// <returns>IList<wanggong_goods></returns> public IList <wanggong_goods> Search(int s, int e, string key, int wid, DateTime startdate, DateTime enddate, string orderby) { string sql1 = "select top " + (e - s + 1).ToString() + " * from wanggong_goods where "; string sql2 = string.IsNullOrEmpty(key) ? " 1=1 " : " ( wgTBid like '%" + key + "%' or wgurl like '%" + key + "%' or wgtittle like '%" + key + "%' or wgprice1 like '%" + key + "%' or wgprice2 like '%" + key + "%' or wgimg like '%" + key + "%' or wgxiaoliang like '%" + key + "%' or wgUPdate like '%" + key + "%' or wgDowndate like '%" + key + "%' or wgremark like '%" + key + "%' or wgremark1 like '%" + key + "%' or wgremark2 like '%" + key + "%' or wgremark3 like '%" + key + "%' )"; //删除无用字段,删除最后一个or string sql3 = wid == 0 ? "" : " and wid=" + wid.ToString() + " "; string sql5 = startdate == new DateTime() ? "" : " and DATEDIFF(D,'" + startdate.ToString("yyyy-MM-dd") + "',wgcollectdate)>=0 "; //时间字段,无用删除 string sql6 = enddate == new DateTime() ? "" : " and DATEDIFF(D,'" + enddate.ToString("yyyy-MM-dd") + "',wgcollectdate)<=0 "; //时间字段,无用删除 string sql7 = string.IsNullOrEmpty(orderby) ? " order by wgcollectdate desc " : " order by " + orderby; string sql8 = s == 1 ? "" : " and wgid not in ( select top " + (s - 1).ToString() + " wgid from wanggong_goods where " + sql2 + sql3 + sql5 + sql6 + sql7 + " ) "; DBHelper.sqlstr = sql1 + sql2 + sql3 + sql5 + sql6 + sql8 + sql7; List <wanggong_goods> list = new List <wanggong_goods>(); SqlDataReader reader = DBHelper.ExecuteReader(); while (reader.Read()) { wanggong_goods Obj = GetByReader(reader); list.Add(Obj); } reader.Close(); return(list); }
/// <summary> /// 插入方法 /// </summary> /// <param name="wanggong_goods">wanggong_goods表实例</param> /// <returns>int</returns> public static int Insert(wanggong_goods wanggong_goodsExample) { int temp = SearchNum(wanggong_goodsExample.wgTBid, 0, new DateTime(), new DateTime()); if (temp == 0) { return(Service.Insert(wanggong_goodsExample)); } else { IList <wanggong_goods> list = Search(1, 99, wanggong_goodsExample.wgTBid, 0, new DateTime(), new DateTime(), ""); wanggong_goods gg = list[0]; gg.wgcollectdate = wanggong_goodsExample.wgcollectdate; gg.wgimg = wanggong_goodsExample.wgimg; gg.wgprice2 = wanggong_goodsExample.wgprice2; gg.wgremark = wanggong_goodsExample.wgremark; gg.wgTBid = wanggong_goodsExample.wgTBid; gg.wgtittle = wanggong_goodsExample.wgtittle; gg.wgurl = wanggong_goodsExample.wgurl; gg.wgxiaoliang = wanggong_goodsExample.wgxiaoliang; return(Service.Update(gg)); } }
/// <summary> /// 数据修改 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgv1_CellParsing(object sender, DataGridViewCellParsingEventArgs e) { try { if (dgv1.CurrentCell.ColumnIndex == 4 || dgv1.CurrentCell.ColumnIndex == 6) { wanggong_goods og = (wanggong_goods)dgv1.CurrentRow.DataBoundItem; if (og != null) { //状态 toolStripProgressBar1.Maximum = 4; //状态 toolStripProgressBar1.Value = 1; //IList<wanggong_goods> list = wanggong_goodsManager.Search(1, 99999, "", new DateTime(), new DateTime(), ""); //状态 toolStripProgressBar1.Value = 2; string remark1 = dgv1.Rows[e.RowIndex].Cells[6].EditedFormattedValue == null ? "" : dgv1.Rows[e.RowIndex].Cells[6].EditedFormattedValue.ToString(); string remark2 = dgv1.Rows[e.RowIndex].Cells[4].EditedFormattedValue == null ? "" : dgv1.Rows[e.RowIndex].Cells[4].EditedFormattedValue.ToString(); og.wgremark1 = remark1; og.wgremark2 = remark2; //状态 toolStripProgressBar1.Value = 3; BLL2.wanggong_goodsManager.Update(og); //状态 toolStripProgressBar1.Value = 4; } } } catch (Exception ex) { Debug.WriteLine("修改信息失败 " + ex.Message); } }
/// <summary> /// 更新 /// </summary> /// <param name="wanggong_goods">wanggong_goods表实例</param> /// <returns>int</returns> public int Update(wanggong_goods wanggong_goodsExample) { DBHelper.sqlstr = "update wanggong_goods set wid=@wid,wgcollectdate=@wgcollectdate,wgTBid=@wgTBid,wgurl=@wgurl,wgtittle=@wgtittle,wgprice1=@wgprice1,wgprice2=@wgprice2,wgimg=@wgimg,wgxiaoliang=@wgxiaoliang,wgUPdate=@wgUPdate,wgDowndate=@wgDowndate,wgremark=@wgremark,wgremark1=@wgremark1,wgremark2=@wgremark2,wgremark3=@wgremark3 where wgid=" + wanggong_goodsExample.wgid; return(DBHelper.ExecuteNonQuery(GetSqlParameters(wanggong_goodsExample))); }
/// <summary> /// 插入方法 /// </summary> /// <param name="wanggong_goods">wanggong_goods表实例</param> /// <returns>int</returns> public int Insert(wanggong_goods wanggong_goodsExample) { DBHelper.sqlstr = "insert into wanggong_goods (wid,wgcollectdate,wgTBid,wgurl,wgtittle,wgprice1,wgprice2,wgimg,wgxiaoliang,wgUPdate,wgDowndate,wgremark,wgremark1,wgremark2,wgremark3)values(@wid,@wgcollectdate,@wgTBid,@wgurl,@wgtittle,@wgprice1,@wgprice2,@wgimg,@wgxiaoliang,@wgUPdate,@wgDowndate,@wgremark,@wgremark1,@wgremark2,@wgremark3)"; return(DBHelper.ExecuteNonQuery(GetSqlParameters(wanggong_goodsExample))); }
/// <summary> /// 根据表,获取一个MySqlParameter数组 /// </summary> /// <returns>MySqlParameter[]</returns> public static MySqlParameter[] GetSqlParameters(wanggong_goods wanggong_goodsExample) { List <MySqlParameter> list_param = new List <MySqlParameter>(); if (wanggong_goodsExample.wid != 0) { list_param.Add(new MySqlParameter("@wid", wanggong_goodsExample.wid)); } else { list_param.Add(new MySqlParameter("@wid", DBNull.Value)); } if (wanggong_goodsExample.wgcollectdate != new DateTime() && wanggong_goodsExample.wgcollectdate != null) { list_param.Add(new MySqlParameter("@wgcollectdate", wanggong_goodsExample.wgcollectdate.ToString("yyyy-MM-dd"))); } else { list_param.Add(new MySqlParameter("@wgcollectdate", DBNull.Value)); } if (!string.IsNullOrEmpty(wanggong_goodsExample.wgTBid)) { list_param.Add(new MySqlParameter("@wgTBid", wanggong_goodsExample.wgTBid)); } else { list_param.Add(new MySqlParameter("@wgTBid", DBNull.Value)); } if (!string.IsNullOrEmpty(wanggong_goodsExample.wgurl)) { list_param.Add(new MySqlParameter("@wgurl", wanggong_goodsExample.wgurl)); } else { list_param.Add(new MySqlParameter("@wgurl", DBNull.Value)); } if (!string.IsNullOrEmpty(wanggong_goodsExample.wgtittle)) { list_param.Add(new MySqlParameter("@wgtittle", wanggong_goodsExample.wgtittle)); } else { list_param.Add(new MySqlParameter("@wgtittle", DBNull.Value)); } if (!string.IsNullOrEmpty(wanggong_goodsExample.wgprice1)) { list_param.Add(new MySqlParameter("@wgprice1", wanggong_goodsExample.wgprice1)); } else { list_param.Add(new MySqlParameter("@wgprice1", DBNull.Value)); } if (!string.IsNullOrEmpty(wanggong_goodsExample.wgprice2)) { list_param.Add(new MySqlParameter("@wgprice2", wanggong_goodsExample.wgprice2)); } else { list_param.Add(new MySqlParameter("@wgprice2", DBNull.Value)); } if (!string.IsNullOrEmpty(wanggong_goodsExample.wgimg)) { list_param.Add(new MySqlParameter("@wgimg", wanggong_goodsExample.wgimg)); } else { list_param.Add(new MySqlParameter("@wgimg", DBNull.Value)); } if (!string.IsNullOrEmpty(wanggong_goodsExample.wgxiaoliang)) { list_param.Add(new MySqlParameter("@wgxiaoliang", wanggong_goodsExample.wgxiaoliang)); } else { list_param.Add(new MySqlParameter("@wgxiaoliang", DBNull.Value)); } if (!string.IsNullOrEmpty(wanggong_goodsExample.wgUPdate)) { list_param.Add(new MySqlParameter("@wgUPdate", wanggong_goodsExample.wgUPdate)); } else { list_param.Add(new MySqlParameter("@wgUPdate", DBNull.Value)); } if (!string.IsNullOrEmpty(wanggong_goodsExample.wgDowndate)) { list_param.Add(new MySqlParameter("@wgDowndate", wanggong_goodsExample.wgDowndate)); } else { list_param.Add(new MySqlParameter("@wgDowndate", DBNull.Value)); } if (!string.IsNullOrEmpty(wanggong_goodsExample.wgremark)) { list_param.Add(new MySqlParameter("@wgremark", wanggong_goodsExample.wgremark)); } else { list_param.Add(new MySqlParameter("@wgremark", DBNull.Value)); } if (!string.IsNullOrEmpty(wanggong_goodsExample.wgremark1)) { list_param.Add(new MySqlParameter("@wgremark1", wanggong_goodsExample.wgremark1)); } else { list_param.Add(new MySqlParameter("@wgremark1", DBNull.Value)); } if (!string.IsNullOrEmpty(wanggong_goodsExample.wgremark2)) { list_param.Add(new MySqlParameter("@wgremark2", wanggong_goodsExample.wgremark2)); } else { list_param.Add(new MySqlParameter("@wgremark2", DBNull.Value)); } if (!string.IsNullOrEmpty(wanggong_goodsExample.wgremark3)) { list_param.Add(new MySqlParameter("@wgremark3", wanggong_goodsExample.wgremark3)); } else { list_param.Add(new MySqlParameter("@wgremark3", DBNull.Value)); } MySqlParameter[] param = new MySqlParameter[list_param.Count]; int index = 0; foreach (MySqlParameter p in list_param) { param[index] = p; index++; } return(param); }
/// <summary> /// 更新 /// </summary> /// <param name="wanggong_goods">wanggong_goods表实例</param> /// <returns>int</returns> public static int Update(wanggong_goods wanggong_goodsExample) { return(Service.Update(wanggong_goodsExample)); }
/// <summary> /// 采集网供数据 /// </summary> void CaiJiWG() { GC.Collect(); dgv1.Rows.Clear(); //设置采集时间 DateCaiJi = DateTime.Now; //设置店铺ID int dpid = 0; string str = cb_dianpu.Text.Trim(); string temp_shopid = cb_dianpu.SelectedValue == null ? "" : cb_dianpu.SelectedValue.ToString(); //判断是否是网址 int temp_isurl = str.IndexOf("http"); //string id = Manager.GetValueByURL(str, "id"); if (temp_isurl > -1 || str == "请选择") { MessageBox.Show("请输入正确的旺旺名称或者店铺名称"); return; } //如果是选择下拉菜单中的店铺 if (temp_shopid != "0") { if (int.TryParse(temp_shopid, out dpid)) { try { wanggong_dianpu temp_dp = BLL2.wanggong_dianpuManager.SearchByID(dpid); //wanggong_dianpu temp_dp = BLL2.wanggong_dianpuManager.Search(1, 10, temp_shopid, "", new DateTime(), new DateTime(), "")[0]; str = temp_dp.wdwangwang; } catch { } } } string pagenum = txt_page.Text.Trim(); int page_temp = 0; if (!int.TryParse(pagenum, out page_temp)) { MessageBox.Show("请输入正确的采集页数"); return; } //设置进度条 toolStripProgressBar1.Maximum = page_temp * 24; toolStripProgressBar1.Value = 0; //根据旺旺查询店铺ID string url = CaiJi.GetShopSearchURL(str); //url = Browser.UrlEncode(url, Encoding.UTF8); //Debug.WriteLine(url); //跳转到搜索店铺页面 Browser.urlstr = url; webBrowser1.Navigate(Browser.urlstr); //判断是否加载完成 if (Browser.WaitWebPageLoad(webBrowser1)) { try { if (webBrowser1.Document != null) { string url_shop = CaiJi.GetShopUrlByWangWang(str, webBrowser1); //如果没有找到店铺网址,或者没有开店信息 if (string.IsNullOrEmpty(url_shop)) { Debug.WriteLine("采集店铺时,没有找到店铺网址,或者没有开店信息"); MessageBox.Show("没有采集到该店铺信息"); return; } List <string> list_url = new List <string>(); List <wanggong_goods> list_goods = new List <wanggong_goods>(); //默认查询20页 for (int i = 0; i < page_temp; i++) { string url_goodslist = CaiJi.GetShopGoodsListURL_NewOn(url_shop, i + 1); Browser.urlstr = url_goodslist; //跳转到商品列表页面 webBrowser1.Navigate(Browser.urlstr); //判断是否加载完成 if (Browser.WaitWebPageLoad(webBrowser1)) { if (webBrowser1.Document != null) { //设置店铺ID if (dpid == 0) { //在本页获取店铺信息 wanggong_dianpu dp = CaiJi.SearchPage_DianPu(DateCaiJi, webBrowser1); BLL2.wanggong_dianpuManager.Insert(dp); IList <wanggong_dianpu> list_dianpu = BLL2.wanggong_dianpuManager.Search(1, 1, dp.wdTBID, "", DateCaiJi, DateCaiJi, ""); if (list_dianpu.Count > 0) { dpid = list_dianpu[0].wid; } else { MessageBox.Show("采集失败,获取店铺信息失败"); return; } } //判断是否还有商品 Browser.jsstr = " getClassName('no-result-new').length; "; if (Browser.JS_WebBrowser(webBrowser1) == "1") { break; } else { //开始采集商品 //List<Entity.wanggong_goods> list_wg = SearchPage_GetWangGongGoods(DateCaiJi, webBrowser1); #region //开始采集 //List<wanggong_goods> list_wg = new List<wanggong_goods>(); Browser.jsstr = " getClassName('photo').length; "; string res = Browser.JS_WebBrowser(webBrowser1); Debug.WriteLine("本页面商品数量为:" + res);//输出 int list_count1 = 0; //如果没有商品,则返回空 if (!int.TryParse(res, out list_count1)) { Debug.WriteLine("没有获取到商品");//输出 break; } else { //遍历每个商品 for (int j = 0; j < list_count1; j++) { //进度条+1 toolStripProgressBar1.PerformStep(); wanggong_goods wg = new wanggong_goods(); string tbid = CaiJi.SearchPage_GetTBID(j, webBrowser1); IList <wanggong_goods> list_temp = BLL2.wanggong_goodsManager.Search(1, 10, tbid, 0, new DateTime(), new DateTime(), ""); if (list_temp.Count > 0) { wg = list_temp[0]; } wg.wgcollectdate = DateCaiJi; wg.wgimg = CaiJi.SearchPage_GetImgUrl(j, webBrowser1); wg.wgprice2 = CaiJi.SearchPage_GetPrice(j, webBrowser1); wg.wgtittle = CaiJi.SearchPage_GetTitle(j, webBrowser1); wg.wgurl = CaiJi.SearchPage_GetGoodsUrl(j, webBrowser1); wg.wgxiaoliang = CaiJi.SearchPage_GetSales(j, webBrowser1); wg.wgTBid = CaiJi.SearchPage_GetTBID(j, webBrowser1); //下载图片 string imgurl = CaiJi.GetImgSizeByUrl(wg.wgimg, 150); string imgname = !string.IsNullOrEmpty(wg.wgremark) ? wg.wgremark : DateTime.Now.ToString("yyyyMMddhhmmss") + Manager.RandomNumber(1000, 99999).ToString() + ".jpg"; Manager.DownloadFile(imgurl, Manager.imgsite_wanggong, imgname, 1000); wg.wgremark = imgname; list_goods.Add(wg); dgv1.DataSource = Manager.BindingSort_wanggong_goods(list_goods); } } #endregion } } } } foreach (wanggong_goods wg in list_goods) { wg.wid = dpid; //把每个采集到的商品添加到数据库 BLL2.wanggong_goodsManager.Insert(wg); //dgv1.DataSource = Manager.BindingSort_wanggong_goods(list_goods); } dgv1.DataSource = Manager.BindingSort_wanggong_goods(list_goods); GC.Collect();//释放内存 } } catch (Exception ex) { Debug.WriteLine("======================================================================"); Debug.WriteLine("网址:" + Browser.urlstr); Debug.WriteLine(DateTime.Now.ToString()); Debug.WriteLine("全店采集信息失败:" + ex.Message); MessageBox.Show("全店采集信息失败:" + ex.Message); } } toolStripProgressBar1.Value = page_temp * 24; bind_dianpu(); }