我收到错误,因为“无法更新ID”在更新table.plz帮助我解决这个问题 [英] I am getting error as "cannot update ID" in update table.plz help me resolving this

查看:65
本文介绍了我收到错误,因为“无法更新ID”在更新table.plz帮助我解决这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误,因为更新表中的无法更新ID.plz帮我解决这个问题



我尝试了什么:



private void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e)

{

if(dataGridView1.Columns [e .ColumnIndex] .Name ==提交)

{

使用(OleDbConnection con1 = new OleDbConnection(con))

{

con1.Open();

for(int i = 0; i< dataGridView1.Rows.Count; i ++)

{

OleDbCommand cmd = new OleDbCommand();

cmd.Connection = con1;

// string query1 =(insert into Reports(ID,Comments, mark,Reviewer,ReviewerDate)值(@ id,@ comments,@ marks,'+ Environment.UserName +',Now));

string query1 =(Update Reports set ID = @ id,Comments = @ comments,marks = @ marks,Reviewer = @ rvr,ReviewerDate = @rvrd Where(ID = @id));

cmd.Parameters.AddWithValue(@ id,dataGridView1.Rows [i] .Cells [0] .Value);

cmd.Parameters.AddWithValue(@ comments,dataGridView1.Rows [i ] .Cells [5] .Value);

cmd.Parameters.AddWithValue(@ marks,dataGridView1.Rows [i] .Cells [6] .Value);

cmd.Parameters.AddWithValue(@ rvr,dataGridView1.Rows [i] .Cells [8] .Value);

cmd.Parameters.AddWithValue(@ rvrd,dataGridView1.Rows [i] .Cells [9] .Value);



cmd.CommandText = query1;

cmd.ExecuteNonQuery();



}

MessageBox.Show(提交的评论,消息,MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

I am getting error as "cannot update ID" in update table.plz help me resolving this

What I have tried:

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++)
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con1;
//string query1 = ("insert into Reports (ID,Comments,marks,Reviewer,ReviewerDate) values(@id,@comments,@marks,'" + Environment.UserName + "',Now)");
string query1 = ("Update Reports set ID = @id,Comments = @comments,marks = @marks,Reviewer = @rvr,ReviewerDate = @rvrd Where(ID = @id)");
cmd.Parameters.AddWithValue("@id", dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@comments", dataGridView1.Rows[i].Cells[5].Value);
cmd.Parameters.AddWithValue("@marks", dataGridView1.Rows[i].Cells[6].Value);
cmd.Parameters.AddWithValue("@rvr", dataGridView1.Rows[i].Cells[8].Value);
cmd.Parameters.AddWithValue("@rvrd", dataGridView1.Rows[i].Cells[9].Value);

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

}
MessageBox.Show("Comments Submitted", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

推荐答案

除了内森所说的,想一想:即使你可以更新ID值,有什么意义吗?

In addition to what Nathan says, think about it: even if you could update the ID value, is there any point?
Update Reports set ID = @id,Comments = @comments,marks = @marks,Reviewer = @rvr,ReviewerDate = @rvrd Where(ID = @id)

鉴于WHERE子句确保只有ID设置为特定值的行才会被更改,尝试将ID设置为具体的值有哪些?

Given that the WHERE clause ensures that only rows with the ID set to a specific value will be altered, what effect would trying to set the ID to that specific value have?


因为ID是行标识,所以你不能像这样更改它。修复此行:

Because the ID is the row identity, you're not going to be able to change it like this. Fix this line:
string query1 = ("Update Reports set Comments = @comments,marks = @marks,Reviewer = @rvr,ReviewerDate = @rvrd Where(ID = @id)");


最可能的问题是 ID 很可能是该表的 Identity 列。 />
如果查看实际的SQL语句本身

The most likely problem is that ID is most likely the Identity column for the table.
If you look at the actual SQL statement itself
Update    Reports
set       ID = @id
,         Comments = @comments
,         marks = @marks
,         Reviewer = @rvr
,         ReviewerDate = @rvrd
Where    (ID = @id)



当你考虑逻辑时,你可能会挠头 - 当改变所选行的ID时ID与提供的ID匹配;为什么你需要将值改为已经存在的问题。

答案是你不需要;并且不试图改变它你将不会得到你收到的异常消息。



所以你需要做的就是删除SQL命令的那一部分......


You may scratch your head when you think about the logic-- change the ID on the selected row when the ID matches the one provided; Why would you need to change the value to what it already is becomes the question.
The answer is you don't need to; and by not attempting to change it you will not get the exception message you received.

So all you need to do is remove that portion of the SQL Command...

Update    Reports
set       Comments = @comments
,         marks = @marks
,         Reviewer = @rvr
,         ReviewerDate = @rvrd
Where    (ID = @id)



......和然后你应该好好去。

假设 ID 自然是身份栏


这篇关于我收到错误,因为“无法更新ID”在更新table.plz帮助我解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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