【问题标题】:How to pass parameters to in the DbContext.Database.ExecuteSqlCommand method to update / insert a XML node?如何在 DbContext.Database.ExecuteSqlCommand 方法中传递参数以更新/插入 XML 节点?
【发布时间】:2014-11-17 22:39:44
【问题描述】:

读完这篇文章How to pass parameters to the DbContext.Database.ExecuteSqlCommand method。我希望有一个选项可以使用 SqlParameter 来更新 sql db 中的 XML 节点。我现在正在使用的代码示例,但这根本不保存。

string name = "Really Big Train";
string traintype = "AT1";

string sql = "UPDATE Trains SET TrainsRef.modify('replace value of(//name/text())[1] with (\"" + name + "\")') Where TrainsRef.value('(//traintype)[1]', 'varchar(3)') = " + traintype;

int result = db.Database.ExecuteSqlCommand(sql);

当我以某种方式尝试使用 SqlParameter 时,它们没有按预期传递。

使用时

string sql = "UPDATE Trains SET TrainsRef.modify('replace value of(//name/text())[1] with (\"@name\")') Where TrainsRef.value('(//traintype)[1]', 'varchar(3)') = @traintype" ;

int result = db.Database.ExecuteSqlCommand(sql,
                              new SqlParameter("name", "Realy Big Train"),
                              new SqlParameter("traintype", "AT1")
                              );

DB 中的 XML 根本没有更新

当我使用时

string sql = "UPDATE Trains SET TrainsRef.modify('replace value of(//name/text())[1] with (\"@name\")') Where TrainsRef.value('(//traintype)[1]', 'varchar(3)') = 'AT1'" ;

int result = db.Database.ExecuteSqlCommand(sql,
                              new SqlParameter("name", "Realy Big Train")
                              );

XML 节点用@name 更新,而不是用“Realy Big Train”

ps {@p1} 方法也不起作用,也很少添加到节点中

【问题讨论】:

  • 从 Management Studio 执行时,您的语句是否有效?尝试将参数传递给无法运行的查询没有多大意义
  • 为什么我没有想到 :(。感谢您的提示。当我在 MSSMS 中运行查询时,我收到以下错误“获取 'xml 数据类型方法的参数 1”修改“在 xml 中插入属性时必须是字符串文字”-> 这将我带到这篇文章 link 并且我得到了答案。我更新了我的答案。

标签: c# entity-framework sql-server-2008-r2


【解决方案1】:

您的所有语句对占位符和参数使用不同的名称。参数名称包含 @ 前缀。在一种情况下,您在参数占位符周围添加了引号,基本上将其转换为文字。 您应该尝试以下语句

string sql = "UPDATE Trains SET TrainsRef.modify('replace value of(//name/text())[1] " +
    " with (@name)') Where TrainsRef.value('(//traintype)[1]', 'varchar(3)') = @traintype" ;

int result = db.Database.ExecuteSqlCommand(sql,
                          new SqlParameter("name", "Realy Big Train"),
                          new SqlParameter("traintype", "AT1")
                          );

您还应该先尝试让查询在 Management Studio 中运行,然后再尝试通过 EF 执行它。以下是否有效?

declare @name varchar(50)='Really Big Train';
delcare @traintype varchar(3) = 'AT1';

UPDATE Trains 
SET TrainsRef.modify('replace value of(//name/text())[1] with (@name)') 
Where TrainsRef.value('(//traintype)[1]', 'varchar(3)') = @traintype" 

如果不是,它也无法通过 EF 工作

【讨论】:

    【解决方案2】:

    感谢 Panagiotis 帮助我找到自己的答案 :)。

    要使用参数更新 XML,您需要 sql:variable("@parameter")

    替换现有 XMLnode 的值

    sql = @"UPDATE Trains SET TrainsRef.modify('replace value of(//name/text())[1] with sql:variable(""@name"")')";
    
    int result = db.Database.ExecuteSqlCommand(sql,
                          new SqlParameter("name", "Realy Big Train"));
    

    替换值新的 XMLnode

    sql = @"UPDATE Trains SET TrainsRef.modify('insert <name>{sql:variable(""@mdo"")}</name> into (/Revalidant)[1]')')";
    
    int result = db.Database.ExecuteSqlCommand(sql,
                          new SqlParameter("name", "Realy Big Train"));
    

    【讨论】:

      猜你喜欢
      • 2011-07-25
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-15
      • 1970-01-01
      相关资源
      最近更新 更多