<?xml version="1.0" encoding="UTF-8"?>
//Read the external file
XmlTextReader xtr = new XmlTextReader(inputFileName);
XmlDocument xd = new XmlDocument();
xd.Load(xtr);
xtr.Close();
//Work with the document after retrieving its top node
XmlNode xnod = xd.DocumentElement;
....
//Write the document
XmlTextWriter xtw =
new XmlTextWriter(outputFileName, System.Text.Endcoding.UTF8);
xd.WriteTo(xtw);
xtw.Flush();
xtw.Close();
SqlDataAdapter da = new SqlDataAdapter(
"Select * from jobs",
"server=localhost;uid=sa;database=pubs");
SqlCommandBuilder bld = new SqlCommandBuilder(da);
da.InsertCommand = bld.GetInsertCommand();
But if you do this (in order to supply your own version of
the command),  you must inform the
CommandBuilder by commanding
bld.RefreshSchema();
OleDbConnection conn = new OleDbConnection("....");
//Outer transaction is based on the Connection
OleDbTransaction outerTrans = conn.BeginTransaction();
//Command needs to know about both the connection and the transaction
OleDbCommand outerCmd = new OleDbCommand("update....", conn, outerTrans);
....
//Inner transaction is based on the outer transaction, not the connection
OleDbTransaction innerTrans = outerTrans.Begin();
//Command needs to know about both the connection and the transaction
OleDbCommand innerCmd = new OleDbCommand("update....", conn, innerTrans);
....
//Roll back inner transaction
innerTrans.Rollback();
....
//Commit outer transaction
outerTrans.Commit();
SqlTransaction sqlTrans = conn.BeginTransaction();
....
command.Transaction = sqlTrans;
....
sqlTrans.Save("savepoint");
....
sqlTrans.Rollback("savepoint");
DataSet GetChanges(DataRowState);
Now, pay attention: it's the DataRowState,
not the DataViewRowState.
I believe this is one of the few
times you will actually use DataRowState in your programming,
as opposed to DataViewRowState.
The enumerations for both of these items are given below.
//Assuming a DataTable dt, with column "id"
DataColumn[] adc = new DataColumn[1];
adc[0] = dt.Columns["id"];
dt.PrimaryKey = adc;
| DataRowState | Description |
|---|---|
| Unchanged | No changes have been made since the last call to AcceptChanges or since the row was created by DataAdapter.Fill. |
| Added | The row has been added to the table, but AcceptChanges has not been called. |
| Modified | Some element of the row has been changed. |
| Deleted | The row has been deleted from a table and AcceptChanges has not been called. |
| Detached | Detached is set for a row that has been created but is not
part of any DataRowCollection. The RowState of a newly created row is set to Detached. After the new DataRow is added to the DataRowCollection by calling the Add method, the value of the RowState property is set to Added. Detached is also set for a row that has been removed from a DataRowCollection using the Remove method, or by the Delete method followed by the AcceptChanges method. |
| DataRowVersion | Description |
|---|---|
| Current | The row contains current values. |
| Default | The default version for the current DataRowState. For a DataRowState value of Added, Modified or Current, the default version is Current. For a DataRowState of Deleted, the version is Original. For a DataRowState value of Detached, the version is Proposed. |
| Original | The row contains its original values. |
| Proposed | The row contains a proposed value. |
DataRow custRow = custTable.Rows[0];
string custID
= custRow["CustomerID", DataRowVersion.Original].ToString();
(I can't find a way of retrieving the entire row according to its
Version. The DataTable.Rows collection doesn't seem
to have an indexer that includes a Version specification.)
//Have a "Products" table in DataSet ds. Tell the DefaultView
//which column is the sort key, which will be used for "Find".
//We don't have to create this DefaultView first.
ds.Tables["Products"].DefaultView.Sort = "ProductID";
//Find a row to change, using the Default View. The integer
//returned is the index in the original table, not in the view
int i = ds.Tables["Products"].DefaultView.Find("123");
//Change the value of the column in the row found by DefaultView.Find--
//but "DefaultView" does not appear in this next statement
ds.Tables["Products"].Rows[i]["Color"] = "mediumblue";
ds.Tables["Products"].DefaultView.RowStateFilter
== DataViewRowState.ModifiedCurrent;
The following table, lifted from the on-line Help,
shows the options for DataViewRowState. Uh-oh.
| DataViewRowState | Description |
|---|---|
| CurrentRows | The Current row version of all Unchanged,
Added, and Modified rows. This is the default. |
| Added | The Current row version of all Added rows. |
| Deleted | The Original row version of all Deleted rows. |
| ModifiedCurrent | The Current row version of all Modified rows. |
| ModifiedOriginal | The Original row version of all Modified rows. |
| None | No rows. |
| OriginalRows | The Original row version of all Unchanged, Modified, and Deleted rows. |
| Unchanged | The Current row version of all Unchanged rows. |
| XmlReadMode | Description |
|---|---|
| Auto | Default. Performs the most appropriate of these actions:
If the data is a DiffGram, sets XmlReadMode to DiffGram. -or- If the dataset already has a schema, or the document contains an in-line schema, sets XmlReadMode to ReadSchema. -or- If the dataset does not already have a schema and the document does not contain an in-line schema, sets XmlReadMode to InferSchema. |
| DiffGram | Reads a DiffGram, applying changes from the DiffGram to the
DataSet. The semantics are identical to those of a
Merge operation. As with the Merge operation,
RowState values are preserved.
Input to ReadXml with DiffGrams should only be obtained
using the output from WriteXml as a DiffGram.
The target DataSet must have the same schema as the DataSet on which WriteXml as DiffGram is called. Otherwise the DiffGram merge operation fails, and an exception is thrown. |
| Fragment | Reads XML documents, such as those generated by executing FOR XML queries, against an instance of SQL Server. When XmlReadMode is set to Fragment, the default namespace is read as the inline schema. |
| IgnoreSchema | Ignores any inline schema and reads data into the existing DataSet schema. If any data does not match the existing schema, it is discarded (including data from differing namespaces defined for the DataSet). If the data is a DiffGram, IgnoreSchema has the same functionality as DiffGram. |
| InferSchema | Ignores any inline schema, infers schema from the data and loads the data. If the DataSet already contains a schema, the current schema is extended by adding new tables or adding columns to existing tables. An exception is thrown if the inferred table already exists but with a different namespace, or if any of the inferred columns conflict with existing columns. |
| ReadSchema | Reads any inline schema and loads the data. If the DataSet already contains schema, new tables may be added to the schema, but an exception is thrown if any tables in the inline schema already exist in the DataSet. |
| XmlWriteMode | Description |
|---|---|
| DiffGram | Writes the entire DataSet as a DiffGram, including original and current values. To generate a DiffGram containing only changed values, call GetChanges, and then call WriteXml as a DiffGram on the returned DataSet.... When a DataSet is written as a DiffGram, it populates the DiffGram with all the necessary information to accurately recreate the contents, though not the schema, of the DataSet, including column values from both the Original and Current row versions, row error information, and row order. |
| IgnoreSchema | Writes the current contents of the DataSet as XML data, without an XSD schema. If no data is loaded into the DataSet, nothing is written. This is the default. |
| WriteSchema | Writes the current contents of the DataSet as XML data with the relational structure as inline XSD schema. If the DataSet has only a schema with no data, only the inline schema is written. If the DataSet does not have a current schema, nothing is written. [Careful. If there's no schema, you still get some XML, though you don't get a schema. It's not true that absolutely nothing is written.] |
dataSet.WriteXML("XMLOutputFileName", XmlWriteMode.WriteSchema);
which writes out the schema and
the XML data in the same file.| MappingType | Description |
|---|---|
| Attribute | The column is mapped to an XML attribute. |
| Element | The column is mapped to an XML element. |
| Hidden | The column is mapped to an internal structure. |
| SimpleContent | The column is mapped to an XmlText node. |