Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在DbContext.Database.ExecuteSqlCommand方法中将参数传递给以更新/插入XML节点?_C#_Entity Framework_Sql Server 2008 R2 - Fatal编程技术网

C# 如何在DbContext.Database.ExecuteSqlCommand方法中将参数传递给以更新/插入XML节点?

C# 如何在DbContext.Database.ExecuteSqlCommand方法中将参数传递给以更新/插入XML节点?,c#,entity-framework,sql-server-2008-r2,C#,Entity Framework,Sql Server 2008 R2,读完这篇文章之后。我希望有一个选项可以使用SqlParameter来更新sql数据库中的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 Tr

读完这篇文章之后。我希望有一个选项可以使用SqlParameter来更新sql数据库中的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")
                              );
数据库中的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}方法也不起作用,也很少添加到节点

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

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")
                          );
在尝试通过EF执行查询之前,还应该首先尝试让查询在ManagementStudio中工作。下面的方法有效吗

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工作

感谢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"));
sql=@“updatetrains SET TrainsRef.modify('insert{sql:variable(“@mdo”)}到(/Revalidant)[1]'))”;
int result=db.Database.ExecuteSqlCommand(sql,
新的SqlParameter(“name”,“Realy Big Train”);

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