更新日期和时间所有行的时间? [英] Updating date & time for all the rows?

查看:80
本文介绍了更新日期和时间所有行的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新日期&所有行的时间?



我尝试过:



修改评论。已删除

Updating Date & time for all the Rows?

What I have tried:

Modify the comment. Deleted

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Submit")
{

using (OleDbConnection con1 = new OleDbConnection(con))
{
con1.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
cmd = new OleDbCommand();
cmd.Connection = con1;
string query1 = "Update Spldetails set ID = @id,Status = @sts,Comment =@cmnt,Approvedhrs =@aphr,Reviewer = ('" + label5.Text + "'),Revieweddate = ('"+dateTimePicker3.Value+"') where ID =@id";
cmd.Parameters.AddWithValue("@id", dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@sts", dataGridView1.Rows[i].Cells[11].Value);
cmd.Parameters.AddWithValue("@cmnt", dataGridView1.Rows[i].Cells[12].Value);
cmd.Parameters.AddWithValue("@aphr", dataGridView1.Rows[i].Cells[13].Value);
cmd.Parameters.AddWithValue("('" + label5.Text + "')", dataGridView1.Rows[i].Cells[14].Value);
cmd.Parameters.AddWithValue("('" + dateTimePicker3.Value + "')", dataGridView1.Rows[i].Cells[15].Value);

cmd.CommandText = query1;
cmd.ExecuteNonQuery();

//spldetailsBindingSource.EndEdit();
//spldetailsTableAdapter.Update(updatedata);

}
MessageBox.Show("Your Details have been Submitted successfully....!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}



它正在工作,但我正在更新一行&它更新了Reviewer&审核所有行的日期。



有人可以解决我的问题先生吗?。


its working but i am updating for one row & it has updated Reviewer & revieweddate for all the rows.

can anyone please solve my problem sir?.

推荐答案

如果你只更新一行。你不应该首先使用for循环。它会更新数据库中GridView1上所有行的值。

尝试删除循环。
If you are updating only one row. You should not have the for loop in the first place. It updates values on your database for all the rows on the GridView1.
Try removing the loop.


SQL没有充分理由做事:和你的WHERE子句专门将更新的行限制为只有ID列与给定值匹配的行。

话虽如此,有些事情我不喜欢这个查询:

1)您显然知道参数化查询,那么为什么要将它们与字符串连接混合使用?只需使用参数化查询 - 它更安全,它使查询更具可读性。

2)你为什么要这样做:

SQL does not do things without good reason: and your WHERE clause specifically limits the rows that are updated to only those where the ID column matches the given value.
Having said that, there are some things I don't like about this query:
1) You obviously know about parameterised queries, so why are you mixing them in with string concatenation? Just use parameterised queries throughout - it's a lot safer and it makes the query more readable.
2) Why are you doing this at all:
cmd.Parameters.AddWithValue("('" + label5.Text + "')", dataGridView1.Rows[i].Cells[14].Value);
cmd.Parameters.AddWithValue("('" + dateTimePicker3.Value + "')", dataGridView1.Rows[i].Cells[15].Value);

这些根本不是参数!

3)为什么要将ID值设置为已经存在的值?既然你只修改了值已经等于@ID的行,为什么要再次将它设置为@ID?



我首先要找出 - 究竟是什么 - 在你的标签中,并找到一种通过参数传递它的方法:完全有可能导致你注意到的问题。使用调试器绝对确定!

摆脱最后两个参数,清理查询,看看标签中的内容。

Those aren't parameters at all!
3) Why are you setting the ID value to the value it already is? Since you only modify rows where the value is already equal to @ID, why set it to @ID again?

I'd start by finding out what - exactly - is in your label, and finding a way to pass that via a parameter: it's entirely possible that is what is causing the problem you have noticed. Use the debugger to make absolutely certain!
Get rid of the last two "parameters", clean up your query, and see what is in the label.


这篇关于更新日期和时间所有行的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆