本文介绍了使用Drag& amp;创建的默认方法删除尝试编辑Primary K.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用VB 2005,并且在我的表单上有药物并丢弃了datagridviews。为插入生成的 默认代码尝试为 自动生成的主键插入值。下面是生成的代码。我把问题代码放在了 周围。我试过删除这个,但似乎没有工作。可能有更多生成的代码我不知道。我可以删除 但我计划对数据源进行大量更新 意味着我每次刷新时都需要编辑大量代码。这是 只是微软的疏忽吗?我似乎VS 2005会检查 如果列是自动递增的,如果它不会尝试在其中插入 值? 我可以在桌子上放置一个设置,以便正确生成 吗?我对ID列的Idenitty规范是肯定的, 为1增量。在我生成的更新语句中也有类似的问题。 自动生成的代码无法更新具有标识的主键 规范设置为yes。我觉得这不应该发生吗?这只是 微软的监督吗? < System.Diagnostics.DebuggerNonUserCodeAttribute(),_ System.ComponentModel.Design.HelpKeywordAttribute(" versusdata.TableAdapter"),_ System.ComponentModel.DataObjectMethodAttribute(Sy stem.ComponentModel.DataObjectMethodType.Insert,true)> ; _ 公共重载可覆盖函数插入(ByVal CompositionID为 整数,ByVal CompositionName为字符串,ByVal CompDesc为字符串,ByVal 密度为双)作为整数 ****** Me.Adapter.InsertCommand.Parameters(0).Value = CType(CompositionID,Integer)****** * 如果(CompositionName没什么)那么 Me.Adapter.InsertCommand.Parameters(1).Value = System.DBNull.Value Else Me.Adapter.InsertCommand.Parameters(1).Value = CType(CompositionName,String) 结束如果 如果(CompDesc什么都没有)那么 Me.Adapter.InsertCommand.Parameters(2).Value = 系统。 DBNull.Value Else Me.Adapter.InsertCommand.Parameters(2).Value = CType(CompDesc,String) 结束如果 Me.Adapter.InsertCommand.Parameters(3).Value = CType(密度,双倍) Dim previousConnectionState As System.Data.Conn ectionState = Me.Adapter.InsertCommand.Connection.State If((Me.Adapter.InsertCommand.Connection.State和 系统。 Data.ConnectionState.Open)_ <> System.Data.ConnectionState.Open)然后 Me.Adapter.InsertCommand.Connection.Open 结束如果 试试 Dim returnValue As Integer = Me.Adapter.InsertCommand.ExecuteNonQuery 返回returnValue 最后 if(previousConnectionState = System.Data.ConnectionState.Closed)然后 Me.Adapter.InsertCommand.Connection.Close 结束如果 结束尝试 结束功能 谢谢, 格雷格 I''m using VB 2005, and have drug and dropped datagridviews onto my form. Thedefault code that is generated for inserts tries to insert a value for theauto generated primary key. Below is the generated code. I put ***** aroundthe problematic code. I''ve tried just removing this and it doesn''t seem towork. There may be more generated code I don''t know about. I could removeit but I am planning on doing a lot of refreshing of the data source whichwould mean I would need to edit a ton of code each time I refreshed. Is thisjust an oversight by microsoft? I would seem that VS 2005 would check to seeif a column is auto incremented and if it was it wouldn''t try to insert avalue into it? Is there a setting I can place on my table so that this is generatedcorrectly? My Idenitty specification for the ID column is seet to yes, witha 1 increment. In my generated Update statments there are similar issues.The auto generated code tires to update a primary key that has IdentitySpecification set to yes. I would think This shouldn''t happen? Is this justan oversight by Microsoft? <System.Diagnostics.DebuggerNonUserCodeAttribute() , _ System.ComponentModel.Design.HelpKeywordAttribute( "vs.data.TableAdapter"), _ System.ComponentModel.DataObjectMethodAttribute(Sy stem.ComponentModel.DataObjectMethodType.Insert, true)> _Public Overloads Overridable Function Insert(ByVal CompositionID AsInteger, ByVal CompositionName As String, ByVal CompDesc As String, ByValDensity As Double) As Integer****** Me.Adapter.InsertCommand.Parameters(0).Value =CType(CompositionID,Integer) *******If (CompositionName Is Nothing) ThenMe.Adapter.InsertCommand.Parameters(1).Value =System.DBNull.ValueElseMe.Adapter.InsertCommand.Parameters(1).Value =CType(CompositionName,String)End IfIf (CompDesc Is Nothing) ThenMe.Adapter.InsertCommand.Parameters(2).Value =System.DBNull.ValueElseMe.Adapter.InsertCommand.Parameters(2).Value =CType(CompDesc,String)End IfMe.Adapter.InsertCommand.Parameters(3).Value =CType(Density,Double)Dim previousConnectionState As System.Data.ConnectionState =Me.Adapter.InsertCommand.Connection.StateIf ((Me.Adapter.InsertCommand.Connection.State AndSystem.Data.ConnectionState.Open) _<> System.Data.ConnectionState.Open) ThenMe.Adapter.InsertCommand.Connection.OpenEnd IfTryDim returnValue As Integer =Me.Adapter.InsertCommand.ExecuteNonQueryReturn returnValueFinallyIf (previousConnectionState =System.Data.ConnectionState.Closed) ThenMe.Adapter.InsertCommand.Connection.CloseEnd IfEnd TryEnd Function Thanks,Greg推荐答案 这篇关于使用Drag& amp;创建的默认方法删除尝试编辑Primary K.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-15 17:43