c# - 如何在 INSERT 上将 BLOB 流式传输到 VARBINARY(MAX)

标签 c# sql-server-2008

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

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

UPDATE.WRITE 链接:

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

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

使用 CommandBehavior.SequentialAccess 选择 BLOB 的链接:

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

Memory effective way to read BLOB data in C#/SQL 2005

Getting binary data using SqlDataReader

How to make streams from BLOBs available in plain old C# objects when using SqlDataReader?

Streaming VARBINARY data from SQL Server in C#

这是使用 .Write 语法的 POC:

数据链接:

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();
            }
        }
    }
}

最佳答案

你应该使用 RBS用于处理 blob 的 SQL Server 接口(interface)。

关于c# - 如何在 INSERT 上将 BLOB 流式传输到 VARBINARY(MAX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8385853/

相关文章:

c# - 在 Visual Studio Ultimate 2010 中添加 MySQL.Data 作为引用

c# - 从 CreatedUser 事件中取消 CreateUserWizard

c# - 当数据源为 Linq 时访问 ItemDataBound 事件中的列

sql - 使用 RAISERROR 指示 ACCESS DENIED 错误

C# ADO.NET 插入具有默认值的新行

sql - 进行多个连接时是否需要连续关系

IS NULL 时的 SQL Server Case 语句

c# - 如何将 Byte[](解码为 PNG 或 JPG)转换为 Tensorflows Tensor

sql-server - Sql Server 2008 的更改通知

c# - 在 HTTP CONNECT over SSL 期间设置用户代理 header ?