开发者

Error writing to an XML file a dataset with multiple Related Tables and not enforcing constraints

Does any body know how to write an XML from a Dataset that has multiple tables with multiple parent rows?

Scenario: I have five children relations to a table, this makes 5 foreign keys on the parent table; and this parent table has its own parent. All Tables contain data and I want to export it to an XML file.

In order to do this, the code I use is:

DataSet dataSet = dataSource as DataSet;

SaveFileDialog dialog = new SaveFileDialog();
dialog.FileName = dataSet.DataSetName;
dialog.Filter = "XML files (*.xml) | *.xml";

if (dialog.ShowDialog() == DialogResult.OK)
{
    dataSet.EnforceConstraints = false;

    dataSet.WriteXml(dialog.FileName, XmlWriteMode.WriteSchema);
}

Every time I debug and hit the dataSet.WriteXml line I get this error message:

Cannot proceed wi开发者_如何学Pythonth serializing DataTable 'Table_Name'. It contains a DataRow which has multiple parent rows on the same Foreign Key.

Any ideas on why this happens and how to solve it?

Thanks in advance.


Why do you set the EnforceConstraints to false? and did you check setting the relationships correctly ?


The problem was resolved after deleting the data in a table and new data was inserted. Since the DB does not have a constraint and the constraint is upon the dataset's FK relationship, thare is were it failed.

The proper solution is to set up a FK constraint in the database, instead of doing it in the dataset alone.

Thanks guys, your answers guide me through.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜