最后我想出了如何做到这一点:
创建文件夹并获取路径定位器
Dim insertDir = "insert into dbo.DocumentStore(name,is_directory,is_archive) output INSERTED.path_locator values(@name,1,0)"
Using sqlCommand As New SqlCommand(insertDir, con)
sqlCommand.Parameters.Add("@name", SqlDbType.VarChar).Value = Me.txtGroupName.Text
parentPathLocator = sqlCommand.ExecuteScalar()
End Using
创建一个新的 hierachyID
Dim retnewpath = "select CONVERT(VARCHAR(20), CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), NEWID()), 1, 6))) +'.'+ CONVERT(VARCHAR(20), CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), NEWID()), 7, 6))) +'.'+ CONVERT(VARCHAR(20), CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), NEWID()), 13, 4))) as path"
Using sqlCommand As New SqlCommand(retnewpath, con)
subpathlocator = sqlCommand.ExecuteScalar()
subpathlocator = parentPathLocator.ToString() & subpathlocator & "/"
End Using
插入具有新层次结构标识的文件作为路径定位器
Dim insertStr = "insert into dbo.DocumentStore(name,file_stream,path_locator) output INSERTED.stream_id values(@name,@file_stream,@parent_path_locator)"
Using sqlCommand As New SqlCommand(insertStr, con)
sqlCommand.Parameters.Add("@name", SqlDbType.VarChar).Value = Path.GetFileName(filename)
sqlCommand.Parameters.Add("@file_stream", SqlDbType.VarBinary).Value = fileStream
sqlCommand.Parameters.AddWithValue("@parent_path_locator", subpathlocator)
streamID = sqlCommand.ExecuteScalar()
End Using
我使用这个链接来创建 hierachyId,所以我还不完全理解它。