加入收藏 | 设为首页 | 会员中心 | 我要投稿 拼字网 - 核心网 (https://www.hexinwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

如何存取带进度的SQL Server FileStream

发布时间:2022-03-04 05:48:58 所属栏目:MySql教程 来源:互联网
导读:小编给大家分享一下怎么存取带进度的SQL Server FileStream,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧! SQL Server FileStream 功能的详细参考联机帮助设计和实现 FILE
       小编给大家分享一下怎么存取带进度的SQL Server FileStream,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
 
     SQL Server FileStream 功能的详细参考联机帮助设计和实现 FILESTREAM 存储
     这里只是把使用 Win32 管理 FILESTREAM 数据的代码调整了一下,实现带进度的存取,这对于存取较大的文件比较有意义
    要使用FileStream,首先要在 SQL Server配置管理器中打开FileStream选项:SQL Server配置管理器–SQL Server服务–右边的服务列表中找到SQL Server服务–属性–FILESTREAM–允许远程客户端访问FILESTREAM数据根据需要选择,其他两荐都选上。配置完成后,需要重新启动SQL Server服务使设置生效。
   然后使用下面的脚本创建测试数据库和测试表
 
 -- =========================================================-- 启用 filestream_access_level-- =========================================================EXEC sp_configure 'filestream_access_level', 2;     -- 0=禁用  1=针对 T-SQL 访问启用 FILESTREAM  2=针对 T-SQL 和 WIN32 流访问启用 FILESTREAMRECONFIGURE;
GO-- =========================================================-- 创建测试数据库-- =========================================================EXEC master..xp_create_subdir 'f:tempdb_test';CREATE DATABASE _testON
    PRIMARY(
        NAME = _test, FILENAME = 'f:tempdb_test_test.mdf'),
    FILEGROUP FG_stream CONTAINS FILESTREAM(
        NAME = _test_file_stream, FILENAME = 'f:tempdb_teststream')
    LOG ON(
        NAME = _test_log, FILENAME = 'f:tempdb_test_test.ldf')
;GO-- =========================================================-- FileStream-- =========================================================-- =================================================-- 创建 包含 FileStream 数据的表-- -------------------------------------------------CREATE TABLE _test.dbo.tb_fs(
    id uniqueidentifier ROWGUIDCOL      -- 必需        DEFAULT NEWSEQUENTIALID ( ) PRIMARY KEY,
    name nvarchar(260),
    content varbinary(max) FILESTREAM
);GO
 如何存取带进度的SQL Server FileStream
            sqlConnection.Open()
            Console.WriteLine("将文件保存到 FileStream")
            Write(sqlConnection, "test", "f:tempre.csv")
            Console.WriteLine("从 FileStream 读取数据保存到文件")
            Read(sqlConnection, "test", "f:tempre_1.csv")
        Catch ex As System.Exception
            Console.WriteLine(ex.ToString())
        Finally
            sqlConnection.Close()        End Try
        Console.WriteLine("处理结束,按 Enter 退出")
        Console.ReadLine()    End Sub    ''' <summary>
    ''' 将文件保存到数据库    ''' </summary>
    ''' <param name="conn">数据库连接</param>    ''' <param name="name">名称</param>
    ''' <param name="file">文件名</param>
    Sub Write(ByVal conn As SqlConnection, ByVal name As String, ByVal file As String)
        Dim bufferSize As Int32 = 1024
        Using sqlCmd As New SqlCommand
            sqlCmd.Connection = conn            '事务
            Dim transaction As SqlTransaction = conn.BeginTransaction("mainTranaction")
            sqlCmd.Transaction = transaction
            '1. 读取 FILESTREAM 文件路径 ( 注意函数大小写 )
            sqlCmd.CommandText = "
UPDATE _test.dbo.tb_fs SET content = 0x WHERE name = @name;
IF @@ROWCOUNT = 0 INSERT _test.dbo.tb_fs(name, content) VALUES( @name, 0x );
SELECT content.PathName() FROM _test.dbo.tb_fs WHERE name = @name;"
            sqlCmd.Parameters.Add(New SqlParameter("name", name))
            Dim filePath As String = Nothing
            Dim pathObj As Object = sqlCmd.ExecuteScalar()            If Not pathObj.Equals(DBNull.Value) Then
                filePath = DirectCast(pathObj, String)            Else
                Throw New System.Exception("content.PathName() failed to read the path name for the content column.")            End If
      
    ''' 从数据库读取数据保存到文件    ''' </summary>
    ''' <param name="conn">数据库连接</param>    ''' <param name="name">名称</param>
    ''' <param name="file">文件名</param>
    Sub Read(ByVal conn As SqlConnection, ByVal name As String, ByVal file As String)
        Dim bufferSize As Int32 = 1024
        Using sqlCmd As New SqlCommand
            sqlCmd.Connection = conn            '1. 读取 FILESTREAM 文件路径 ( 注意函数大小写 )
            sqlCmd.CommandText = "SELECT content.PathName() FROM _test.dbo.tb_fs WHERE name = @name;"
            sqlCmd.Parameters.Add(New SqlParameter("name", name))
            Dim filePath As String = Nothing
            Dim pathObj As Object = sqlCmd.ExecuteScalar()
            If Not pathObj.Equals(DBNull.Value) Then
                filePath = DirectCast(pathObj, String)
            Else
                Throw New System.Exception("content.PathName() failed to read the path name for the content column.")
            End If
            '2. 读取当前事务上下文
            Dim transaction As SqlTransaction = conn.BeginTransaction("mainTranaction")
            sqlCmd.Transaction = transaction
            sqlCmd.CommandText = "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()"
            Dim obj As Object = sqlCmd.ExecuteScalar()
            Dim txContext As Byte() = Nothing
            Dim contextLength As UInteger            If Not obj.Equals(DBNull.Value) Then
                txContext = DirectCast(obj, Byte())
                contextLength = txContext.Length()            Else
                Dim message As String = "GET_FILESTREAM_TRANSACTION_CONTEXT() failed"
                Throw New System.Exception(message)            End If
            '3. 获取 Win32 句柄,并使用该句柄在 FILESTREAM BLOB 中读取和写入数据
            Using sqlFileStream As New SqlFileStream(filePath, txContext, FileAccess.Read)
                Dim buffer As Byte() = New Byte(bufferSize - 1) {}
                Dim numBytes As Integer = 0
                Using fsRead As New FileStream(file, FileMode.Create)
                    While True
                        numBytes = sqlFileStream.Read(buffer, 0, bufferSize)
                        If numBytes = 0 Then Exit While
                        fsRead.Write(buffer, 0, numBytes)
                        Console.WriteLine(String.Format("{0} -> {1} -> {2}", sqlFileStream.Position, sqlFileStream.Position, numBytes))
                    End While
                    fsRead.Close()
                End Using
                sqlFileStream.Close()
            End Using
            sqlCmd.Transaction.Commit()
        End Using
    End Sub
End Module
以上是怎么存取带进度的SQL Server FileStream的所有内容,感谢各位的阅读!

(编辑:拼字网 - 核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!