I'm trying to access a Sql filestream from a CLR stored procedure. I've set up a very simple database with a single table which includes a filestream column. I can successfully read from the filestream using a simple console app. Here's some example code for the proc that fails:
[SqlProcedure]
public static void GetDataFromFileStream(string path, out int data)
{
using (var connection = new SqlConnection("context connection=true"))
{
connection.Open();
var transaction = connection.BeginTransaction();
var transactionContext = GetTransactionContext(connection, transaction);
// the following line throws an exception
var sqlFileStream = new SqlFileStream(path, transactionContext, FileAccess.Read);
var buffer = new byte[4];
sqlFileStream.Read(buffer, 0, 4);
data = BitConverter.ToInt32(buffer, 0);
}
}
private static byte[] GetTransactionContext(SqlConnection connection, SqlTransaction transaction)
{
using (var cmd = connection.CreateCommand())
{
const string myGetTxContextQuery = "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()";
cmd.CommandText = myGetTxContextQuery;
cmd.CommandTimeout = 60;
cmd.CommandType = CommandType.Text;
cmd.Transaction = transaction;
return (byte[])cmd.ExecuteScalar();
}
}
An exception is thrown when trying to construct the SqlFileStream instance:
System.ComponentModel.Win32Exception occurred Message="The request is not supported" Source="System.Data" ErrorCode=-2147467259 NativeErrorCode=50
Anyone know what I'm doing wrong?