Example #1
0
        public WaterStateInfo GetStateInfo(string state)
        {
            WaterStateInfo record = new WaterStateInfo();

            record.ID         = -1;
            record.IsImported = false;
            using (SqlConnection conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                using (SqlCommand stateLookup = new SqlCommand(string.Format("select * from WaterDataStates where StateCode='{0}'", state), conn))
                    using (SqlDataReader reader = stateLookup.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            record.ID         = reader.GetInt64(0);
                            record.StateText  = reader.GetString(1);
                            record.StateCode  = reader.GetString(2);
                            record.IsImported = (bool)reader.GetSqlBoolean(3);
                        }
                        reader.Close();
                    }
            }
            return(record);
        }
        } //End RemoveCurrentWaterData

        private void ImportWaterDataByState(string state, DateTime startDate, bool reset)
        {
            WaterStateInfo stateInfo = _repo.GetStateInfo(state);

            if (!stateInfo.IsImported || reset)
            {
                //Get information
                string    url      = String.Format(@"http://waterservices.usgs.gov/nwis/site/?format=rdb&stateCd={0}&outputDataTypeCd=dv&startDT={1}&parameterCd={2}", state, startDate.ToString("yyyy-MM-dd"), ParametersToStringForRead());
                WebClient client   = new WebClient();
                string    response = client.DownloadString(url);

                // split the response into rows based on the new line character
                List <string> rows = response.Split('\n').ToList <string>();
                while (rows[0].StartsWith("#"))
                {
                    rows.RemoveAt(0); // remove the header comment rows
                }
                rows.RemoveAt(0);     // remove the header row
                rows.RemoveAt(0);     // remove the definition row
                this.WriteData(rows, stateInfo.ID, startDate);
                //Reset the instance's last date value
                this._lastDate = _repo.GetLastDate();
            }
        } //End ImportWaterDataByState