private int UpdateMGT_OP(MgtParameter mgt, int subbasinID, int hruID, int Month, int Day, int MGT_OP) { int n = 0; AccessHelper helper = new AccessHelper(); if (MGT_OP == 1) { string sql = @"update mgt2 set [MONTH] =" + mgt.plant_month + ",[DAY] = " + mgt.plant_day + ",HUSC = " + mgt.plant_husc + ",PLANT_ID = " + mgt.plant_id + ",CURYR_MAT = " + mgt.curyr_mat + ",HEATUNITS = " + mgt.heat_units + ",BIO_INIT = " + mgt.bio_init + ",HI_TARG = " + mgt.hi_targ + ",LAI_INIT = " + mgt.lai_init + ",BIO_TARG = " + mgt.bio_init + ",CNOP = " + mgt.plant_cnop + " where SUBBASIN = " + subbasinID + " and HRU = " + hruID + " and MGT_OP = " + MGT_OP + " and MONTH = " + Month + " and DAY = " + Day; n = helper.ExcuteSql(sql); } else if (MGT_OP == 2) { string sql = @"update mgt2 set [MONTH] = " + mgt.irr_month + ",[DAY] = " + mgt.irr_day + ",HUSC = " + mgt.irr_husc + ",IRR_AMT = " + mgt.irr_amt + ",IRR_SALT = " + mgt.irr_salt + ",IRR_EFM = " + mgt.irr_efm + ",IRR_SQ = " + mgt.irr_sq + ",IRR_SC = " + mgt.irr_sc + ",IRR_NO = " + mgt.irr_no + " where SUBBASIN = " + subbasinID + " and HRU = " + hruID + " and MGT_OP = " + MGT_OP + " and MONTH = " + Month + " and DAY = " + Day; n = helper.ExcuteSql(sql); } else if (MGT_OP == 3) { string sql = @"update mgt2 set [MONTH] = " + mgt.frt_month + ",[DAY] = " + mgt.frt_day + ",HUSC = " + mgt.frt_husc + ",FERT_ID = " + mgt.fert_id + ",FRT_KG = " + mgt.frt_kg + ",FRT_SURFACE = " + mgt.frt_surface + " where SUBBASIN = " + subbasinID + " and HRU = " + hruID + " and MGT_OP = " + MGT_OP + " and MONTH = " + Month + " and DAY = " + Day; n = helper.ExcuteSql(sql); } else if (MGT_OP == 5) { string sql = @"update mgt2 set [MONTH] = " + mgt.harv_month + ",[DAY] = " + mgt.harv_day + ",HUSC = " + mgt.harv_husc + ",CNOP = " + mgt.harv_cnop + " where SUBBASIN = " + subbasinID + " and HRU = " + hruID + " and MGT_OP = " + MGT_OP + " and MONTH = " + Month + " and DAY = " + Day; n = helper.ExcuteSql(sql); } return(n); }
/// <summary> /// 同一个表中复制一条记录到另外一条记录(主键除外) /// </summary> /// <param name="subbasinID">子流域编号</param> /// <param name="hruID">hru编号</param> /// <param name="MGT_OP">操作编号</param> /// <param name="mgt">MGT参数类</param> private void CopyToSelf(int subbasinID, int hruID, int MGT_OP, MgtParameter mgt) { AccessHelper MGTHelper = new AccessHelper(); /* * 改代码舍弃!!!存在严重BUG * 拷贝指定子流域下的指定HRU的一个副本,然后对该副本进行更新数据(利用原来的数据表格式) * 由于YEAR,MONTH,DAY,都是ACCESS的关键字,所以要加中括号(“[]”) */ // string sql = @"INSERT INTO mgt2(SUBBASIN,HRU,LANDUSE,SOIL, SLOPE_CD, CROP, [YEAR], [MONTH], [DAY], HUSC, MGT_OP, HEATUNITS, PLANT_ID, CURYR_MAT, LAI_INIT,BIO_INIT, // HI_TARG, BIO_TARG,CNOP, IRR_AMT, FERT_ID,FRT_KG,FRT_SURFACE,PEST_ID,PST_KG,TILLAGE_ID,HARVEFF,HI_OVR,GRZ_DAYS,MANURE_ID,BIO_EAT, // BIO_TRMP,MANURE_KG,WSTRS_ID,AUTO_WSTRS,AFERT_ID,AUTO_NSTRS,AUTO_NAPP,AUTO_NYR,AUTO_EFF,AFRT_SURFACE,SWEEPEFF,FR_CURB,IMP_TRIG, // FERT_DAYS,CFRT_ID,IFRT_FREQ,CFRT_KG, PST_DEP,IHV_GBM,IRR_SALT,IRR_EFM,IRR_SQ,IRR_EFF,IRR_MX,IRR_ASQ, CPST_ID,PEST_DAYS,IPEST_FREQ, // CPST_KG,BURN_FRLB,OP_NUM,IRR_SC,IRR_NO,IRR_SCA,IRR_NOA) // SELECT TOP 1 SUBBASIN,HRU,LANDUSE,SOIL, SLOPE_CD, CROP, YEAR, MONTH, DAY, HUSC, MGT_OP, HEATUNITS, PLANT_ID, CURYR_MAT, LAI_INIT, BIO_INIT, // HI_TARG, BIO_TARG,CNOP, IRR_AMT, FERT_ID,FRT_KG,FRT_SURFACE,PEST_ID,PST_KG,TILLAGE_ID,HARVEFF,HI_OVR,GRZ_DAYS,MANURE_ID,BIO_EAT, // BIO_TRMP,MANURE_KG,WSTRS_ID,AUTO_WSTRS,AFERT_ID,AUTO_NSTRS,AUTO_NAPP,AUTO_NYR,AUTO_EFF,AFRT_SURFACE,SWEEPEFF,FR_CURB,IMP_TRIG, // FERT_DAYS,CFRT_ID,IFRT_FREQ,CFRT_KG, PST_DEP,IHV_GBM,IRR_SALT,IRR_EFM,IRR_SQ,IRR_EFF,IRR_MX,IRR_ASQ, CPST_ID,PEST_DAYS,IPEST_FREQ, // CPST_KG,BURN_FRLB,OP_NUM,IRR_SC,IRR_NO,IRR_SCA,IRR_NOA // FROM mgt2 WHERE SUBBASIN = " + subbasinID + " AND HRU = " + hruID + " AND MGT_OP = " + MGT_OP; DataTable hru_info = getLandUse_Soil_SlopeBySUBAndHRU(subbasinID, hruID); //获得hru的三项信息 string land_use = hru_info.Rows[0][0].ToString(); string soil = hru_info.Rows[0][1].ToString(); string slope = hru_info.Rows[0][2].ToString(); string sql = @"INSERT INTO mgt2(SUBBASIN,HRU,LANDUSE,SOIL, SLOPE_CD, CROP, [YEAR], [MONTH], [DAY], HUSC, MGT_OP,HEATUNITS, PLANT_ID, CURYR_MAT, LAI_INIT,BIO_INIT,HI_TARG, BIO_TARG,CNOP, IRR_AMT, FERT_ID,FRT_KG,FRT_SURFACE,PEST_ID,PST_KG,TILLAGE_ID,HARVEFF,HI_OVR,GRZ_DAYS,MANURE_ID, BIO_EAT,BIO_TRMP,MANURE_KG,WSTRS_ID,AUTO_WSTRS,AFERT_ID,AUTO_NSTRS,AUTO_NAPP,AUTO_NYR,AUTO_EFF, AFRT_SURFACE,SWEEPEFF,FR_CURB,IMP_TRIG,FERT_DAYS,CFRT_ID,IFRT_FREQ,CFRT_KG, PST_DEP,IHV_GBM, IRR_SALT,IRR_EFM,IRR_SQ,IRR_EFF,IRR_MX,IRR_ASQ, CPST_ID,PEST_DAYS,IPEST_FREQ,CPST_KG, BURN_FRLB,OP_NUM,IRR_SC,IRR_NO,IRR_SCA,IRR_NOA) values(" + subbasinID + "," + hruID + ",'" + land_use + "','" + soil + "','" + slope + "','',1,1,1,0," + "1,0,0,0,0,0,0,0,0,0," + "0,0,0,0,0,0,0,0,0,0," + "0,0,0,0,0,0,0,0,0,0," + "0,0,0,0,0,0,0,0,0,0," + "0,0,0,0,0,0,0,0,0,0," + "0,0,0,0,0,0)"; int n = MGTHelper.ExcuteSql(sql); //获取新加数据的OID编号 sql = "select oid from mgt2 where subbasin = " + subbasinID + " and hru = " + hruID + " order by oid desc"; DataTable dt = MGTHelper.GetDataTableFromDB(sql); int OID = Convert.ToInt32(dt.Rows[0][0]); //根据MGT_OP和OID更新数据 if (MGT_OP == 1) //种植 { sql = @"update mgt2 set CROP = '" + mgt.crop + "',[YEAR] = 1" + ",[MONTH] = " + mgt.plant_month + ",[DAY] = " + mgt.plant_day + ",HUSC = 0" + ",MGT_OP = " + mgt.plant_mgt_op + ",HEATUNITS = " + mgt.heat_units + ",PLANT_ID = " + mgt.plant_id + ",CURYR_MAT = " + mgt.curyr_mat + ",LAI_INIT = " + mgt.lai_init + ",HI_TARG = " + mgt.hi_targ + ",BIO_TARG = " + mgt.bio_targ + ",CNOP = " + mgt.plant_cnop + " WHERE OID =" + OID + " and subbasin = " + subbasinID + " and hru =" + hruID; n = MGTHelper.ExcuteSql(sql); } else if (MGT_OP == 2) //灌溉 { sql = @"update mgt2 set CROP = null,[YEAR] = 1" + ",[MONTH] = " + mgt.irr_month + ",[DAY] = " + mgt.irr_day + ",HUSC = " + mgt.irr_husc + ",MGT_OP = " + mgt.irr_mgt_op + ",IRR_AMT = " + mgt.irr_amt + ",IRR_SALT = " + mgt.irr_salt + ",IRR_EFM = " + mgt.irr_efm + ",IRR_SQ = " + mgt.irr_sq + ",IRR_SC = " + mgt.irr_sc + ",IRR_NO = " + mgt.irr_no + " WHERE OID =" + OID + " and subbasin = " + subbasinID + " and hru =" + hruID; n = MGTHelper.ExcuteSql(sql); } else if (MGT_OP == 3) //施肥 { sql = @"update mgt2 set CROP = null,[YEAR] = 1" + ",[MONTH] = " + mgt.frt_month + ",[DAY] = " + mgt.frt_day + ",HUSC = " + mgt.frt_husc + ",MGT_OP =" + mgt.frt_mgt_op + ",FERT_ID = " + mgt.fert_id + ",FRT_KG = " + mgt.frt_kg + ",FRT_SURFACE = " + mgt.frt_surface + " where OID = " + OID + " and subbasin = " + subbasinID + " and hru = " + hruID; n = MGTHelper.ExcuteSql(sql); } else if (MGT_OP == 5) //收割 { sql = @"update mgt2 set CROP = null,[YEAR] = 1" + ",[MONTH] = " + mgt.harv_month + ",[DAY] = " + mgt.harv_day + ",HUSC = " + mgt.harv_husc + ",MGT_OP = " + mgt.harv_mgt_op + ",CNOP = " + mgt.harv_cnop + " where OID = " + OID + " and subbasin = " + subbasinID + " and hru = " + hruID; n = MGTHelper.ExcuteSql(sql); } }
/// <summary> /// 添加操作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAddOperation_Click(object sender, EventArgs e) { MgtParameter mgt = new MgtParameter(); int hruNum = 0; //hru的数量 //判断不同的操作 if (tabControlMgt.SelectedIndex == 0) //种植 { try { hruNum = getHruNum(Convert.ToInt32(subbasinId)); mgt.plant_month = Convert.ToInt32(PLANT_MONTH.SelectedItem); mgt.plant_day = Convert.ToInt32(PLANT_DAY.SelectedItem); mgt.plant_husc = 0; mgt.plant_mgt_op = 1; mgt.plant_id = Convert.ToInt32(PLANT_ID.Text.Split(',')[0]); mgt.crop = getPlantNameByID(mgt.plant_id); //作物名称 mgt.curyr_mat = Convert.ToInt32(CURYR_MAT.Text); mgt.heat_units = Convert.ToInt32(PLANT_HEAT_UNITS.Text); mgt.bio_init = Convert.ToDouble(BIO_INIT.Text); mgt.hi_targ = Convert.ToDouble(HI_TARG.Text); mgt.lai_init = Convert.ToDouble(LAI_INIT.Text); mgt.bio_targ = Convert.ToDouble(BIO_TARG.Text); mgt.plant_cnop = Convert.ToDouble(PLANT_CNOP.Text); //对子流域的所有hru进行操作 for (int hruID = 1; hruID <= hruNum; hruID++) { CopyToSelf(Convert.ToInt32(subbasinId), hruID, 1, mgt); //※拷贝固定子流域的每一个HRU的副本,然后在对该副本进行更新数据 MgtMessage.Text = "subbasin : " + subbasinId + ",hru:" + hruID + " 添加成功!"; } } catch (Exception ex) { MessageBox.Show(ex.Message); MgtMessage.Text = "添加失败!"; } InitDataTable(); } else if (tabControlMgt.SelectedIndex == 1) //灌溉 { try { hruNum = getHruNum(Convert.ToInt32(subbasinId)); mgt.irr_month = Convert.ToInt32(IRR_MONTH.SelectedItem); mgt.irr_day = Convert.ToInt32(IRR_DAY.SelectedItem); mgt.irr_husc = 0; mgt.irr_mgt_op = 2; mgt.irr_amt = Convert.ToDouble(IRR_AMT.Text); mgt.irr_salt = Convert.ToDouble(IRR_SALT.Text); mgt.irr_efm = Convert.ToDouble(IRR_EFM.Text); mgt.irr_sq = Convert.ToDouble(IRR_SQ.Text); mgt.irr_sc = Convert.ToDouble(IRR_SC.Text); mgt.irr_no = Convert.ToDouble(IRR_NO.Text); //对子流域的所有hru进行操作 for (int hruID = 1; hruID <= hruNum; hruID++) { CopyToSelf(Convert.ToInt32(subbasinId), hruID, 2, mgt); //复制一条数据 MgtMessage.Text = "subbasin : " + subbasinId + ",hru:" + hruID + " 添加成功!"; } } catch (Exception ex) { MessageBox.Show(ex.Message); MgtMessage.Text = "添加失败!"; } InitDataTable(); } else if (tabControlMgt.SelectedIndex == 2) //施肥 { try { hruNum = getHruNum(Convert.ToInt32(subbasinId)); mgt.frt_month = Convert.ToInt32(FRT_MONTH.SelectedItem); mgt.frt_day = Convert.ToInt32(FRT_DAY.SelectedItem); mgt.frt_husc = 0; mgt.frt_mgt_op = 3; mgt.fert_id = Convert.ToInt32(FERT_ID.Text.Split(',')[0]); mgt.fert_name = getFertNameByID(mgt.fert_id); mgt.frt_kg = Convert.ToDouble(FRT_KG.Text); mgt.frt_surface = Convert.ToDouble(FRT_SURFACE.Text); //对子流域的所有hru进行操作 for (int hruID = 1; hruID <= hruNum; hruID++) { CopyToSelf(Convert.ToInt32(subbasinId), hruID, 3, mgt); //复制一条数据 MgtMessage.Text = "subbasin : " + subbasinId + ",hru:" + hruID + " 添加成功!"; } } catch (Exception ex) { MessageBox.Show(ex.Message); MgtMessage.Text = "添加失败!"; } InitDataTable(); } else if (tabControlMgt.SelectedIndex == 3) //收割 { try { hruNum = getHruNum(Convert.ToInt32(subbasinId)); mgt.harv_month = Convert.ToInt32(HARV_MONTH.SelectedItem); mgt.harv_day = Convert.ToInt32(HARV_DAY.SelectedItem); mgt.harv_mgt_op = 5; mgt.harv_husc = 0; mgt.harv_cnop = Convert.ToDouble(HARV_CNOP.Text); //对子流域的所有hru进行操作 for (int hruID = 1; hruID <= hruNum; hruID++) { CopyToSelf(Convert.ToInt32(subbasinId), hruID, 5, mgt); //复制一条数据 MgtMessage.Text = "subbasin : " + subbasinId + ",hru:" + hruID + " 添加成功!"; } } catch (Exception ex) { MessageBox.Show(ex.Message); MgtMessage.Text = "添加失败!"; } InitDataTable(); } }
/// <summary> /// 更新操作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnUpdate_Click(object sender, EventArgs e) { MgtParameter mgt = new MgtParameter(); int hruNum = 0; //hru的数量 DialogResult dr = MessageBox.Show("正在进行更新操作", "更新数据", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (dr == DialogResult.Cancel) { return; } //判断不同的操作 if (tabControlMgt.SelectedIndex == 0) //种植 { try { int currentMonth = Convert.ToInt32(dataGridViewMgt.CurrentRow.Cells["Month"].Value.ToString()); int currentDay = Convert.ToInt32(dataGridViewMgt.CurrentRow.Cells["Day"].Value.ToString()); hruNum = getHruNum(Convert.ToInt32(subbasinId)); mgt.plant_month = Convert.ToInt32(PLANT_MONTH.SelectedItem); mgt.plant_day = Convert.ToInt32(PLANT_DAY.SelectedItem); mgt.plant_husc = 0; mgt.plant_mgt_op = 1; mgt.plant_id = Convert.ToInt32(PLANT_ID.Text.Split(',')[0]); mgt.crop = getPlantNameByID(mgt.plant_id); //作物名称 mgt.curyr_mat = Convert.ToInt32(CURYR_MAT.Text); mgt.heat_units = Convert.ToDouble(PLANT_HEAT_UNITS.Text); mgt.bio_init = Convert.ToDouble(BIO_INIT.Text); mgt.hi_targ = Convert.ToDouble(HI_TARG.Text); mgt.lai_init = Convert.ToDouble(LAI_INIT.Text); mgt.bio_targ = Convert.ToDouble(BIO_TARG.Text); mgt.plant_cnop = Convert.ToDouble(PLANT_CNOP.Text); //对子流域的所有hru进行操作 for (int hruID = 1; hruID <= hruNum; hruID++) { UpdateMGT_OP(mgt, Convert.ToInt32(subbasinId), hruID, currentMonth, currentDay, mgt.plant_mgt_op); MgtMessage.Text = "subbasin : " + subbasinId + ",hru:" + hruID + " 更新参数成功!"; } } catch (Exception ex) { MessageBox.Show(ex.Message); MgtMessage.Text = "更新参数失败,请重试!"; } InitDataTable(); } else if (tabControlMgt.SelectedIndex == 1) //灌溉 { try { int currentMonth = Convert.ToInt32(dataGridViewMgt.CurrentRow.Cells["Month"].Value.ToString()); int currentDay = Convert.ToInt32(dataGridViewMgt.CurrentRow.Cells["Day"].Value.ToString()); hruNum = getHruNum(Convert.ToInt32(subbasinId)); mgt.irr_month = Convert.ToInt32(IRR_MONTH.SelectedItem); mgt.irr_day = Convert.ToInt32(IRR_DAY.SelectedItem); mgt.irr_husc = 0; mgt.irr_mgt_op = 2; mgt.irr_amt = Convert.ToDouble(IRR_AMT.Text); mgt.irr_salt = Convert.ToDouble(IRR_SALT.Text); mgt.irr_efm = Convert.ToDouble(IRR_EFM.Text); mgt.irr_sq = Convert.ToDouble(IRR_SQ.Text); mgt.irr_sc = Convert.ToDouble(IRR_SC.Text); mgt.irr_no = Convert.ToDouble(IRR_NO.Text); //对子流域的所有hru进行操作 for (int hruID = 1; hruID <= hruNum; hruID++) { UpdateMGT_OP(mgt, Convert.ToInt32(subbasinId), hruID, currentMonth, currentDay, mgt.irr_mgt_op); MgtMessage.Text = "subbasin : " + subbasinId + ",hru:" + hruID + " 更新参数成功!"; } } catch (Exception ex) { MessageBox.Show(ex.Message); MgtMessage.Text = "更新参数失败,请重试!"; } InitDataTable(); } else if (tabControlMgt.SelectedIndex == 2) //施肥 { try { int currentMonth = Convert.ToInt32(dataGridViewMgt.CurrentRow.Cells["Month"].Value.ToString()); int currentDay = Convert.ToInt32(dataGridViewMgt.CurrentRow.Cells["Day"].Value.ToString()); hruNum = getHruNum(Convert.ToInt32(subbasinId)); mgt.frt_month = Convert.ToInt32(FRT_MONTH.SelectedItem); mgt.frt_day = Convert.ToInt32(FRT_DAY.SelectedItem); mgt.frt_husc = 0; mgt.frt_mgt_op = 3; mgt.fert_id = Convert.ToInt32(FERT_ID.Text.Split(',')[0]); mgt.fert_name = getFertNameByID(mgt.fert_id); mgt.frt_kg = Convert.ToDouble(FRT_KG.Text); mgt.frt_surface = Convert.ToDouble(FRT_SURFACE.Text); //对子流域的所有hru进行操作 for (int hruID = 1; hruID <= hruNum; hruID++) { UpdateMGT_OP(mgt, Convert.ToInt32(subbasinId), hruID, currentMonth, currentDay, mgt.frt_mgt_op); MgtMessage.Text = "subbasin : " + subbasinId + ",hru:" + hruID + " 更新参数成功!"; } } catch (Exception ex) { MessageBox.Show(ex.Message); MgtMessage.Text = "更新参数失败,请重试!"; } InitDataTable(); } else if (tabControlMgt.SelectedIndex == 3) //收割 { try { int currentMonth = Convert.ToInt32(dataGridViewMgt.CurrentRow.Cells["Month"].Value.ToString()); int currentDay = Convert.ToInt32(dataGridViewMgt.CurrentRow.Cells["Day"].Value.ToString()); hruNum = getHruNum(Convert.ToInt32(subbasinId)); mgt.harv_month = Convert.ToInt32(HARV_MONTH.SelectedItem); mgt.harv_day = Convert.ToInt32(HARV_DAY.SelectedItem); mgt.harv_mgt_op = 5; mgt.harv_husc = 0; mgt.harv_cnop = Convert.ToDouble(HARV_CNOP.Text); //对子流域的所有hru进行操作 for (int hruID = 1; hruID <= hruNum; hruID++) { UpdateMGT_OP(mgt, Convert.ToInt32(subbasinId), hruID, currentMonth, currentDay, mgt.harv_mgt_op); MgtMessage.Text = "subbasin : " + subbasinId + ",hru:" + hruID + " 更新参数成功!"; } } catch (Exception ex) { MessageBox.Show(ex.Message); MgtMessage.Text = "更新参数失败,请重试!"; } MgtMessage.Text = "更新参数成功!"; InitDataTable(); } }