【问题标题】:How to insert a file under a directory in SQL Filetable如何在 SQL Filetable 中的目录下插入文件
【发布时间】:2014-05-13 06:02:06
【问题描述】:

我在文件表中创建了一个目录。现在我需要在该目录中插入文件。由于 parent_path_locator 无法设置,我想不出如何实现这一点。我在代码中做所有这些。

这就是我创建目录的方式;

 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

注意:我看到我们可以使用 dbo.GetNewPathLocator(path) 根据路径定位器获取子目录路径。但我不确定如何在我的情况下使用它来插入文件。

更新:我发现如何在 TSQL TSQL 中执行此操作,但如何在代码中执行此操作?

【问题讨论】:

    标签: sql .net vb.net filetable sqlfilestream


    【解决方案1】:

    终于想通了:

    创建文件夹并获取路径定位器

     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
    

    使用新的hierarchyid插入文件作为路径定位器

     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
    

    我用这个link创建了hierachyId,所以我还没有完全理解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-06
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多