这是为了大学,所以我不想有人只为我做,只是需要一些帮助,以了解方向。
无论如何,我需要将信息从我的数据库导出到XML文件中,并遵守XML提供的规则。我提出的解决方案是行不通的,我不知道是因为这是错误的解决方案,还是我错过了一步。
基本上,我创建了一个DataSet并将XML加载到其中。然后我从DataTableCollection中获取DataSet,并用各自的数据填充每个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中添加包含在DataTable中的相应数据。一个例子是:
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生成的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
无法启用约束。一个或多个行包含违反>非空、唯一或外键约束的值.
所以我不知道该怎么做才能找到正确的方法来解决我的问题。
对不起,如果我的问题没有遵循每一条规则,如果你指出错误,我会尽力纠正或改进我的问题。
发布于 2016-01-15 16:00:47
正如T凯利所说,问题是,当我填充DataTables时,我没有建立表之间的关系。
解决方案非常简单,我只需将id值分配给父级,并且每个孩子也必须使用该id。
For example, being tickets the parent table
dt.Rows.Add(new object[] { dr["tt_id"],dr["cod"],dr["ticketState"],dr["ticketDescription"],id});
and for a child of tickets, like owners, I would have to use that same id in the end, so that the relationship was estabilished.谢谢你们的帮助。
https://stackoverflow.com/questions/34783522
复制相似问题