public void Execute() { var baseUrl = "http://q.stock.sohu.com/hisHq?code=cn_{0}&start=19910101&end=20150424&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp&r=0.4286239536013454&0.14818861591629684"; var sql = "select * from StockItem a where not exists (select 1 from [DayData] where StockCode = a.Code and Area = a.Area )"; using (var db = new StockContext1()) { // db.Configuration.AutoDetectChangesEnabled = false; // db.Configuration.ValidateOnSaveEnabled = false; var stocks = db.ExecuteStoreQuery<StockItem>(sql).ToList(); WebClient client = new WebClient(); var jsonpFuncLen = "historySearchHandler(".Length; foreach (var stock in stocks) { Console.WriteLine("Downloading " + stock.Code + "." + stock.Area); try { var jsonpContent = client.DownloadString(string.Format(baseUrl, stock.Code)); Console.WriteLine("Download done."); if (string.IsNullOrEmpty(jsonpContent)) { continue; } var jsonStr = jsonpContent.Substring(jsonpFuncLen, jsonpContent.Length - jsonpFuncLen - 2); var jsonObj = JsonConvert.DeserializeObject<SohuStockInfo[]>(jsonStr); if (jsonObj[0].status != 0) { continue; } Console.WriteLine("Begin commit"); foreach (var array in jsonObj[0].hq) { var stockData = new DayData() { Date = DateTime.Parse(array[0]), Open = decimal.Parse(array[1]), High = decimal.Parse(array[6]), Low = decimal.Parse(array[5]), Close = decimal.Parse(array[2]), Volume = decimal.Parse(array[7]), //AdjClose = decimal.Parse(array[6]), CreateAt = DateTime.Now, Area = stock.Area, StockCode = stock.Code }; db.DayData.AddObject(stockData); } db.SaveChanges(); Console.WriteLine("Commit done"); } catch (Exception ex) { Console.WriteLine(ex); } } } }
/// <summary> /// Deprecated Method for adding a new object to the DayData EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToDayData(DayData dayData) { base.AddObject("DayData", dayData); }
/// <summary> /// Create a new DayData object. /// </summary> /// <param name="stockCode">Initial value of the StockCode property.</param> /// <param name="area">Initial value of the Area property.</param> /// <param name="date">Initial value of the Date property.</param> /// <param name="open">Initial value of the Open property.</param> /// <param name="high">Initial value of the High property.</param> /// <param name="low">Initial value of the Low property.</param> /// <param name="close">Initial value of the Close property.</param> /// <param name="volume">Initial value of the Volume property.</param> /// <param name="adjClose">Initial value of the AdjClose property.</param> public static DayData CreateDayData(global::System.String stockCode, global::System.String area, global::System.DateTime date, global::System.Decimal open, global::System.Decimal high, global::System.Decimal low, global::System.Decimal close, global::System.Decimal volume, global::System.Decimal adjClose) { DayData dayData = new DayData(); dayData.StockCode = stockCode; dayData.Area = area; dayData.Date = date; dayData.Open = open; dayData.High = high; dayData.Low = low; dayData.Close = close; dayData.Volume = volume; dayData.AdjClose = adjClose; return dayData; }
public void Execute() { var baseUrl = "http://ichart.finance.yahoo.com/table.csv?ignore=.csv&s="; var sql = "select * from StockItem a where not exists (select 1 from [DayData] where StockCode = a.Code and Area = a.Area )"; using (var db = new StockContext1()) { var stocks = db.ExecuteStoreQuery<StockItem>(sql); WebClient client = new WebClient(); foreach (var stock in stocks) { Console.WriteLine("Downloading " + stock.Code + "." + stock.Area); try { var csvContent = client.DownloadString(baseUrl + stock.Code + "." + stock.Area); Console.WriteLine("Download done."); if (string.IsNullOrEmpty(csvContent)) { continue; } var index = 0; foreach (var line in csvContent.Split('\r', '\n')) { if (index++ != 0 && !string.IsNullOrEmpty(line)) { var array = line.Split(','); var stockData = new DayData() { Date = DateTime.Parse(array[0]), Open = decimal.Parse(array[1]), High = decimal.Parse(array[2]), Low = decimal.Parse(array[3]), Close = decimal.Parse(array[4]), Volume = decimal.Parse(array[5]), AdjClose = decimal.Parse(array[6]), CreateAt = DateTime.Now, Area = stock.Area, StockCode = stock.Code }; db.DayData.AddObject(stockData); } } db.SaveChanges(); //var objectStateEntries = db // .ObjectStateManager // .GetObjectStateEntries(EntityState.Added); //foreach (var objectStateEntry in objectStateEntries) //{ // db.Detach(objectStateEntry); //} } catch (Exception ex) { Console.WriteLine(ex); } } } }