将数据从XML传输到SQL

时间:2010-10-08 17:39:03

标签: c# .net sql xml

我创建了一个XML文件..... 节点:

<Patient>Kanna</Patient>
<Id>12</Id>

我将这些值传递给SQL Db。使用C#.Net

现在我在相同的XML文件中添加了一个更多的值

<Patient>Kanna</Patient>
<Id>12</Id>
<Patient>Raja</Patient>
<Id>13</Id>

问题是:如何将新值附加到SQL Db ....

你能告诉我任何想法吗?

提前致谢: - )

4 个答案:

答案 0 :(得分:1)

问题是:如何将新值附加到SQL Db ....

答案是:你是如何为第一个价值做的......

我不确定你的问题是否清楚btw。

答案 1 :(得分:0)

两种简单的方法:

1)定期更新:

UPDATE myTable SET myvalue = myValue + @newXML WHERE myID = @id

2)使用SQL XML命令更新XML:

示例(来自egghead cafe链接):

update [News].[News] set

Article.modify('replace value of(/NewsArticle/@Title)[1] with
sql:variable("@Title")') where ID=@ID;

http://www.eggheadcafe.com/software/aspnet/33280808/update-xml-column-in-a-single-statement.aspx

http://whyiamright.wordpress.com/2008/01/02/updating-xml-column-in-sql-server-2005/

http://www.15seconds.com/issue/050818.htm

答案 2 :(得分:0)

如果可以使用DataSet方法将XML导入ReadXml(...),则可以非常优雅的方式将其存储到数据库中。

示意性地:

// Create and fill dataset
DataSet set = ...;
set.LoadXml(...);

// Open sql connection, make command and adapter
SqlDataAdapter adapter = ...;
adapter.InsertCommand = new SqlCommand("your sql command", connection);

// Save the dataset
adapter.Update(set, "TableName");

答案 3 :(得分:0)

也许这会对你开始有所帮助,我同意Lucero你需要密切关注你的xml结构......

DECLARE @xml xml
SET @xml = '<Patients/>'

DECLARE @insertedPatient nvarchar(45)
SET @insertedPatient = 'Nads McSteamy'

DECLARE @insertedID int
SET @insertedID = 1


SET @xml.modify
            ('
            insert element Patient {sql:variable("@insertedPatient")} into 
            (/Patients)[1]
            ')

SET @xml.modify
            ('
            insert element ID {sql:variable("@insertedID")} after 
            (/Patients/Patient)[1]
            ')

select @xml