//private void getNeededData(DataTable table, Sites site) private void getNeededData(DataTable table, sitedata site) {//get a list of all the variables in the table var x = (from r in table.AsEnumerable() select r["Element"]).Distinct().ToList(); //loop through all of the variables foreach (var variable in x) { if (variable.ToString() == "PRCP") { Variables vari = db.getVariableID(variable.ToString()); int seriesID = db.getSeriesID(site.SiteID, vari.VariableID); DateTime startdate; if (seriesID >= 0) { //get the end date for the current variable startdate = db.getSeriesLastDate(seriesID); } else { //if series is not in table set start date startdate = new DateTime(1900, 01, 01); } if (Math.Abs(startdate.Subtract(DateTime.Today).Days) > 3) { string expression = "Date > '" + startdate.ToString() + "' AND Date <= '" + DateTime.Today.Add(new TimeSpan(-1, 0, 0, 0)).ToString() + "' AND Element = '" + variable + "'"; DataRow[] selectedrows = table.Select(expression); int rows = db.saveDataValues(selectedrows, vari, site); DBLogging.WriteLog(projectName, "Log", "harvestData" + "." + (new StackTrace(true)).GetFrame(0).GetMethod().Name + "()", string.Format("{0} rows saved. Date > {1}, Site:{2}, Variable:{3} ", rows, startdate, site.SiteCode, variable.ToString())); } } } }
public int saveDataValues(DataRow[] Rows, Variables variable, sitedata site) { DataTable values = createDVTable("DataValues"); int count = 0; try { foreach (DataRow row in Rows) { DateTime localDateTime; localDateTime = (DateTime)row["Date"]; double utcOffset = -7; int qualifierID; try { string qualCode = row["MFlag"].ToString() + row["QFlag"].ToString() + row["SFlag"].ToString(); qualCode = qualCode.Replace(' ', '_'); qualifierID = GetQualifierID(qualCode); } catch { qualifierID = -99; } DateTime dateTimeUTC = localDateTime.Add(new TimeSpan((int)utcOffset, 0, 0)); try { SeriesCatalog sc = (from series in this.SeriesCatalog where series.SiteID == site.SiteID && series.VariableID == variable.VariableID select series).First(); values.Rows.Add(SqlInt64.Null, Convert.ToDouble(row["Value"]), SqlDouble.Null, localDateTime, utcOffset, dateTimeUTC, site.SiteID, variable.VariableID, SqlDouble.Null, SqlInt32.Null, "nc", (qualifierID < 0 ? SqlInt32.Null : qualifierID), sc.MethodID, sc.SourceID, SqlInt32.Null, SqlInt32.Null, sc.QualityControlLevelID); count++; } catch (Exception ex) { values.Rows.Add(SqlInt64.Null, Convert.ToDouble(row["Value"]), SqlDouble.Null, localDateTime, utcOffset, dateTimeUTC, site.SiteID, variable.VariableID, SqlDouble.Null, SqlInt32.Null, "nc", (qualifierID < 0 ? SqlInt32.Null : qualifierID), 0, 1, SqlInt32.Null, SqlInt32.Null, 1); count++; } } this.InsertBulk(values); } catch (Exception ex) { throw new Exception("clsDatabase.SaveSeries( DataTable, " + site.SiteID + ", " + variable.VariableID + ") on row " + count, ex); } return(count); }