【发布时间】: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