private void OnGetdata(object obj) { Task.Factory.StartNew(() => { try { Enable = false; //Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => //{ //})); string tableName = Name; if (DbHelperSqLite.Exists("select count(*) from sqlite_master where type='table' and name='BaseDataReceived_" + tableName + "'")) { if (DbHelperSqLite.Exists("select count(*) from BaseDataReceived_" + tableName + " where status = 1")) { Log.WriteLog("股票【" + tableName + "】的数据已获取,不必重新获取"); return; } } string url = "http://www.aigaogao.com/tools/history.html?s=" + Name; Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { Detail.Clear(); sy.WebBrowser webBrowser = new sy.WebBrowser(); webBrowser.Navigate(url); webBrowser.DocumentCompleted += new sy.WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted); })); } catch (Exception ex) { Log.WriteLog(ex.Message); } finally { Enable = true; } }); }
private void web_DocumentCompleted(object sender, sy.WebBrowserDocumentCompletedEventArgs e) { sy.WebBrowser web = (sy.WebBrowser)sender; if (web.Document != null) { sy.HtmlElementCollection tbs = web.Document.GetElementsByTagName("TABLE"); if (tbs.Count == 8) { sy.HtmlElementCollection trs = tbs[7].GetElementsByTagName("TR"); ArrayList list = new ArrayList(); for (int j = 1; j < trs.Count; j++) { sy.HtmlElementCollection tds = trs[j].GetElementsByTagName("TD"); if (tds.Count == 13) { var bd = new BaseDetail { Time = Pfun.StringtoDatetime1(tds[0].InnerText.Trim(), "MM/dd/yyyy").ToString("yyyyMMdd"), Kp = Pfun.Stringtodouble1(tds[1].InnerText.Trim()), Zg = Pfun.Stringtodouble1(tds[2].InnerText.Trim()), Zd = Pfun.Stringtodouble1(tds[3].InnerText.Trim()), Sp = Pfun.Stringtodouble1(tds[4].InnerText.Trim()), Cjl = Pfun.Stringtodouble1(tds[5].InnerText.Replace(",", "").Trim()), Cjje = Pfun.Stringtodouble1(tds[6].InnerText.Replace(",", "").Trim()), Sdl = Pfun.Stringtodouble1(tds[7].InnerText.Trim()), Sdbl = Pfun.Stringtodouble1(tds[8].InnerText.Replace("%", "").Trim()), S = Pfun.Stringtoint1(tds[9].InnerText), Gdc = Pfun.Stringtodouble1(tds[10].InnerText.Replace("%", "").Trim()), Sz = Pfun.Stringtodouble1(tds[11].InnerText.Trim()), Szl = Pfun.Stringtodouble1(tds[12].InnerText.Replace("%", "").Trim()), }; Detail.Add(bd); list.Add("insert into BaseData_" + Name + "(tradetime,kpjg,zgjg,zdjg,spjg,cjl,cjje,sd,sdbl) " + "values ('" + bd.Time + "'," + bd.Kp + "," + bd.Zg + "," + bd.Zd + "," + bd.Sp + "," + bd.Cjl + "," + bd.Cjje + "," + bd.Sdl + "," + bd.Sdbl + ")"); } } if (list.Count > 0) { if (!DbHelperSqLite.Exists("select count(*) from sqlite_master where type='table' and name='BaseData_" + Name + "'")) { string tableText = (string)DbHelperSqLite.GetSingle("select table_text from need_createtable where Table_name = 'BaseData' Limit 1"); if (tableText == null) { Log.WriteLog("need_createtable表中没有BaseData_表的创建代码"); return; } tableText = tableText.Replace("XX", Name); DbHelperSqLite.ExecuteSql(tableText); } DbHelperSqLite.ExecuteSqlTran(list); if (!DbHelperSqLite.Exists("select count(*) from sqlite_master where type='table' and name='BaseDataReceived_" + Name + "'")) { string tableText = (string)DbHelperSqLite.GetSingle("select table_text from need_createtable where Table_name = 'BaseDataReceived' Limit 1"); if (tableText == null) { Log.WriteLog("need_createtable表中没有BaseDataReceived表的创建代码"); return; } tableText = tableText.Replace("XX", Name); DbHelperSqLite.ExecuteSql(tableText); } if (DbHelperSqLite.Exists("select count(*) from BaseDataReceived_" + Name)) { DbHelperSqLite.ExecuteSql("update BaseDataReceived_" + Name + " set status = 1 where tradedate ='" + DateTime.Now.ToString("yyyyMMdd") + "'"); } else { DbHelperSqLite.ExecuteSql("insert into BaseDatareceived_" + Name + "(tradedate,status) values('" + DateTime.Now.ToString("yyyyMMdd") + "',1) "); } Log.WriteLog("股票【" + Name + "】的数据获取成功"); } } } }
private void OnGetdata(object obj) { Task.Factory.StartNew(() => { try { Enable = false; Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { Detail.Clear(); })); string tableName = Name.Substring(2); if (DbHelperSqLite.Exists("select count(*) from sqlite_master where type='table' and name='DataReceived_" + tableName + "'")) { if (DbHelperSqLite.Exists("select count(*) from DataReceived_" + tableName + " where tradedate = '" + Date + "' and status =1")) { Log.WriteLog("股票【" + tableName + "】【" + Date + "】的数据已获取,不必重新获取"); return; } } string url = "http://market.finance.sina.com.cn/downxls.php?date=" + Date + "&symbol=" + Name; WebClient myWebClient = new WebClient(); Byte[] pageData = myWebClient.DownloadData(url); string pageHtml = Encoding.Default.GetString(pageData); string[] list1 = pageHtml.Split('\n'); if (list1.Length <= 5) { Log.WriteLog("股票【" + tableName + "】【" + Date + "】没有数据"); return; } ArrayList list = new ArrayList(); foreach (string m in list1) { if (m == list1[0]) { continue; } string[] n = m.Split('\t'); if (n.Length >= 5) { Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { Detail.Add(new Detail { Time = Pfun.StringtoDatetime1(Date + " " + n[0], "yyyy-MM-dd HH:mm:ss"), Price = Pfun.Stringtodouble1(n[1]), Pricechg = Pfun.Stringtodouble1(n[2]), Volume = Pfun.Stringtoint1(n[3]), Amount = Pfun.Stringtoint1(n[4]), Type = n[5] }); list.Add("insert into Detail_" + tableName + "(tradetime,price,pricechg,volume,amount,type) " + "values ('" + Date + " " + n[0] + "'," + Pfun.Stringtodouble1(n[1]).ToString(CultureInfo.InvariantCulture) + "," + Pfun.Stringtodouble1(n[2]).ToString(CultureInfo.InvariantCulture) + "," + Pfun.Stringtoint1(n[3]).ToString() + "," + Pfun.Stringtoint1(n[4]).ToString() + ",'" + n[5] + "')"); })); } } if (list.Count > 0) { if (!DbHelperSqLite.Exists("select count(*) from sqlite_master where type='table' and name='Detail_" + tableName + "'")) { string tableText = (string)DbHelperSqLite.GetSingle("select table_text from need_createtable where Table_name = 'Detail' Limit 1"); if (tableText == null) { Log.WriteLog("need_createtable表中没有Detail表的创建代码"); return; } tableText = tableText.Replace("XX", tableName); DbHelperSqLite.ExecuteSql(tableText); } DbHelperSqLite.ExecuteSqlTran(list); if (!DbHelperSqLite.Exists("select count(*) from sqlite_master where type='table' and name='DataReceived_" + tableName + "'")) { string tableText = (string)DbHelperSqLite.GetSingle("select table_text from need_createtable where Table_name = 'DataReceived' Limit 1"); if (tableText == null) { Log.WriteLog("need_createtable表中没有DataReceived表的创建代码"); return; } tableText = tableText.Replace("XX", tableName); DbHelperSqLite.ExecuteSql(tableText); } if (DbHelperSqLite.Exists("select count(*) from DataReceived_" + tableName + " where tradedate ='" + Date + "'")) { DbHelperSqLite.ExecuteSql("update DataReceived_" + tableName + " set status = 1 where tradedate ='" + Date + "'"); } else { DbHelperSqLite.ExecuteSql("insert into Datareceived_" + tableName + "(tradedate,status) values('" + Date + "',1) "); } Log.WriteLog("股票【" + tableName + "】【" + Date + "】的数据获取成功"); } } catch (Exception ex) { Log.WriteLog(ex.Message); } finally { Enable = true; } }); }