我如何在INSERT上流式传输BLOB到VARBINARY(MAX)

本文关键字:BLOB VARBINARY MAX 传输 INSERT | 更新日期: 2023-09-27 18:16:37

我发现了很多关于如何使用CommandBehavior.SequentialAccess选择BLOB作为流的信息。

我也想在插入上流式传输BLOB(以避免将BLOB缓存为内存中的字节数组),我找不到任何示例。我发现一些文档提到UPDATE T-SQL语句中的.WRITE (expression,@Offset, @Length)语法与VARBINARY(MAX)兼容。因此,我正在考虑编写一个类,它可以采用Stream并使用连续的UPDATE (. write)语句将其块到数据库中。这是正确的方法吗,还是有更好的方法?

UPDATE的链接。写:

http://msdn.microsoft.com/en-us/library/ms178158 (SQL.100) . aspx

http://msdn.microsoft.com/en-us/library/ms177523 (v = SQL.100) . aspx

使用CommandBehavior选择blob的链接。SequentialAccess:

http://msdn.microsoft.com/en-us/library/87z0hy49.aspx

c#/SQL 2005中读取BLOB数据的有效方法

使用SqlDataReader获取二进制数据

当使用SqlDataReader时,如何使blob流在普通的旧c#对象中可用?

用c#从SQL Server流化VARBINARY数据

下面是使用。write语法的POC:

DDL:

create database BlobTest
go
use blobtest
go
create table Blob
(
    Id bigint not null primary key identity(1,1),
    Data varbinary(max) not null default(0x)
)
c#:

using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            string pathToBigFile = "C:''bigfile.big";
            int optimumBufferSizeForSql = 8040; //See https://stackoverflow.com/questions/5629991/how-can-i-generate-an-insert-script-for-a-table-with-a-varbinarymax-field
            long newBlobId = InitialiseNewBlobInSqlServer();
            using (Stream stream = new FileStream(  pathToBigFile, 
                                                    FileMode.Open, 
                                                    FileAccess.Read, 
                                                    FileShare.ReadWrite))
            {
                byte[] buffer = new byte[optimumBufferSizeForSql];
                while(true)
                {
                    int numberBytesRead = stream.Read(buffer, 0, optimumBufferSizeForSql);
                    if (numberBytesRead == 0)
                    {
                        //Done
                        break;
                    }
                    WriteBufferToSqlServer(
                        numberBytesRead == optimumBufferSizeForSql ? buffer : buffer.Take(numberBytesRead).ToArray(),
                        newBlobId);
                }
            }
        }
        static long InitialiseNewBlobInSqlServer()
        {
            using (SqlConnection conn = new SqlConnection("Data Source=localhost; Initial Catalog=BlobTest; Integrated Security=SSPI;"))
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = conn;
                command.CommandType = CommandType.Text;
                command.CommandText = "Insert into blob (Data) values (0x); select convert(bigint,Scope_identity());";
                conn.Open();
                return (long) command.ExecuteScalar();
            }
        }
        static void WriteBufferToSqlServer(byte[] data, long blobId)
        {
            using (SqlConnection conn = new SqlConnection("Data Source=localhost; Initial Catalog=BlobTest; Integrated Security=SSPI;"))
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = conn;
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@id", blobId);
                command.Parameters.AddWithValue("@data", data);
                command.CommandText = "Update Blob set Data.Write(@data, null, null) where Id = @id;";
                conn.Open();
                command.ExecuteNonQuery();
            }
        }
    }
}

我如何在INSERT上流式传输BLOB到VARBINARY(MAX)

您应该使用SQL Server的RBS接口来处理blob。