更新查询不起作用。 [英] Update query not working.

查看:118
本文介绍了更新查询不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表单中的一个文本框和一个命令按钮。在这里我添加Leave_Type(EL / CL)。假设我不得不添加ELE而不是EL。如何编辑。

数据库字段离开。(主键)

datatypr- varchar

i使用下面的代码,但显示错误 -



违反PRIMARY KEY约束'  PK_Leaves_Type'。无法在  object  ' <中插入重复键 span class =code-string> dbo.Leaves_Type'。该声明已被终止。





  private   void  CmdUpdate_Click( object  sender,EventArgs e)
{
SqlConnection conn = new SqlConnection( @ 数据源.... ..);
conn.Open();
SqlCommand comm = new SqlCommand( update Leaves_Type set Leaves = @ Leaves);
comm.Parameters.AddWithValue(
@Leaves ,txtltype.Text);
comm.Connection = conn;
comm.ExecuteNonQuery();
MessageBox.Show(
成功更新 );
conn.Close();
}





我应该再使用一个字段用于where条件。

解决方案

如果你使用了主键,那么你不能将重复值插入该colunmn



主键不允许NULL值和重复值。如果要插入重复值,则必须删除主键约束


检查Leave_Type表。我认为Leaves设置为PK列,你试图设置更多具有相同值的行(没有应用where子句)


那么......你的更新 工作,或 尝试 来。

只是你试图将表中的每一行更新为相同的值!

(认为自己很幸运,SQL不想这样做,因为它违反了您的主键规则,或者你会有一个完整相同条目的表 - 而且无法撤消它......)



尝试向SQL添加WHERE子句以将其限制为特定行:

 SqlCommand comm =  new  SqlCommand(  UPDATE Leaves_Type SET Leaves = @ Leaves WHERE MyIdentifyingColumn = @ ID); 
comm.Parameters.AddWithValue( @ Leaves,txtltype.Text);
comm.Parameters.AddWithValue( @ ID,myIdentifingValue);


one textbox and one command button in form. Here i adding Leave_Type(EL/CL). suppose wrongly i have to added ELE instead of EL . how to edit .
database field-leaves.(primary key)
datatypr- varchar
i using below code but its showing error-

Violation of PRIMARY KEY constraint 'PK_Leaves_Type'. Cannot insert duplicate key in object 'dbo.Leaves_Type'. The statement has been terminated.



private void CmdUpdate_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(@"Data Source......");
    conn.Open();
    SqlCommand comm = new SqlCommand("update Leaves_Type set Leaves=@Leaves);
	comm.Parameters.AddWithValue("@Leaves",txtltype.Text);    
    comm.Connection = conn;
    comm.ExecuteNonQuery();
    MessageBox.Show("Successfully Updated");
    conn.Close(); 
}



should i use one more field for where condition .

解决方案

if you used Primary key for the column ,you can't insert the duplicate value to that colunmn

The Primary key won't allow NULL values and Duplicate Values.If you want to Insert duplicate value you must Drop the Primary key constraints


Check the Leave_Type table. I think Leaves is set as PK column and you are trying to set more rows with same value(There is no where clause applied)


Well...your update is working, or it's trying to.
It's just that you are trying to update every single row in your table to the same value!
(Think yourself lucky that SQL didn't want to do that because it violates your Primary Key rules or you would have a table full of identical entries - and no way to undo it...)

Try adding a WHERE clause to your SQL to restrict it to a specific row:

SqlCommand comm = new SqlCommand("UPDATE Leaves_Type SET Leaves=@Leaves WHERE MyIdentifyingColumn=@ID");
comm.Parameters.AddWithValue("@Leaves",txtltype.Text); 
comm.Parameters.AddWithValue("@ID", myIdentifingValue);


这篇关于更新查询不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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