以下代码,如果什么都不做,只是空点一下,就会提示“未将对象引入到对应的实例”,能否优化一下,以增加容错,实现,若是空点该“保存”按钮,就提示,“请您修改后,再做保存”;
private void btnmodify_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=192.168.100.247;database=WHMesInfo;user=sa;password=whyy@2021");
conn.Open();
try
{
// 遍历每一行数据,并进行保存
for (int i = 0; i < resultTable.Rows.Count; i++)
{
if (comprondate.Text == "请选择")//先判断汇总日期是否选择正常日期了,正常再继续执行;
{
MessageBox.Show("请您选择汇总日期");
return;
}
if (resultTable.Rows[i].RowState == DataRowState.Deleted)
{
continue; // 跳过已删除的行
}
else
{
//DateTime date = Convert.ToDateTime(resultTable.Rows[i]["date"]); //标准做法,用数据表名resultTable来定义
//DateTime sumdate = Convert.ToDateTime(comprondate.Text);
string sumdate = comprondate.Text;
string batchNo = Convert.ToString(resultTable.Rows[i]["batchNo"]);
//string probatchNo = Convert.ToString(resultTable.Rows[i]["probatchNo"]);
string proname = Convert.ToString(resultTable.Rows[i]["proname"]);
string unit = Convert.ToString(resultTable.Rows[i]["unit"]);
string teams = Convert.ToString(resultTable.Rows[i]["teams"]);
string process = Convert.ToString(resultTable.Rows[i]["process"]);
string spec = Convert.ToString(resultTable.Rows[i]["spec"]);
decimal ulh = Convert.ToDecimal(resultTable.Rows[i]["ulh"]);
int instock = Convert.ToInt32(resultTable.Rows[i]["instock"]);
int sample = Convert.ToInt32(resultTable.Rows[i]["sample"]);
int checkNum = Convert.ToInt32(resultTable.Rows[i][9]);
decimal redquan;//1217,增加了“折合数量”的计算和数据库字段;
if (dataGridView1.Rows[i].Cells["折合数量"].Value.ToString() != string.Empty)
redquan = Convert.ToDecimal(dataGridView1.Rows[i].Cells["折合数量"].Value);
else
redquan = 0;
decimal uniPrice = Convert.ToDecimal(resultTable.Rows[i][10]);//因之前减少了产品批号列,20231217,将列由11改为了10;;
decimal proSalary = Convert.ToDecimal(resultTable.Rows[i]["产量薪资"]);
decimal proReward, packReward;
if (dataGridView1.Rows[i].Cells["出成奖励"].Value.ToString() != string.Empty)
proReward = Convert.ToDecimal(dataGridView1.Rows[i].Cells["出成奖励"].Value);
else
proReward = 0;
if (dataGridView1.Rows[i].Cells["包材奖励"].Value.ToString() != string.Empty)
packReward = Convert.ToDecimal(dataGridView1.Rows[i].Cells["包材奖励"].Value.ToString());
else
packReward = 0;
decimal toSalary = Convert.ToDecimal(resultTable.Rows[i]["总薪资"]);
decimal dayPrice = Convert.ToDecimal(resultTable.Rows[i]["工日单价"]);
decimal boxWorkhours = Convert.ToDecimal(resultTable.Rows[i]["万盒工时"]);
//以下检验插与数据库内的内容,是否有重复,无重复,便插入;
string str = @"select count(*) from w_protran where dateinfo='" + sumdate.ToString() + "' and batchNo='" + batchNo.ToString() + "' and proname='" + proname.ToString() + "' and teams='" + teams.ToString() + "' and process='" + process.ToString()
+ "'and checkNum='" + checkNum.ToString() + "'and toSalary='" + toSalary.ToString() + "'";
SqlCommand com = new SqlCommand(str, conn);
int intcont = Convert.ToInt32(com.ExecuteScalar());
if (intcont > 0)//判断数据库中是否有相同的内容;
{
MessageBox.Show("对不起!新增内容,和历史记录有重复,请检查确认!");
return;
}
else
{
string strsql = @"INSERT INTO [dbo].[w_protran]
([dateInfo],[batchNo],[proname],[unit],[teams],[process],[spec],[ulh],[instock],[sample],[checkNum],[redquantity],[uniPrice],[proSalary],[proReward],[packReward],[toSalary],[dayPrice],[boxWorkhours])
VALUES (@dateInfo, @batchNo, @proname, @unit, @teams, @process, @spec, @ulh, @instock, @sample,@checkNum,@redquantity, @uniPrice, @proSalary,@proReward,@packReward,@toSalary,@dayPrice,@boxWorkhours)";
using (SqlCommand comm = new SqlCommand(strsql, conn))
{
comm.Parameters.AddWithValue("@dateInfo", sumdate);
comm.Parameters.AddWithValue("@batchNo", batchNo);
//comm.Parameters.AddWithValue("@probatchNo", probatchNo);
comm.Parameters.AddWithValue("@proname", proname);
comm.Parameters.AddWithValue("@unit", unit);
comm.Parameters.AddWithValue("@teams", teams);
comm.Parameters.AddWithValue("@process", process);
comm.Parameters.AddWithValue("@spec", spec);
comm.Parameters.AddWithValue("@ulh", ulh);
comm.Parameters.AddWithValue("@instock", instock);
comm.Parameters.AddWithValue("@sample", sample);
comm.Parameters.AddWithValue("@checkNum", checkNum);
comm.Parameters.AddWithValue("@redquantity", redquan);
comm.Parameters.AddWithValue("@uniPrice", uniPrice);
comm.Parameters.AddWithValue("@proSalary", proSalary);
comm.Parameters.AddWithValue("@proReward", proReward);
comm.Parameters.AddWithValue("@packReward", packReward);
comm.Parameters.AddWithValue("@toSalary", toSalary);
comm.Parameters.AddWithValue("@dayPrice", dayPrice);
comm.Parameters.AddWithValue("@boxWorkhours", boxWorkhours);
int rowsAffected = comm.ExecuteNonQuery();
if (rowsAffected <= 0)
{
MessageBox.Show("数据修改失败!");
return;
}
}
}
}
}
MessageBox.Show("数据修改成功!");
}
catch (Exception ex)
{
MessageBox.Show("数据修改出错:" + ex.Message);
}
finally { conn.Close(); }
}