1

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 = {"Incorrect syntax near '/'."}

-- ck

これは私が期待しているものです

"INSERT Structure (Path,Description,ParentID) VALUES(/.GetDescendant(NULL, NULL) ,'Test', 1"

-- ポール・ルアン

私はすでにこのページを見ましたが、何かを見落としていない限り、本当に役に立ちませんでしたか?

ありがとう

クレア

4

2 に答える 2

3

'' だけが間違っている場合は、キャストしようとします。

SqlCommand command = new SqlCommand("INSERT Structure (Path,Description,ParentID) " +
    "VALUES(CAST('"+path+"' AS hierarchyid).GetDescendant(" + lastChildPath +
    ", NULL) " +
    ",@description, @parentId", _connection);
于 2010-08-17T08:42:59.647 に答える
1

これは古く、答えがあることに気づきましたが、同じことをしようとしてこの質問に出くわしたので、別のオプションを追加したいと思いました。

Microsoft.SqlServer.Types 名前空間と次のいずれかを使用して、文字列から C# の HierarchyId に変換できます。

SqlHierarchyId hierarchyIdRepresentation = (SqlHierarchyId)Convert.ChangeType(input, typeof(SqlHierarchyId))

また

SqlHierarchyId hierarchyIdRepresentation = SqlHierarchyId.Parse(input)

この.Parse()メソッドは、文字列からの変換が行われるたびに自動的に呼び出され.ToString()、型のメソッドがSqlHierarchyId標準の文字列演算子をオーバーライドして正規表現を返します。

ソース: https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.types.sqlhierarchyid_methods(v=sql.105).aspx

于 2015-12-31T19:29:50.347 に答える