如果需要以流式方式从SqlServer 读取数据,则可以使用一些功能。例如使用SqlDataReader
with CommandBehavior.SequentialAccess
,特别是当需要访问二进制列数据时,有以下GetStream(int)
方法:
var cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = @"select 0x0123456789 as Data";
using (var dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
dr.Read();
var stream = dr.GetStream(0);
// access stream
}
但是,当需要使用向SqlServer提供数据时SqlBulkCopy
,特别是如果需要将流作为二进制列的数据源提供时,反方向的流数据呢?
我试过以下
var cmd2 = new SqlCommand();
cmd2.Connection = connection;
cmd2.CommandText = @"create table #Test (ID int, Data varbinary(max))";
cmd2.ExecuteNonQuery();
using (SqlBulkCopy sbc = new SqlBulkCopy(connection, SqlBulkCopyOptions.TableLock, null))
{
sbc.DestinationTableName = "#Test";
sbc.EnableStreaming = true;
sbc.ColumnMappings.Add(0, "ID");
sbc.ColumnMappings.Add(1, "Data");
sbc.WriteToServer(new TestDataReader());
}
其中TestDataReader
实现IDataReader
如下:
class TestDataReader : IDataReader
{
public int FieldCount { get { return 2; } }
int rowCount = 1;
public bool Read() { return (rowCount++) < 3; }
public bool IsDBNull(int i) { return false; }
public object GetValue(int i)
{
switch (i)
{
case 0: return rowCount;
case 1: return new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89 };
default: throw new Exception();
}
}
//the rest members of IDataReader
}
它按预期工作。
然而变化
case 1: return new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89 };
至
case 1: return new MemoryStream(new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89 });
System.InvalidOperationException
导致消息异常
来自数据源的 MemoryStream 类型的给定值无法转换为指定目标列的 varbinary 类型。
有没有办法提供Stream
from IDataReader
(或可能DbDataReader
) toSqlBulkCopy
作为二进制列的数据源,而无需先将其所有数据复制到内存(字节数组)中?