public bool Insert(OrgPercent orgPercent) { SqlConnection conn = null; string sqlInsert = string.Format("Insert Into OrgPercent Values('{0}','{1}','{2}','{3}','{4}')", orgPercent.Code, orgPercent.Day, orgPercent.Value, orgPercent.Zhuli, orgPercent.Chaoda); try { conn = new SqlConnection(SqlHelper.SqlConStr); conn.Open(); SqlCommand sqlCmd = new SqlCommand(sqlInsert, conn); sqlCmd.ExecuteNonQuery(); return true; } catch { return false; } finally { if (conn != null && conn.State != ConnectionState.Closed) { conn.Close(); } } }
public List<OrgPercent> GetOrgPercentByWhere(string sqlWhere) { SqlConnection conn = null; string strPercent = "SELECT * FROM [OrgPercent] WHERE 1=1"; if (!string.IsNullOrEmpty(sqlWhere)) { strPercent += " AND " + sqlWhere; } try { conn = new SqlConnection(SqlHelper.SqlConStr); conn.Open(); SqlCommand sqlPercent = new SqlCommand(strPercent, conn); SqlDataReader readerPercent = sqlPercent.ExecuteReader(); List<OrgPercent> orgPercentList = new List<OrgPercent>(); while (readerPercent.Read()) { OrgPercent orgPercent = new OrgPercent(); orgPercent.ID = int.Parse(readerPercent["ID"].ToString()); orgPercent.Code = readerPercent["Code"].ToString().Trim(); orgPercent.Day = readerPercent["Day"].ToString().Trim(); orgPercent.Value = Convert.ToSingle(readerPercent["Value"].ToString().Trim()); orgPercent.Zhuli = Convert.ToSingle(readerPercent["Zhuli"].ToString().Trim()); orgPercent.Chaoda = Convert.ToSingle(readerPercent["Chaoda"].ToString().Trim()); orgPercentList.Add(orgPercent); } readerPercent.Close(); return orgPercentList; } catch { return null; } finally { if (conn != null && conn.State != ConnectionState.Closed) { conn.Close(); } } }
private void DownOrgPercentData() { IEquRepository equService = ImportService.EquService; List<string> allTickers = equService.GetAllTickers(); if (allTickers == null) { this.lblMktEqud.Text = "数据库异常Q"; this.btnMktEqud.Enabled = true; return; } int tickerIndex = 0; int tickersCount = allTickers.Count; IOrgPercentRepository orgPercentService = ImportService.OrgPercentService; while (allTickers.Count > 0) { string testUrl = string.Format(Eastmoney.OrgPercentUrl, "600000"); string message = Eastmoney.GetDataFromUrl(testUrl); if (message == null) { Thread.Sleep(10000); continue; } //获取当前交易日所有股票机构控盘度数据 string orgSqlWhere = "Day='" + message.Substring(message.IndexOf("数据日期") + 5, 10) + "'"; List<OrgPercent> orgList = orgPercentService.GetOrgPercentByWhere(orgSqlWhere); //重新生成所有股票代码的副本列表 List<string> allTickersCopy = Common.GetListCopy<string>(allTickers); foreach (string ticker in allTickersCopy) { this.pbOrgPercent.Value = (tickerIndex * 100) / tickersCount; OrgPercent findPercent = orgList.Find(p => p.Code == ticker); if (findPercent == null) { testUrl = string.Format(Eastmoney.OrgPercentUrl, ticker); message = Eastmoney.GetDataFromUrl(testUrl); if (message == null) { Thread.Sleep(10000); continue; } //停牌的股票是有所有数据的的,但有些新上市的股票是没有机构参与度数据的; if (message.Contains("数据日期") && message.Contains("机构参与度为")) { string strDataTime = message.Substring(message.IndexOf("数据日期") + 5, 10); string strOrgPercent = message.Substring(message.IndexOf("机构参与度为") + 6, 10).Split('%')[0]; if (Common.IsDateTime(strDataTime) && Common.IsFloat(strOrgPercent)) { OrgPercent newOrg = new OrgPercent(); newOrg.Code = ticker; newOrg.Day = strDataTime; newOrg.Value = float.Parse(strOrgPercent); newOrg.Zhuli = double.Parse(message.Substring(message.IndexOf("主力净流入") + 5, 100).Split('>')[1].Split('<')[0]); newOrg.Chaoda = double.Parse(message.Substring(message.IndexOf("超大单流入") + 5, 100).Split('>')[1].Split('<')[0]); if (!orgPercentService.Insert(newOrg)) { Thread.Sleep(1024); continue; } } } //避免频繁访问网页被禁 Thread.Sleep(1024); } //只要能正常返回网页数据,哪怕新上市的新股没有机构参与度,也需要Remove allTickers.Remove(ticker); this.lblOrgPercent.Text = string.Format("{0}/{1}", ++tickerIndex, tickersCount); } } Thread.Sleep(1024); this.pbOrgPercent.Value = 100; this.lblOrgPercent.Text = "下载完毕!"; this.btnOrgPercent.Enabled = true; }