溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么存取帶進(jìn)度的SQL Server FileStream

發(fā)布時(shí)間:2020-09-28 17:46:27 來源:億速云 閱讀:157 作者:小新 欄目:MySQL數(shù)據(jù)庫(kù)

小編給大家分享一下怎么存取帶進(jìn)度的SQL Server FileStream,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

SQL Server FileStream 功能的詳細(xì)參考聯(lián)機(jī)幫助設(shè)計(jì)和實(shí)現(xiàn) FILESTREAM 存儲(chǔ)
這里只是把使用 Win32 管理 FILESTREAM 數(shù)據(jù)的代碼調(diào)整了一下,實(shí)現(xiàn)帶進(jìn)度的存取,這對(duì)于存取較大的文件比較有意義
   要使用FileStream,首先要在 SQL Server配置管理器中打開FileStream選項(xiàng):SQL Server配置管理器–SQL Server服務(wù)–右邊的服務(wù)列表中找到SQL Server服務(wù)–屬性–FILESTREAM–允許遠(yuǎn)程客戶端訪問FILESTREAM數(shù)據(jù)根據(jù)需要選擇,其他兩薦都選上。配置完成后,需要重新啟動(dòng)SQL Server服務(wù)使設(shè)置生效。
   然后使用下面的腳本創(chuàng)建測(cè)試數(shù)據(jù)庫(kù)和測(cè)試表

 -- =========================================================-- 啟用 filestream_access_level-- =========================================================EXEC sp_configure 'filestream_access_level', 2;     -- 0=禁用  1=針對(duì) T-SQL 訪問啟用 FILESTREAM  2=針對(duì) T-SQL 和 WIN32 流訪問啟用 FILESTREAMRECONFIGURE;
GO-- =========================================================-- 創(chuàng)建測(cè)試數(shù)據(jù)庫(kù)-- =========================================================EXEC master..xp_create_subdir 'f:\temp\db\_test';CREATE DATABASE _testON
    PRIMARY(
        NAME = _test, FILENAME = 'f:\temp\db\_test\_test.mdf'),
    FILEGROUP FG_stream CONTAINS FILESTREAM(
        NAME = _test_file_stream, FILENAME = 'f:\temp\db\_test\stream')
    LOG ON(
        NAME = _test_log, FILENAME = 'f:\temp\db\_test\_test.ldf')
;GO-- =========================================================-- FileStream-- =========================================================-- =================================================-- 創(chuàng)建 包含 FileStream 數(shù)據(jù)的表-- -------------------------------------------------CREATE TABLE _test.dbo.tb_fs(
    id uniqueidentifier ROWGUIDCOL      -- 必需        DEFAULT NEWSEQUENTIALID ( ) PRIMARY KEY,
    name nvarchar(260),
    content varbinary(max) FILESTREAM
);GO
下面的 VB 腳本實(shí)現(xiàn)帶進(jìn)度顯示的文件存(Write方法)取(Read方法)
Imports System.IO
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Module Module1
    Public Sub Main(ByVal args As String())
        Dim sqlConnection As New SqlConnection("Integrated Security=true;server=localhost")
        Try
            sqlConnection.Open()
            Console.WriteLine("將文件保存到 FileStream")
            Write(sqlConnection, "test", "f:\temp\re.csv")
            Console.WriteLine("從 FileStream 讀取數(shù)據(jù)保存到文件")
            Read(sqlConnection, "test", "f:\temp\re_1.csv")
        Catch ex As System.Exception
            Console.WriteLine(ex.ToString())
        Finally
            sqlConnection.Close()        End Try
        Console.WriteLine("處理結(jié)束,按 Enter 退出")
        Console.ReadLine()    End Sub    ''' <summary>
    ''' 將文件保存到數(shù)據(jù)庫(kù)    ''' </summary>
    ''' <param name="conn">數(shù)據(jù)庫(kù)連接</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            '事務(wù)
            Dim transaction As SqlTransaction = conn.BeginTransaction("mainTranaction")
            sqlCmd.Transaction = transaction
            '1. 讀取 FILESTREAM 文件路徑 ( 注意函數(shù)大小寫 )
            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
            '2. 讀取當(dāng)前事務(wù)上下文
            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 中讀取和寫入數(shù)據(jù)            Using sqlFileStream As New SqlFileStream(filePath, txContext, FileAccess.Write)
                Dim buffer As Byte() = New Byte(bufferSize - 1) {}
                Dim numBytes As Integer = 0
                Using fsRead As New FileStream(file, FileMode.Open)
                    While True
                        numBytes = fsRead.Read(buffer, 0, bufferSize)                        If numBytes = 0 Then Exit While
                        sqlFileStream.Write(buffer, 0, numBytes)
                        Console.WriteLine(String.Format("{0} -> {1} -> {2}", fsRead.Position, sqlFileStream.Position, numBytes))                    End While
                    fsRead.Close()                End Using
                sqlFileStream.Close()            End Using
            sqlCmd.Transaction.Commit()        End Using
    End Sub    ''' <summary>
    ''' 從數(shù)據(jù)庫(kù)讀取數(shù)據(jù)保存到文件    ''' </summary>
    ''' <param name="conn">數(shù)據(jù)庫(kù)連接</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 文件路徑 ( 注意函數(shù)大小寫 )
            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. 讀取當(dāng)前事務(wù)上下文
            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 中讀取和寫入數(shù)據(jù)
            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

以上是怎么存取帶進(jìn)度的SQL Server FileStream的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI