</pre>出现此情况的可能是数据库没有设置有效的主键,添加有效的主键。</p><p></p><p></p><pre name="code" class="csharp"><span style="font-size:18px;"> this.Validate();
this.绑定表单BindingSource.EndEdit();
this.绑定表单TableAdapter.Update(this.ExDataSet.绑定表单);
this.绑定表单TableAdapter.Fill(this.ExDataSet.绑定表单);//如果没有主键,代码会出现上述错误,添加后正常</span>
利用SqlCommandBuilder来配置数据适配器,进而来更新数据
<span style="font-size:18px;">SqlAdapter adapt=new SqlAdapter(str,conn);
SqlCommandBuider builder=new SqlCommandBuilder(adapt);</span>
SqlCommandBuilder利用最初的selectcommand自动生成包含在SqlDataAdapter的InsertCommand ,UpdateCommand 和DeleteCommand属性中的值。不许用手动构建SqlCommand 和Sqlparameter的类型。