//获取数据库现有数据 private int getPreData() { try { int m = int.Parse(month.Text); //月 int d = int.Parse(day.Text); //日 int y = int.Parse(yearsText.Text); //年 Dates dates = new Dates(); int searchDate = dates.getStrDateNew(y, m, d); Console.WriteLine(searchDate); if (searchDate == 0) { MessageBox.Show("请检查日期输入", "错误提示"); return(0); } basedate = searchDate; string name = comboBox1.SelectedItem.ToString(); string sqlstr = "select* from cityList where name='" + name + "'"; MySqlDataReader reader = Config.sqlSearch(sqlstr); if (reader == null) { MessageBox.Show("数据错误", "错误提示"); return(0); } if (reader.Read()) { cityWord = reader.GetString("city"); provinceWord = reader.GetString("province"); sqlstr = "select* from " + cityWord + " where date=" + searchDate.ToString(); reader = Config.sqlSearch(sqlstr); if (reader.Read()) { preDatalist = Config.setData(reader); return(1); } else { MessageBox.Show("该日数据未输入", "错误提示"); return(0); } } else { MessageBox.Show("找不到地区", "错误提示"); return(0); } } catch (Exception) { MessageBox.Show("请输入正确数据", "错误提示"); return(0); } }
//点击生成平均值 private void averdata_Click(object sender, EventArgs e) { int m1; //起始月份 int m2; //终止月份 int d1; //起始日期 int d2; //终止日期 int y1; //起始年份 int y2; //终止年份 try { m1 = int.Parse(month1.Text); m2 = int.Parse(month2.Text); d1 = int.Parse(day1.Text); d2 = int.Parse(day2.Text); y1 = int.Parse(yearText1.Text); y2 = int.Parse(yearText2.Text); Dates dates = new Dates(); int days1 = dates.getStrDateNew(y1, m1, d1); int days2 = dates.getStrDateNew(y2, m2, d2); //判断日期是否合理 if (days1 == 0 || days2 == 0) { MessageBox.Show("日期非法", "错误提示"); return; } if (days2 - days1 > 0) { string sqlstr = "select* from allData where date >= " + days1.ToString() + "&&" + "date <= " + days2.ToString(); Console.WriteLine(sqlstr); if (updateViewForAver(sqlstr) == 1) { listView1.Items.Add("平均数据"); for (int i = 0; i < 10; i++) { listView1.Items[flg].SubItems.Add(avers[i].ToString()); } } } else { MessageBox.Show("数据错误", "错误提示"); } } catch (Exception) { MessageBox.Show("数据错误", "错误提示"); } }
//专门为平均值准备的查询 public int updateViewForAver(string sqlstr) { int[] total = new int[10]; for (int i = 0; i < 10; i++) { total[i] = 0; } MySqlDataReader reader = Config.sqlSearch(sqlstr); flg = 0; if (reader == null) { MessageBox.Show("未找到数据", "错误提示"); return(0); } listView1.Items.Clear(); while (reader.Read()) { int[] data = new int[11]; data = setData(reader); for (int i = 0; i < 10; i++) { total[i] = total[i] + data[i + 1]; } Dates dates = new Dates(); string datestr = dates.getDateStrNew(data[0]); Console.WriteLine(datestr); listView1.Items.Add(datestr); for (int i = 1; i < 11; i++) { listView1.Items[flg].SubItems.Add(data[i].ToString()); } flg = flg + 1; } avers = new int[10]; //avers数组,用于记录平均数 for (int i = 0; i < 10; i++) { avers[i] = total[i] / flg; } return(1); }
//用于查询地区 private void provinceName_TextChanged(object sender, EventArgs e) { int date = 0; string sqlstr = "select* from cityList where name='" + provinceName.Text + "'"; string province; //省份 string city; //城市 MySqlDataReader reader = Config.sqlSearch(sqlstr); if (reader == null) { MessageBox.Show("数据错误", "错误提示"); return; } if (reader.Read()) { province = reader.GetString("province"); city = reader.GetString("city"); sqlstr = "select* from " + city; reader = Config.sqlSearch(sqlstr); if (reader == null) { MessageBox.Show("数据错误", "错误提示"); return; } while (reader.Read()) { date = reader.GetInt32("date"); } Dates dates = new Dates(); textBox1.Text = (date + 1).ToString(); nextDate.Text = dates.getDateStrNew(date + 1); } else { return; } }
private void button1_Click(object sender, EventArgs e) { string province; //省份 string city; //城市 string sqlstr = "select* from cityList where name='" + provinceName.Text + "'"; int[] datalist = new int[11]; for (int i = 0; i < 11; i++) { datalist[i] = 0; } int[] inputdata; MySqlDataReader reader = Config.sqlSearch(sqlstr); if (reader == null) { MessageBox.Show("数据错误", "错误提示"); return; } if (reader.Read()) { province = reader.GetString("province"); city = reader.GetString("city"); int date = int.Parse(textBox1.Text); sqlstr = "select* from " + city + " where date=" + (date - 1).ToString(); reader = Config.sqlSearch(sqlstr); if (reader == null) { MessageBox.Show("数据错误", "错误提示"); return; } if (reader.Read()) { datalist = Config.setData(reader); } inputdata = datalist; try { inputdata[0] = date; inputdata[2] = int.Parse(newDefinite.Text); inputdata[4] = int.Parse(newCure.Text); inputdata[6] = int.Parse(newDeath.Text); inputdata[8] = int.Parse(newSuspected.Text); inputdata[10] = int.Parse(newDanger.Text); for (int i = 0; i < 5; i++) { inputdata[i * 2 + 1] = datalist[i * 2 + 1] + inputdata[i * 2 + 2]; if (inputdata[i * 2 + 1] < 0) { MessageBox.Show("数据不合法", "提示"); return; } } } catch (Exception) { MessageBox.Show("错误输入", "提示"); return; } sqlstr = "insert into " + city + " (date,allDefinite,newDefinite,allCure,newCure,allDeath,newDeath,nowSuspected,newSuspected,nowDanger,newDanger) values ("; for (int i = 0; i < 10; i++) { sqlstr = sqlstr + inputdata[i] + ","; } sqlstr = sqlstr + inputdata[10] + ")"; Console.WriteLine(sqlstr); if (Config.sqlExcuteChange(sqlstr) == 1) { textBox1.Text = (inputdata[0] + 1).ToString(); Dates dates = new Dates(); nextDate.Text = dates.getDateStrNew((inputdata[0] + 1)); basedate = inputdata[0]; changedatalist = inputdata; //这个地方出错需要回滚,需要加强鲁棒性 updateProvince(province, basedate, changedatalist); updateAll(basedate, changedatalist); MessageBox.Show("添加成功", "提示"); return; } else { MessageBox.Show("数据库未正常连接", "提示"); return; } } else { MessageBox.Show("数据错误", "提示"); return; } }
//点击查看拐点信息(写到这里才想起来C#有list,之前一直都在郁闷为什么C#没有vector) private void keyPoint_Click(object sender, EventArgs e) { string sqlstr = "select * from allData"; MySqlDataReader reader = Config.sqlSearch(sqlstr); if (reader == null) { MessageBox.Show("数据错误", "错误提示"); return; } int check1 = 0; int check2 = 0; int tmp = 0; int[] maybeData = new int[11]; listView1.Items.Clear(); flg = 0; while (reader.Read()) { if (reader.GetInt32("newDefinite") > tmp) { check1 = check1 + 1; if (check1 >= 5) { maybeData = setData(reader); } } else if (reader.GetInt32("newDefinite") == tmp) { } else { if (check1 >= 5) { check2 = check2 + 1; check1 = 5; } else { check1 = 0; check2 = 0; } } tmp = reader.GetInt32("newDefinite"); if (check1 >= 5 && check2 >= 5) { Console.WriteLine(maybeData[0]); Dates date = new Dates(); string datastr = date.getDateStrNew(maybeData[0]); listView1.Items.Add(datastr); for (int i = 1; i < 11; i++) { listView1.Items[flg].SubItems.Add(maybeData[i].ToString()); } flg = flg + 1; check1 = 0; check2 = 0; } } }