C# 将字符串转换为C中的层次ID#

C# 将字符串转换为C中的层次ID#,c#,tsql,hierarchyid,C#,Tsql,Hierarchyid,我需要能够在c#.net中将字符串转换为层次ID-我不能使用存储过程 当我传入路径(字符串)时,查询会失败,因为路径存储方式是“/”而不是/ 我能把它换成另一种类型吗 SqlCommand command = new SqlCommand("INSERT Structure (Path,Description,ParentID) " + "VALUES(" + path + ".GetDescendant(" + lastChildPath + ", NULL) " +

我需要能够在c#.net中将字符串转换为层次ID-我不能使用存储过程

当我传入路径(字符串)时,查询会失败,因为路径存储方式是“/”而不是/

我能把它换成另一种类型吗

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"
——保罗·鲁安

我已经看了这一页,但它没有真正的帮助,除非我忽略了什么

谢谢


克莱尔

如果只是“”错了,我会试着投它

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

我会试着投下它,只要“”是错的

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

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

您可以使用Microsoft.SqlServer.Types命名空间和类似以下内容在C#中将字符串转换为HierarchyId:

SqlHierarchyId hierarchyid表示=(SqlHierarchyId)Convert.ChangeType(输入,typeof(SqlHierarchyId))

SqlHierarchyId hierarchyIdRepresentation=SqlHierarchyId.Parse(输入)

只要字符串发生转换,就会自动调用
.Parse()
方法,
SqlHierarchyId
类型的
.ToString()
方法覆盖标准字符串运算符以返回规范表示形式


来源:

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

您可以使用Microsoft.SqlServer.Types命名空间和类似以下内容在C#中将字符串转换为HierarchyId:

SqlHierarchyId hierarchyid表示=(SqlHierarchyId)Convert.ChangeType(输入,typeof(SqlHierarchyId))

SqlHierarchyId hierarchyIdRepresentation=SqlHierarchyId.Parse(输入)

只要字符串发生转换,就会自动调用
.Parse()
方法,
SqlHierarchyId
类型的
.ToString()
方法覆盖标准字符串运算符以返回规范表示形式


来源:

输入和输出示例将非常有用。。。可能会有帮助。您必须引用/,比如“/”示例输入和输出将非常有用。。。可能会有帮助。你必须引用/,比如“/”谢谢BitKFu-它确实需要引用才能再次播放,我想你错过了引用。谢谢BitKFu-它确实需要引用才能再次播放,我想你错过了引用。