private void button1_Click(object sender, EventArgs e) { if (typeOfCheck.SelectedItem == null) { MessageBox.Show("Please choose the type of resources to check"); return; } this.Enabled = false; db _db = new db("GLOBALEDGE_MVCConnectionString"); var manageURL = ""; switch (typeOfCheck.SelectedItem.ToString()) { case "Resources": _db.ExecuteSqlReader("SELECT uniqueid, url FROM v2_resources WHERE URL IS NOT NULL"); manageURL = "https://globaledge.msu.edu/Management/Resources/Edit/"; break; case "International Internships": _db.ExecuteSqlReader("SELECT internshipid as uniqueid, url FROM Internships"); manageURL = "https://globaledge.msu.edu/Management/InternationalInternships/InternshipEdit/"; break; default: this.Enabled = true; return; } Dictionary<int, string> urls = new Dictionary<int, string>(); while (_db.Reader.Read()) { urls.Add((int)_db.Reader["uniqueid"], (string)_db.Reader["url"]); } var counter = urls.Count; var width = (100.0 / counter); var current = 1; System.Threading.Tasks.Parallel.ForEach(urls, i => //foreach(var i in urls) { progressBar1.Value = Convert.ToInt16(width * current); current++; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(i.Value); request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3"; request.Method = "GET"; request.Proxy = null; request.UseDefaultCredentials = true; CookieContainer cc = new CookieContainer(); request.CookieContainer = cc; using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse()) { } } catch (WebException ex) { dataGridView1.Rows.Add(new string[] { i.Value, ex.Message, manageURL + i.Key + "?tag=0§ion=0" }); } }); this.Enabled = true; progressBar1.Value = 100; }
static void Main(string[] args) { // Creates two database connections db db = new db("DB"); db dbUpdate = new db("DB"); // Gets the default Parameters so I can just update instead. dbUpdate.ParameterAdd("@Latitude", ""); dbUpdate.ParameterAdd("@Longitude", ""); dbUpdate.ParameterAdd("@ZipCode", ""); // Queries the DB var sqlStr = "SELECT ZipCode FROM ZipCodes WHERE ZipCode > 48101"; var ex = db.ExecuteSqlReader(sqlStr); while (db.Reader.Read()) { Console.WriteLine("Updating " + db.Reader[0]); // Makes the call to bing. Only could get US to work for now as a zip code was giving me heck. HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://dev.virtualearth.net/REST/v1/Locations/US/-/" + db.Reader[0] + "/?key=" + System.Configuration.ConfigurationManager.AppSettings["BingKey"]); var resp = req.GetResponse(); var stream = resp.GetResponseStream(); var strReader = new StreamReader(stream); var str = (strReader.ReadToEnd()); var data = JsonConvert.DeserializeObject<BingMaps>(str); // Update the datbase sqlStr = "UPDATE ZipCodes SET Latitude=@Latitude, Longitude=@Longitude WHERE ZipCode=@ZipCode"; dbUpdate.ParameterEdit("@Latitude", data.resourceSets[0].resources[0].point.coordinates[0]); dbUpdate.ParameterEdit("@Longitude", data.resourceSets[0].resources[0].point.coordinates[1]); dbUpdate.ParameterEdit("@ZipCode", db.Reader[0]); ex = dbUpdate.ExecuteSql(sqlStr); if (ex != null) { Console.WriteLine("Zip Code: " + db.Reader[0] + " had an error in updating. Error -->" + ex.Message); } Thread.Sleep(1000); } dbUpdate.CloseConnection(); db.CloseConnection(); }