【发布时间】:2016-01-16 00:00:47
【问题描述】:
这是为大学准备的,所以我不希望有人为我做这件事,只是需要一些帮助来了解该走哪条路。
无论如何,我需要将数据库中的信息导出到 XML 文件中,并遵守 XML Schema 提供的规则。我使用的解决方案不起作用,我不知道是因为它是错误的解决方案还是我错过了一个步骤。
基本上,我创建一个 DataSet 并将我的 XML Schema 加载到其中。 然后我从 DataSet 中获取 DataTableCollection,并用相应的数据填充每个 DataTable。 我遇到的问题是我创建的 XML 文件不遵守关系规则。
代码如下:
/*
there's code here to make the query to the database
basically, it's selecting a stored procedure that returns several
inner joined tables, with different columns data that I'll need to put
in the XML file
*/
SqlDataReader dr = cmd.ExecuteReader();
/*
there's code here to make sure that there's data
*/
DataSet dataSet = new DataSet();
dataSet.ReadXmlSchema("XML_Schema.xsd");
FillTables(dr, dataSet.Tables);
string xml = ds.GetXml();
至于我的 FillTables 代码,我只是从我的 DataTableCollection 中获取每个 DataTable,并向每个 DataTable 添加包含在我的 SqlDataReader 中的相应数据。一个例子是这个:
dt.Rows.Add(new object[] { dr["tt_id"],dr["cod"],dr["ticketState"],dr["ticketDescription"]});
毕竟,我有一个 XML 文件,看起来像这样:
<?xml version="1.0"?>
<NewDataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ticket status="Closed" ticketId="aaa" type="LF1010">
<description>...</description>
</ticket>
<owner email="1" name="zarolho" ownerId="uEmail1">uEmail1</owner>
<supervisor email="aEmail2" name="pp" technicianID="2">2</supervisor>
<type_type name="Login Failed" typeId="LF1010">LF1010</type_type>
<actions/>
<action endDate="14-01-2016 02:31:52" beginDate="14-01-2016 02:31:52" orderNum="1">1</action>
<action endDate="14-01-2016 02:31:52" beginDate="14-01-2016 02:31:52" orderNum="2">2</action>
</NewDataSet>
当它看起来像这样时,基于 Visual Studio 生成的 XML 示例:
<?xml version="1.0" encoding="utf-8"?>
<ticket type="type1" ticketId="ticketId1" status="status1">
<owner ownerId="ownerId1" name="name1" email="email1">owner1</owner>
<supervisor technicianID="technicianID1" name="name1" email="email1">supervisor1</supervisor>
<description>description1</description>
<type_type typeId="typeId1" name="name1">type_type1</type_type>
<actions>
<action orderNum="orderNum1" beginDate="beginDate1" endDate="endDate1">action1</action>
<action orderNum="orderNum2" beginDate="beginDate2" endDate="endDate2">action2</action>
<action orderNum="orderNum3" beginDate="beginDate3" endDate="endDate3">action3</action>
</actions>
</ticket>
问题似乎是我的 DataSet 不尊重表关系,因为我没有正确填充它们...我尝试使用 SqlDataAdapters,但它也不起作用。
我的 SqlDataAdapters 代码如下:
string query_ticket = "select cod as ticketId, ticketState as status, ticketDescription as description, ticketType as type from vi_Ticket where cod=@cod";
SqlDataAdapter adapter = new SqlDataAdapter(query_ticket, con);
adapter.SelectCommand.Parameters.Add(cod);
adapter.Fill(ds,"ticket");
string query_owner = "SELECT anumber as technicianID, name, email FROM dbo.Technician where anumber=2";
SqlDataAdapter adapter2 = new SqlDataAdapter(query_owner, con);
adapter2.Fill(ds, "supervisor");
但它给了我一个 ConstraintException
未能启用约束。一行或多行包含违反>非空、唯一或外键约束的值。
所以我不确定该继续以哪种方式尝试找到解决我的问题的正确方法。
很抱歉,如果我的问题不符合所有规则,如果您指出错误,我会尝试修复它们或改进我的问题。
【问题讨论】:
-
您好。作为起点,这里有一篇关于您要实现的目标的非常好的文章。如果没有定义数据集中每个表之间的关系,ToXML 将无法按您希望的方式工作。 tomasvera.com/programming/…
-
但是,您可能会在数据集中发现父表和子表之间的关系确实存在,但我认为您需要手动将每一行与其父表关联。
dataSet.Tables["action"].Rows[0].SetParentRow(dataSet.Tables["actions"].Rows[0]);