C# 如何在SQLServer中修改xml节点
我的表中有一个名为“Content_Html”的xml列,其中的数据如下所示:C# 如何在SQLServer中修改xml节点,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我的表中有一个名为“Content_Html”的xml列,其中的数据如下所示: <root> <Category>Cover Impression</Category> <Title>Mystery of the Wolves</Title> <Month>April</Month> ... ... </root> 但给我“不能在
<root>
<Category>Cover Impression</Category>
<Title>Mystery of the Wolves</Title>
<Month>April</Month>
...
...
</root>
但给我“不能在ntext上调用方法”的错误
谢谢,你可以这样试试 对于此代码
<Sample>
<NodeOne>Value1</NodeOne>
<NodeTwo>Value2</NodeTwo>
<NodeThree>OldValue</NodeThree>
</Sample>
请仔细阅读以下内容,因为更新:您可以尝试(但请先在测试环境中)将列的数据类型更改为XML:
ALTER TABLE dbo.YourTable
ALTER COLUMN Content_Html XML
如果该列的所有行中只有有效的XML内容,那么该命令应该可以工作
一旦成功,您就可以在具体示例中尝试以下方法:
UPDATE dbo.YourTable
SET Content_Html.modify('replace value of (/root/Category/text())[1] with "Cover Impressions"')
WHERE ...(whatever condition need here).....
感谢您的回复,但我在执行此语句时收到“Cannot call methods on ntext”错误消息。@marc_感谢您的反馈,我不精通sql-无论如何,我尝试了您将变量声明为XML的方法,但仍然收到相同的消息-“Cannot call methods on ntext”…嗯,如果列的类型是
NTEXT
,那么XML函数当然不会工作!我只是假设它是一个XML列(既然你这么说了!)首先:不要再使用NTEXT了-它已经被弃用了-如果有的话,使用nvarchar(max)
,如果它是一个包含XML的列-使用XML
…嗯,如果你的列类型是NTEXT
,当然XML函数不会工作!我只是假设它是一个XML列(既然你这么说了!)首先:不要再使用NTEXT了-它已被弃用-如果有,请使用nvarchar(max)
,如果它是一个包含XML的列,请使用XML
。。。
DECLARE @newValue varchar(50)
SELECT @newValue = 'NewValue'
UPDATE [Product]
SET ProductXml.modify('replace value of (/Sample/NodeThree/text())[1] with sql:variable("@newValue")')
ALTER TABLE dbo.YourTable
ALTER COLUMN Content_Html XML
UPDATE dbo.YourTable
SET Content_Html.modify('replace value of (/root/Category/text())[1] with "Cover Impressions"')
WHERE ...(whatever condition need here).....