在C#中将字符串转换为hierarchyid
我需要能够将字符串转换为 c#.net 中的 hierarchyid - 我无法使用存储过程。
当我传入路径(字符串)时,查询失败,因为路径存储为“/”而不是 /
我可以将其转换为另一种类型吗?
SqlCommand command = new SqlCommand("INSERT Structure (Path,Description,ParentID) " +
"VALUES(" + path + ".GetDescendant(" + lastChildPath +
", NULL) " +
",@description, @parentId", _connection);
-- BitKFu
我已经添加了,这是它生成的 sql 查询:
CommandText = "INSERT Structure (Path,Description,ParentID) VALUES(CAST(/ AS hierarchyid).GetDescendant(NULL, NULL) ,@description, @parentId"
我收到以下错误:ex = {“'/'附近的语法不正确。”}
-- ck
这就是我所期待的
"INSERT Structure (Path,Description,ParentID) VALUES(/.GetDescendant(NULL, NULL) ,'Test', 1"
-- Paul Ruane
我已经看过此页面,但它并没有真正帮助,除非我忽略了一些东西?
谢谢
克莱尔
I need to be able to convert a string to a hierarchyid in c#.net - I cannot use stored procedures.
When I pass in the path (string) the query fails as the path is stored like this '/' instead of /
Can I convert it to another type?
SqlCommand command = new SqlCommand("INSERT Structure (Path,Description,ParentID) " +
"VALUES(" + path + ".GetDescendant(" + lastChildPath +
", NULL) " +
",@description, @parentId", _connection);
-- BitKFu
I have added the and this is the sql query it produces:
CommandText = "INSERT Structure (Path,Description,ParentID) VALUES(CAST(/ AS hierarchyid).GetDescendant(NULL, NULL) ,@description, @parentId"
I get the following error: ex = {"Incorrect syntax near '/'."}
-- ck
This is what I am expecting
"INSERT Structure (Path,Description,ParentID) VALUES(/.GetDescendant(NULL, NULL) ,'Test', 1"
-- Paul Ruane
I have looked at this page already but it didnt really help, unless I overlooked something?
Thanks
Clare
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果只有 '' 是错误的,我会尝试投射它。
I would try to cast it, if only the '' is wrong.
我意识到这是旧的,并且有一个答案,但想添加另一个选项,因为我遇到了这个问题试图做同样的事情。
您可以使用 Microsoft.SqlServer.Types 命名空间和类似以下内容的方式将 C# 中的字符串转换为 HierarchyId:
SqlHierarchyId HierarchyIdRepresentation = (SqlHierarchyId)Convert.ChangeType(input, typeof(SqlHierarchyId))
或
SqlHierarchyId HierarchyIdRepresentation = SqlHierarchyId.Parse(input)
每当有从字符串转换以及
SqlHierarchyId
类型的.ToString()
方法会覆盖标准字符串运算符以返回规范表示形式。来源: https:// msdn.microsoft.com/en-us/library/microsoft.sqlserver.types.sqlhierarchyid_methods(v=sql.105).aspx
I realize this is old, and has an answer, but wanted to add another option since I ran into this question trying to do the same thing.
You can convert from a string to a HierarchyId in C# by using the Microsoft.SqlServer.Types namespace and something like either:
SqlHierarchyId hierarchyIdRepresentation = (SqlHierarchyId)Convert.ChangeType(input, typeof(SqlHierarchyId))
or
SqlHierarchyId hierarchyIdRepresentation = SqlHierarchyId.Parse(input)
The
.Parse()
method is called automatically anytime there is a conversion from a string and the.ToString()
method of theSqlHierarchyId
type overrides the standard string operator to return the canonical representation.Source: https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.types.sqlhierarchyid_methods(v=sql.105).aspx