/// <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 dataGridViewMgt_CellClick(object sender, DataGridViewCellEventArgs e) { try { AccessHelper helper = new AccessHelper(); DataTable dt; int Month = Convert.ToInt32(dataGridViewMgt.CurrentRow.Cells["month"].Value.ToString()); int Day = Convert.ToInt32(dataGridViewMgt.CurrentRow.Cells["day"].Value.ToString()); int MGT_OP = GetMGT_OPByName(dataGridViewMgt.CurrentRow.Cells["operation"].Value.ToString()); if (MGT_OP == 1) //种植 { tabControlMgt.SelectedIndex = 0; string sql = @"select MONTH,DAY,MGT_OP,HEATUNITS,PLANT_ID,CURYR_MAT,LAI_INIT,BIO_INIT,HI_TARG,BIO_TARG,CNOP from mgt2 WHERE SUBBASIN = " + subbasinId + " AND HRU = 1 " + "AND MONTH = " + Month + " AND DAY = " + Day + " AND MGT_OP = " + MGT_OP; dt = helper.GetDataTableFromDB(sql); if (dt != null) { PLANT_MONTH.Text = dt.Rows[0][0].ToString(); PLANT_DAY.Text = dt.Rows[0][1].ToString(); PLANT_ID.Text = dt.Rows[0][4].ToString() + "," + getPlantNameByID(Convert.ToInt32(dt.Rows[0][4].ToString())); CURYR_MAT.Text = dt.Rows[0][5].ToString(); PLANT_HEAT_UNITS.Text = dt.Rows[0][3].ToString(); BIO_INIT.Text = dt.Rows[0][7].ToString(); HI_TARG.Text = dt.Rows[0][8].ToString(); LAI_INIT.Text = dt.Rows[0][6].ToString(); BIO_TARG.Text = dt.Rows[0][9].ToString(); PLANT_CNOP.Text = dt.Rows[0][10].ToString(); } } else if (MGT_OP == 2) { tabControlMgt.SelectedIndex = 1; string sql = @"select MONTH,DAY,MGT_OP,IRR_AMT,IRR_SALT,IRR_EFM,IRR_SQ,IRR_SC,IRR_NO from mgt2 WHERE SUBBASIN = " + subbasinId + " AND HRU = 1 " + "AND MONTH = " + Month + " AND DAY = " + Day + " AND MGT_OP = " + MGT_OP; dt = helper.GetDataTableFromDB(sql); if (dt != null) { IRR_MONTH.Text = dt.Rows[0][0].ToString(); IRR_DAY.Text = dt.Rows[0][1].ToString(); IRR_AMT.Text = dt.Rows[0][3].ToString(); IRR_SALT.Text = dt.Rows[0][4].ToString(); IRR_EFM.Text = dt.Rows[0][5].ToString(); IRR_SQ.Text = dt.Rows[0][6].ToString(); IRR_SC.Text = dt.Rows[0][7].ToString(); IRR_NO.Text = dt.Rows[0][8].ToString(); } } else if (MGT_OP == 3) { tabControlMgt.SelectedIndex = 2; string sql = @"select MONTH,DAY,MGT_OP,FERT_ID,FRT_KG,FRT_SURFACE from mgt2 WHERE SUBBASIN = " + subbasinId + " AND HRU = 1 " + "AND MONTH = " + Month + " AND DAY = " + Day + " AND MGT_OP = " + MGT_OP; dt = helper.GetDataTableFromDB(sql); if (dt != null) { FRT_MONTH.Text = dt.Rows[0][0].ToString(); FRT_DAY.Text = dt.Rows[0][1].ToString(); FERT_ID.Text = dt.Rows[0][3].ToString() + "," + getFertNameByID(Convert.ToInt32(dt.Rows[0][3].ToString())); FRT_KG.Text = dt.Rows[0][4].ToString(); FRT_SURFACE.Text = dt.Rows[0][5].ToString(); } } else if (MGT_OP == 5) { tabControlMgt.SelectedIndex = 3; string sql = @"select MONTH,DAY,MGT_OP,CNOP from mgt2 WHERE SUBBASIN = " + subbasinId + " AND HRU = 1 " + "AND MONTH = " + Month + " AND DAY = " + Day + " AND MGT_OP = " + MGT_OP; dt = helper.GetDataTableFromDB(sql); if (dt != null) { HARV_MONTH.Text = dt.Rows[0][0].ToString(); HARV_DAY.Text = dt.Rows[0][1].ToString(); HARV_CNOP.Text = dt.Rows[0][3].ToString(); } } else { MessageBox.Show("错误"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 保存编辑 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSaveEdit_Click(object sender, EventArgs e) { helper = new AccessHelper(); string sql = "select MGT_OP,MONTH,DAY from mgt2 where SUBBASIN =" + subbasinId + " AND HRU = 1;"; DataTable dt_c = helper.GetDataTableFromDB(sql); DataTable dt_show = new DataTable(); DataRow dr = dt_show.NewRow(); string str = ""; foreach (DataRow dr_c in dt_c.Rows) { switch (Convert.ToInt32(dr_c[0].ToString())) { case 1: //获取mgt2表中的信息 //以每个子流域的第一个HRU为代表 sql = "select MONTH,DAY,HUSC,MGT_OP,PLANT_ID,CURYR_MAT,HEATUNITS,LAI_INIT,HI_TARG,BIO_INIT,BIO_TARG,CNOP from mgt2 where SUBBASIN =" + subbasinId + " and hru = 1 and MGT_OP=1 and MONTH=" + dr_c[1] + " and DAY=" + dr_c[2] + ";"; dt_show = helper.GetDataTableFromDB(sql); str += " "; str += dt_show.Rows[0].ItemArray[0].ToString().PadLeft(2, ' '); str += " "; str += dt_show.Rows[0].ItemArray[1].ToString().PadLeft(2, ' '); str += " "; str += string.Format("{0:0000.000}", Convert.ToDouble(dt_show.Rows[0].ItemArray[2])).PadLeft(8, ' '); str += " "; str += dt_show.Rows[0].ItemArray[3].ToString().PadLeft(2, ' '); str += " "; str += dt_show.Rows[0].ItemArray[4].ToString().PadLeft(4, ' '); str += " "; str += dt_show.Rows[0].ItemArray[5].ToString().PadLeft(2, ' '); str += " "; str += string.Format("{0:000000.00000}", Convert.ToDouble(dt_show.Rows[0].ItemArray[6])).PadLeft(12, ' '); str += " "; str += string.Format("{0:000.00}", Convert.ToDouble(dt_show.Rows[0].ItemArray[7])).PadLeft(6, ' '); str += " "; str += string.Format("{0:00000.00000}", Convert.ToDouble(dt_show.Rows[0].ItemArray[8])).PadLeft(11, ' '); str += " "; str += string.Format("{0:0.00}", Convert.ToDouble(dt_show.Rows[0].ItemArray[9])).PadLeft(4, ' '); str += " "; str += string.Format("{0:00000.00}", Convert.ToDouble(dt_show.Rows[0].ItemArray[10])).PadLeft(6, ' '); str += " "; str += string.Format("{0:00.00}", Convert.ToDouble(dt_show.Rows[0].ItemArray[11])).PadLeft(5, ' '); str += " "; str += "\r\n"; break; case 2: sql = "select MONTH,DAY,HUSC,MGT_OP,IRR_SC,IRR_NO,IRR_AMT,IRR_SALT,IRR_EFM,IRR_SQ from mgt2 where SUBBASIN =" + subbasinId + " and hru = 1 and MGT_OP=2 and MONTH=" + dr_c[1] + " and DAY=" + dr_c[2] + ";"; dt_show = helper.GetDataTableFromDB(sql); str += " "; str += dt_show.Rows[0].ItemArray[0].ToString().PadLeft(2, ' '); str += " "; str += dt_show.Rows[0].ItemArray[1].ToString().PadLeft(2, ' '); str += " "; str += string.Format("{0:0000.000}", Convert.ToDouble(dt_show.Rows[0].ItemArray[2])).PadLeft(8, ' '); str += " "; str += dt_show.Rows[0].ItemArray[3].ToString().PadLeft(2, ' '); str += " "; str += dt_show.Rows[0].ItemArray[4].ToString().PadLeft(3, ' '); str += " "; str += dt_show.Rows[0].ItemArray[5].ToString().PadLeft(2, ' '); str += " "; str += string.Format("{0:000000.00000}", Convert.ToDouble(dt_show.Rows[0].ItemArray[6])).PadLeft(12, ' '); str += " "; str += string.Format("{0:000.00}", Convert.ToDouble(dt_show.Rows[0].ItemArray[7])).PadLeft(6, ' '); str += " "; str += string.Format("{0:00000.00000}", Convert.ToDouble(dt_show.Rows[0].ItemArray[8])).PadLeft(11, ' '); str += " "; str += string.Format("{0:000.00}", Convert.ToDouble(dt_show.Rows[0].ItemArray[9])).PadLeft(4, ' '); str += " "; str += "\r\n"; break; case 3: sql = "select MONTH,DAY,HUSC,MGT_OP,FERT_ID,FRT_KG,FRT_SURFACE from mgt2 where SUBBASIN =" + subbasinId + " and hru = 1 and MGT_OP=3 and MONTH=" + dr_c[1] + " and DAY=" + dr_c[2] + ";"; dt_show = helper.GetDataTableFromDB(sql); str += " "; str += dt_show.Rows[0].ItemArray[0].ToString().PadLeft(2, ' '); str += " "; str += dt_show.Rows[0].ItemArray[1].ToString().PadLeft(2, ' '); str += " "; str += string.Format("{0:0000.000}", Convert.ToDouble(dt_show.Rows[0].ItemArray[2])).PadLeft(8, ' '); str += " "; str += dt_show.Rows[0].ItemArray[3].ToString().PadLeft(2, ' '); str += " "; str += dt_show.Rows[0].ItemArray[4].ToString().PadLeft(4, ' '); str += " "; str += string.Format("{0:000000.00000}", Convert.ToDouble(dt_show.Rows[0].ItemArray[5])).PadLeft(12, ' '); str += " "; str += string.Format("{0:000.00}", Convert.ToDouble(dt_show.Rows[0].ItemArray[6])).PadLeft(6, ' '); str += " "; str += "\r\n"; break; case 5: sql = "select MONTH,DAY,HUSC,MGT_OP,CNOP,HI_OVR,FRAC_HARVK from mgt2 where SUBBASIN =" + subbasinId + " and hru = 1 and MGT_OP=5 and MONTH=" + dr_c[1] + " and DAY=" + dr_c[2] + ";"; dt_show = helper.GetDataTableFromDB(sql); str += " "; str += dt_show.Rows[0].ItemArray[0].ToString().PadLeft(2, ' '); str += " "; str += dt_show.Rows[0].ItemArray[1].ToString().PadLeft(2, ' '); str += " "; str += string.Format("{0:0000.000}", Convert.ToDouble(dt_show.Rows[0].ItemArray[2])).PadLeft(8, ' '); str += " "; str += dt_show.Rows[0].ItemArray[3].ToString().PadLeft(2, ' '); str += " "; str += string.Format("{0:000000.00000}", Convert.ToDouble(dt_show.Rows[0].ItemArray[4])).PadLeft(12, ' '); str += " "; str += string.Format("{0:000.00}", Convert.ToDouble(dt_show.Rows[0].ItemArray[5])).PadLeft(6, ' '); str += " "; str += string.Format("{0:00.00}", Convert.ToDouble(dt_show.Rows[0].ItemArray[6])).PadLeft(5, ' '); str += " "; str += "\r\n"; break; } } string way = Application.StartupPath + @"\TxtInOut"; DirectoryInfo folder = new DirectoryInfo(way); foreach (FileInfo file in folder.GetFiles(subbasinId.ToString().PadLeft(5, '0') + "*.mgt")) //遍历所有指定的mgt文件 { int rows = find(Application.StartupPath + @"\TxtInOut\" + file.Name, "Operation Schedule:"); //查找指定字符串"Operation Schedule:"所在行数 if (rows == 0) { MessageBox.Show(file.Name + "文件格式错误!"); } var lines = File.ReadAllLines(Application.StartupPath + @"\TxtInOut\" + file.Name); //读取文件的所有行 var query = lines.Take(rows).ToArray(); //读取"Operation Schedule:"之前(包含)的字符串 File.WriteAllLines(Application.StartupPath + @"\TxtInOut\" + file.Name, query); //重写 using (StreamWriter sw = new StreamWriter(Application.StartupPath + @"\TxtInOut\" + file.Name, true)) { sw.WriteLine(str); } } MessageBox.Show("保存编辑完成!"); }