Now let's see an example of DiffGrams. The code is to reads data from Employees tables and write in an XML document in DiffGram format.
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Northwind.mdb"
Dim sql As String = "SELECT EmployeeID, FirstName, LastName, Title FROM Employees"
Dim conn As OleDbConnection = Nothing
Dim ds As DataSet = Nothing
' Create and open connection
conn = New OleDbConnection(connectionString)
If conn.State <> ConnectionState.Open Then
conn.Open()
End If 'Create a data adapter
Dim adapter As New OleDbDataAdapter(sql, conn)
' Create and fill a DataSet
ds = New DataSet("TempDtSet")
adapter.Fill(ds, "DtSet")
' Write XML in DiffGram format
ds.WriteXml("DiffGramFile.xml", XmlWriteMode.DiffGram)
' Close connection
If conn.State = ConnectionState.Open Then
conn.Close()
End If
Now if you update data, you'll see new additions to the XML file with
You can use the ReadXml method to read XML documents in DiffGram format. The first parameter of ReadXml is the XML document name and second parameter should be XmlReadMode.DiffGram. See the following code snippet:
' Create a DataSet Object
Dim ds As New DataSet
' Fill with the data
ds.ReadXml("DiffGramFile.xml", XmlReadMode.DiffGram)
DataRowState members
Added
Add added rows to a DataRowCollection of a DataSet and AcceptChanges has not been called. Deleted
All the deleted rows. Detached Rows were created but not added to the row collection. Either waiting for the addition or have removed from the collection.
Modified
Modified rows and AcceptChanges has not been called.
Unchanged
Unchanged rows since last AcceptChanges was called.
The following code copies only modified rows of ds to new DataSet tempDs.
Dim tempDs As DataSet
tempDs = ds.GetChanges(DataRowState.Modified)
No comments:
Post a Comment