本文介绍了更新表,但为什么它给出插入错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在更新数据适配器时遇到错误。 违反PRIMARY KEY约束'PK__Compalint__62D5F22C'。无法在对象'Compalint'中插入重复键。语句已被终止。 i我更新表但为什么会出现插入错误? 如果我做错了,请告诉我.. :( i代码如下: - I am getting below error while updating the data adapter." Violation of PRIMARY KEY constraint 'PK__Compalint__62D5F22C'. Cannot insert duplicate key in object 'Compalint'. The statement has been terminated. "i am updating table but why it is giving insertion error ?Let me know if i am doing something wrong.. :(i used below code:-Dim Da As New SqlDataAdapter("select userid,loginid from compalint", conn) Dim cmdbld As New SqlCommandBuilder(Da) cmdbld.GetUpdateCommand() Da.Update(ds1, "table") 和我的ds1包含两个字段userid,loginid only ... 提前感谢... and my ds1 contain two fields userid,loginid only...thanks in advance...推荐答案 检查更新命令我认为它想要更改或插入PrimaryKey列。改进更新命令。 或 您需要检查SQL Server中表的约束。删除不必要的约束。 或 首次生成Transact-SQL语句后,如果应用程序以任何方式更改语句,则必须显式调用RefreshSchema。否则,GetUpdateCommand仍将使用前一个语句中的信息,这可能不正确。当应用程序调用Update或GetUpdateCommand时,首先生成Transact-SQL语句。 ( MSDN中的更多信息) 另外,看看这个:使用CommandBuilders生成命令(ADO.NET) Check the Update Command I think it wants to change or insert a PrimaryKey column. Improve the Update Command.OrYou need to check the constraints of the table in SQL Server. Remove the unnecessarily constraints.OrAfter the Transact-SQL statement is first generated, the application must explicitly call RefreshSchema if it changes the statement in any way. Otherwise, the GetUpdateCommand will still be using information from the previous statement, which might not be correct. The Transact-SQL statements are first generated when the application calls either Update or GetUpdateCommand. (More information in MSDN)And also, look at this: Generating Commands with CommandBuilders (ADO.NET) 这是一个解决方案(让我们使用DataTable代替DataSet) 1.让dt1拥有xml数据 从DB创建dt2 Here is a solution (lets use DataTable instead of DataSet)1. Let dt1 have xml dataCreate dt2 from DBDim Da As New SqlDataAdapter("select userid,loginid from compalint", conn) Dim cmdbld As New SqlCommandBuilder(Da) Dim dt2 As DataTable = New DataTable Da.Fill(dt2) 3.现在你需要遍历dt1中的每一行并更新dt2。完成更新dt2后,调用udpate命令.. 3. Now you need to go through each row in dt1 and update dt2. Once you have finished updating dt2 call the udpate command..Dim foundRows As DataRow()For Each dRow As DataRow In dt1.Rows 'search userid in Dt2 foundRows = dt2.Select("userid = '" & dRow("UserId") & "'") 'go thru each row in foundrows and update the value For Each dFoundRow As DataRow In foundRows dFoundRow("LoginId") = dRow("LoginId") NextNextdt2.AcceptChanges()cmdbld.GetUpdateCommand()Da.Update(dt2, "table") You need to update the value in dataset so that the 'RowState' property is set to UPDATE mode.. then only it will update.. that's y I am asking the code where you update the dataset. You are saying Dataset is loaded from xml but you are trying to update the table.. DS1 should be taken from Complaint table for update to work.. otherwise it will be inserting only.. 这篇关于更新表,但为什么它给出插入错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 11:12