P
Paul Wheeler
I'm having trouble with Merge creating duplicate records, violating primary
key constraints.
I have a DataSet w/ Primary keys set up for each table.
I make some changes.
Merge data from server w/ PreserveChanges = True works fine.
I make some more changes
I write it and its schema out to Xml using DiffGram mode
I restart and read the dataset and it's schema from Xml
Merge data from server w/ PreserveChanges = True or False don't work
The merge adds duplicates of the changed fields, one with the changes,
one without
The problem only occurs when there are outstanding changes when the file is
written to Xml, which makes me think that the Data->Xml->Data conversion is
changing something in the DataSet, but I can't figure out what it is.
Here is a quick sample that exemplifies the problem:
Dim connectstring As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\Microsoft
Office\Office10\Samples\Northwind.mdb;"
Dim selectstring As String = "SELECT * FROM Products"
Dim tablename As String = "Products"
Dim primarykey As String = "ProductID"
Dim xmlfile As String = "data.xml"
Dim xsdfile As String = "data.xsd"
'Load The Database
Dim ds As New DataSet(tablename + "DataSet")
Dim da As New OleDbDataAdapter(selectstring, connectstring)
da.TableMappings.Add("Table", tablename)
da.Fill(ds)
ds.Tables(tablename).PrimaryKey = New DataColumn()
{ds.Tables(tablename).Columns(primarykey)}
'Make A Change
ds.Tables(tablename).Rows(5)("ProductName") = "Something Different"
'Write To XML
ds.WriteXmlSchema(xsdfile)
ds.WriteXml(xmlfile, XmlWriteMode.DiffGram)
'Read From XML
ds = New DataSet(tablename + "DataSet")
ds.ReadXmlSchema(xsdfile)
ds.ReadXml(xmlfile, XmlReadMode.DiffGram)
'Merge from source
Dim tempds As New DataSet(tablename + "Update")
da.Fill(tempds)
Try
ds.Merge(tempds, True)
'This Causes a Constraint Exception becuase duplicate rows are
created
Catch
End Try
Thanks Much,
-Paul Wheeler
key constraints.
I have a DataSet w/ Primary keys set up for each table.
I make some changes.
Merge data from server w/ PreserveChanges = True works fine.
I make some more changes
I write it and its schema out to Xml using DiffGram mode
I restart and read the dataset and it's schema from Xml
Merge data from server w/ PreserveChanges = True or False don't work
The merge adds duplicates of the changed fields, one with the changes,
one without
The problem only occurs when there are outstanding changes when the file is
written to Xml, which makes me think that the Data->Xml->Data conversion is
changing something in the DataSet, but I can't figure out what it is.
Here is a quick sample that exemplifies the problem:
Dim connectstring As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\Microsoft
Office\Office10\Samples\Northwind.mdb;"
Dim selectstring As String = "SELECT * FROM Products"
Dim tablename As String = "Products"
Dim primarykey As String = "ProductID"
Dim xmlfile As String = "data.xml"
Dim xsdfile As String = "data.xsd"
'Load The Database
Dim ds As New DataSet(tablename + "DataSet")
Dim da As New OleDbDataAdapter(selectstring, connectstring)
da.TableMappings.Add("Table", tablename)
da.Fill(ds)
ds.Tables(tablename).PrimaryKey = New DataColumn()
{ds.Tables(tablename).Columns(primarykey)}
'Make A Change
ds.Tables(tablename).Rows(5)("ProductName") = "Something Different"
'Write To XML
ds.WriteXmlSchema(xsdfile)
ds.WriteXml(xmlfile, XmlWriteMode.DiffGram)
'Read From XML
ds = New DataSet(tablename + "DataSet")
ds.ReadXmlSchema(xsdfile)
ds.ReadXml(xmlfile, XmlReadMode.DiffGram)
'Merge from source
Dim tempds As New DataSet(tablename + "Update")
da.Fill(tempds)
Try
ds.Merge(tempds, True)
'This Causes a Constraint Exception becuase duplicate rows are
created
Catch
End Try
Thanks Much,
-Paul Wheeler