FILESTREAM - 在 FILESTREAM 列中读写一个大文件

FILESTREAM - Read and write a large file in FILESTREAM column

我应该使用 System.Data.SqlTypes.SqlFileStream 来读写 FILESTREAM 列吗?

例如:

var sqlCommand = sqlConnection3.CreateCommand();
sqlCommand.CommandText = "Select FileData.PathName() As Path, GET_FILESTREAM_TRANSACTION_CONTEXT() As TransactionContext From PictureTable Where PkId = (Select Max(PkId) From PictureTable)"; 

var reader = sqlCommand.ExecuteReader();
reader.Read();
var filePath = (string)reader["Path"];
var transactionContext = (byte[])reader["TransactionContext"];
var sqlFileStream = new SqlFileStream(filePath, transactionContext, FileAccess.Read);
var data = new byte[sqlFileStream.Length];
sqlFileStream.Read(data, 0, Convert.ToInt16(sqlFileStream.Length));

在上面的代码中,我可以使用此处显示的查询并直接访问 FileData:

Select FileData From PictureTable Where PkId = (Select Max(PkId) From PictureTable)

不使用SqlFileStream而使用varbinary(max)读写会怎样?

当 SQL 服务器和应用程序安装在单独的服务器中时,我在设置 Windows 访问 SqlFileStream 文件路径的身份验证时遇到一些问题。我怎样才能避免这个问题?

根据 Microsoft 的文档,SqlFileStream 是合适的解决方案(参见 here). The SqlFileStream handles other things like file handles as data stored in a file stream is different from data stored in a row-based data file (see here)。

希望对您有所帮助!