在C#中将字符串转换为hierarchyid

发布于 2024-09-14 19:41:36 字数 909 浏览 7 评论 0原文

我需要能够将字符串转换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执着的年纪 2024-09-21 19:41:36

如果只有 '' 是错误的,我会尝试投射它。

SqlCommand command = new SqlCommand("INSERT Structure (Path,Description,ParentID) " +
    "VALUES(CAST('"+path+"' AS hierarchyid).GetDescendant(" + lastChildPath +
    ", NULL) " +
    ",@description, @parentId", _connection);

I would try to cast it, if only the '' is wrong.

SqlCommand command = new SqlCommand("INSERT Structure (Path,Description,ParentID) " +
    "VALUES(CAST('"+path+"' AS hierarchyid).GetDescendant(" + lastChildPath +
    ", NULL) " +
    ",@description, @parentId", _connection);
走走停停 2024-09-21 19:41:36

我意识到这是旧的,并且有一个答案,但想添加另一个选项,因为我遇到了这个问题试图做同样的事情。

您可以使用 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 the SqlHierarchyId 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文