public int Counter(int caseCount, Lotto item, int start, int end, bool bonusTogle) { if (item.LottoNo1 > start && item.LottoNo1 < end) { caseCount++; } if (item.LottoNo2 > start && item.LottoNo2 < end) { caseCount++; } if (item.LottoNo3 > start && item.LottoNo3 < end) { caseCount++; } if (item.LottoNo4 > start && item.LottoNo4 < end) { caseCount++; } if (item.LottoNo5 > start && item.LottoNo5 < end) { caseCount++; } if (item.LottoNo6 > start && item.LottoNo6 < end) { caseCount++; } if (bonusTogle) { if (item.LottoBonusNo > start && item.LottoBonusNo < end) { caseCount++; } } return(caseCount); }
//Lotto 홈페이지의 회차, 번호를 저장(갱신)하는 이벤트 메서드. private void btnSave_Click(object sender, EventArgs e) { ConnectDb(); bool check = false; SqlCommand comm = ConnectProcedure(); comm.CommandText = "selectRecentDate"; try { sdr = comm.ExecuteReader(); if (sdr.HasRows) { while (sdr.Read()) { winningDateNumber = Convert.ToInt32(sdr["WinDateNo"].ToString()) + 1; } } } catch (Exception ex) { MessageBox.Show("다시DB를 연결 해 주세요!\n" + ex.Message); } sdr.Close(); while (!check) { string html = "https://www.dhlottery.co.kr/gameResult.do?method=byWin&drwNo=" + winningDateNumber; HtmlWeb htmlWeb = new HtmlWeb(); HtmlAgilityPack.HtmlDocument htmlDoc = htmlWeb.Load(html); HtmlNode body = htmlDoc.DocumentNode.SelectSingleNode("//body"); //1회~835회까지 읽어온다 //만약 다음회차가 없으면 회차가 없음을 출력. //있으면 다음회차 읽어와 DB에 저장 //1회차만 읽어와 클래스에 저장. foreach (var item in body.SelectNodes("//div")) { //if (item.GetAttributeValue("class", "Not Found") == "page-error") //{ // check = true; // break; //} if (item.GetAttributeValue("class", "Not Found") == "win_result") { if (string.IsNullOrEmpty(item.ChildNodes["div"].SelectNodes("div")[0].ChildNodes["p"].SelectNodes("span")[0].InnerText)) { check = true; } else { check = false; Lotto lotto = new Lotto { WinningDateNo = Int32.Parse((item.ChildNodes["h4"].SelectSingleNode("strong").InnerText).Remove((item.ChildNodes["h4"].SelectSingleNode("strong").InnerText).Length - 1, 1)), LottoNo1 = Int32.Parse(item.ChildNodes["div"].SelectNodes("div")[0].ChildNodes["p"].SelectNodes("span")[0].InnerText), LottoNo2 = Int32.Parse(item.ChildNodes["div"].SelectNodes("div")[0].ChildNodes["p"].SelectNodes("span")[1].InnerText), LottoNo3 = Int32.Parse(item.ChildNodes["div"].SelectNodes("div")[0].ChildNodes["p"].SelectNodes("span")[2].InnerText), LottoNo4 = Int32.Parse(item.ChildNodes["div"].SelectNodes("div")[0].ChildNodes["p"].SelectNodes("span")[3].InnerText), LottoNo5 = Int32.Parse(item.ChildNodes["div"].SelectNodes("div")[0].ChildNodes["p"].SelectNodes("span")[4].InnerText), LottoNo6 = Int32.Parse(item.ChildNodes["div"].SelectNodes("div")[0].ChildNodes["p"].SelectNodes("span")[5].InnerText), LottoBonusNo = Int32.Parse(item.ChildNodes["div"].SelectNodes("div")[1].ChildNodes["p"].SelectSingleNode("span").InnerText) }; SqlCommand command = ConnectProcedure(); command.CommandText = "InsertbyLottoNo"; command.Parameters.AddWithValue("lottoNo1", lotto.LottoNo1); command.Parameters.AddWithValue("lottoNo2", lotto.LottoNo2); command.Parameters.AddWithValue("lottoNo3", lotto.LottoNo3); command.Parameters.AddWithValue("lottoNo4", lotto.LottoNo4); command.Parameters.AddWithValue("lottoNo5", lotto.LottoNo5); command.Parameters.AddWithValue("lottoNo6", lotto.LottoNo6); command.Parameters.AddWithValue("lottoBonusNo", lotto.LottoBonusNo); int result = 0; try { result = command.ExecuteNonQuery(); } catch (InvalidOperationException ex) { MessageBox.Show("연결 실패 \n" + ex.Message); } } break; } } winningDateNumber++; } connection.Close(); }