protected virtual void AddImportRow(DateTime updateTime, ForexData data, bool isTotalVolume, databases.importDS.importPriceDataTable tbl) { databases.importDS.importPriceRow oldImportRow; databases.importDS.importPriceRow importRow = tbl.NewimportPriceRow(); databases.AppLibs.InitData(importRow); importRow.isTotalVolume = isTotalVolume; importRow.onDate = updateTime; importRow.stockCode = data.code; importRow.volume = 0; importRow.closePrice = data.last; //Only add new when there are some changes oldImportRow = lastImportData.Find(importRow); if (!lastImportData.IsSameData(importRow, oldImportRow)) { tbl.AddimportPriceRow(importRow); lastImportData.Update(importRow); } else { importRow.CancelEdit(); } }
/// <summary> /// Import data from URL to tables : importPrice /// The function also detect and add new companies to the database /// </summary> /// <param name="url">URL to get data</param> /// <param name="priceMeta">meta describe the webpage structure</param> /// <param name="stockExchangeCode">stock exchange code of imported data </param> /// <param name="importPriceTbl"> where to store imported data </param> public databases.importDS.importPriceDataTable GetPriceFromWeb(DateTime updateTime, metaStock priceMeta, databases.baseDS.exchangeDetailRow exchangeDetailRow) { bool bHaveData = false; //Kiem tra xem bang co du lieu hay ko //Data in CultureInfo CultureInfoUS = common.language.GetCulture("en-US"); databases.importDS.importPriceDataTable importPriceTbl = new databases.importDS.importPriceDataTable(); this.mappingList = CreateMapping(priceMeta, importPriceTbl); // Get the URL specified var webGet = new HtmlWeb(); var document = webGet.Load(exchangeDetailRow.address); // Get <a> tags that have a href attribute and non-whitespace inner text var linksOnPage = from item in document.DocumentNode.Descendants() where item.Name == "td" && item.Attributes["id"] == null select new { Text = item.InnerText }; bool fError = false; int igmoreRowCount = 0, columnCount = 0; decimal val = 0; databases.importDS.importPriceRow importRow = null; databases.importDS.importPriceRow oldImportRow; string stockCode; foreach (var item in linksOnPage) { //Check whether to ignore some items at the first if (++igmoreRowCount <= priceMeta.noRowIgnore) { continue; } if (fError) { break; } if (columnCount == priceMeta.startAtColId) { stockCode = item.Text.Trim(); importRow = importPriceTbl.NewimportPriceRow(); databases.AppLibs.InitData(importRow); importRow.onDate = updateTime; importRow.stockCode = stockCode; importRow.isTotalVolume = true; columnCount++; continue; } //Last column if (columnCount == priceMeta.endAtColId) { if (importRow.closePrice > 0) { //Only add new when there are some changes oldImportRow = lastImportData.Find(importRow); if (!lastImportData.IsSameData(importRow, oldImportRow)) { importPriceTbl.AddimportPriceRow(importRow); lastImportData.Update(importRow); } else { importRow.CancelEdit(); } } else { importRow.CancelEdit(); } columnCount = 0; continue; } object obj = this.mappingList.GetValue(columnCount.ToString()); if (obj != null) { val = 0; common.system.StrToDecimal(item.Text, CultureInfoUS, out val); //15/07: Kiem tra gia tri co bang 0 hay ko. Neu co thi se co loi doc du lieu if (val != 0) { bHaveData = true; } importRow[(string)obj] = val; } columnCount++; } //Neu ko co du lieu if (!bHaveData) { throw new Exception(); } return(importPriceTbl); }