1

Platform : V4.0, SQL Server 2008, Windows 2003.

Below code throws SqlException on FillMethod:

"Primary Key Violation Error. Insert to the Table Failed".

Not sure why in Fill Method. Its not happening every time. Column1 and Column2 - Composite Key.

Dim ObjDAdapter As SqlDataAdapter
String selectstring = "select * from Table where Column1 = 'A' and Column2 = 'B'"
ObjDAdapter = New SqlDataAdapter(selectString, myConnection)
ObjDAdapter.Fill(Dataset)
4

1 回答 1

1

我有类似的代码来用 SQL 查询中的行填充数据集,它工作正常。看看这是否有帮助:

Dim ObjDAdapter As SqlDataAdapter
String selectstring = "select * from Table where Column1 = 'A' and Column2 = 'B'"
ObjDAdapter = New SqlDataAdapter(selectString, myConnection)
ObjDAdapter.AcceptChangesDuringFill = False
ObjDAdapter.Fill(Dataset)

唯一的变化是将 AcceptChangesDuringFill 设置为 false。

但听起来您创建的本地数据表的主键设置可能与 SQL 表不同。

即,如果您的 SQL 表有两个字段设置为主键,但您的本地表只有一个字段设置作为键,当它尝试将行插入本地表时,您可能会遇到零星的键冲突错误。

于 2012-08-17T14:26:45.970 回答