如何将数据或数据集中的数据作为XML参数传递给SQL Server中的存储过程
亲爱的所有
我使用的是VB.NET,我在数据表中有数据。我想将它作为参数传递给存储过程,但是在XML FORMAT中。如何从数据表或数据集传递它?
Dear all
I am using VB.NET, and I have data in a datatable. I want to pass it as a parameter to stored procedure in but in XML FORMAT. How can I pass it from datatable or dataset?
创建自定义格式,然后从数据表(或数据集)创建或使用数据集getXML方法并传递那到数据库。
我建议你自己使用。这将是a)更小的b)更容易维护(不依赖于最终的MS变化)和c)更容易解析(由于是你自己的)。
这种转换所需的类是XmlNode,XmlDocument和XmlAttribute
我使用以下格式:
Either create custom format which you will then create from the datatable (or dataset) or use datasets getXML method and pass that to the database.
I recommend using your own. It will be a) smaller b) easier to maintain (does not depend on eventual MS changes) and c) easier to parse (due to being your own).
Classes you need for such transformations are XmlNode, XmlDocument and XmlAttribute
I used the following format:
<customers>
<customer id="int" name="string" maxlimit="float" address="string" />
<customer id="int" name="string" maxlimit="float" address="string" />
<customer id="int" name="string" maxlimit="float" address="string" />
</customers>
节点中的属性为列,节点是行,外部文档节点是表。
另外,由于SQL使用XML的方式,我没有在客户下放置任何潜在的子节点(比方说你有一个以上的地址 - 你可能会想要这样做
Where attributes in the node are columns, nodes are rows and outer document node is table.
Also, due to way SQL uses XML, I didn't put any potential subnodes under customer (lets say you have more then one address - you might be tempted to do this
<customer id="3" name="st">
<address street="ul" />
<address street="ul2" />
</customer>
不要!而是设置另一个节点< adresses>或者您可以从客户那里获取所有地址并使用PK / FK关系访问它们的地方。相信我,它节省了时间:)
如果这有帮助请花时间接受解决方案。谢谢。
DON'T! Instead, set another node <adresses> or something where you'll put all addresses from the customers and use PK/FK relationship to access them. Trust me, it saves time :)
If this helps please take time to accept the solution. Thank you.