请教一下,我下面这段代码是一个修改表单,目的是将主表单(workhour)所查询的数据行,一点每行对应的修改按钮,便弹出这个修改表单,修改后的值,更新保存到所对应数据表的对应行,但针对主表的ID不知该怎么处理,我按如下处理后,一点保存,就弹出如下的错误来,请看看是否里面的问题
private void btnupsave_Click(object sender, EventArgs e)
{
string IdValue = workhour.idvalue;
string whudate = dateTimePicker1.Text;
string whubatchNo = whbatchNo.Text;
string whuprocess = comuprocess.Text;
string whuname = comupname.Text;
string whulh = whupulh.Text;
string whnote = whupnote.Text;
//SqlTransaction transaction = conn.BeginTransaction();
try
{
conn = new SqlConnection("server=192.168.100.247;database= Whmesinfo;user=sa;password=whyy@2021 ");
conn.Open();
string SqlString = @"update [dbo].[w_workerhour]
set date=@date,batchNo=@batchNo,process=@process,name=@name,dulh=@dulh,note=@note
where id = @id";
SqlCommand comm = new SqlCommand(SqlString, conn);//需要加上回滚事务;
comm.Parameters.AddWithValue("@id", int.Parse(IdValue));
comm.Parameters.AddWithValue("@date",DateTime.Parse(whudate)); // 替换为实际的日期值,以下相同;
comm.Parameters.AddWithValue("@batchNo", whubatchNo);
comm.Parameters.AddWithValue("@process", whuprocess);
comm.Parameters.AddWithValue("@name", whuname);
comm.Parameters.AddWithValue("@dulh", whulh);
comm.Parameters.AddWithValue("@note", whnote);
comm.ExecuteNonQuery();
//transaction.Commit();
MessageBox.Show("数据保存成功!");
}
catch (Exception)
{
MessageBox.Show("更新失败");
//transaction.rollback();
}
finally { conn.Close(); }
}
目测idValue没有获取到值。
string IdValue = workhour.idvalue; debug看看这个有没有值
没有值,是空的,请问若想得到主表上的这个id,我应该怎么处理呢;
【相关推荐】
数据库自己搞定,就是说我在数据插入的时候,依然不考虑主键的问题,希望继续使用数据库的主键自增,但是很明显,原本默认的主键自增现在没法用了,我们必须有新的方案。
不一定要id,只要是任意能定位到对应记录的字段就行,可以是batchNo,也可以是batchNo+process+name,具体怎样要看你的业务场景和数据库设计