Partager via


GET_FILESTREAM_TRANSACTION_CONTEXT (Transact-SQL)

S'applique à :SQL Server

Retourne un jeton qui représente le contexte de transaction actuel d'une session. Le jeton est utilisé par une application pour lier des opérations de diffusion en continu de système de fichiers FILESTREAM à la transaction. Pour obtenir la liste des articles FILESTREAM, consultez les données d’objet blob (Blob) binaires (SQL Server).

Conventions de la syntaxe Transact-SQL

Syntaxe

GET_FILESTREAM_TRANSACTION_CONTEXT()

Types de retour

varbinary(max)

Valeur retournée

NULL est retourné si la transaction n’a pas été démarrée ou a été annulée ou validée.

Notes

La transaction doit être explicite. Utiliser BEGIN TRANSACTION suivi ou COMMIT TRANSACTIONROLLBACK TRANSACTION.

Lorsque vous appelez GET_FILESTREAM_TRANSACTION_CONTEXT, l’appelant reçoit l’accès du système de fichiers à la transaction pendant la durée de la transaction. Pour autoriser un autre utilisateur à accéder à la transaction via le système de fichiers, utilisez-le EXECUTE AS pour s’exécuter GET_FILESTREAM_TRANSACTION_CONTEXT en tant qu’autre utilisateur.

Exemples

L’exemple suivant utilise GET_FILESTREAM_TRANSACTION_CONTEXT dans une transaction Transact-SQL pour obtenir le contexte de transaction.

using System;
using System.Data.SqlClient;
using System.Data;

namespace ConsoleApplication
{
    /// <summary>
    /// This class is a wrapper that contains methods for:
    ///
    ///     GetTransactionContext() - Returns the current transaction context.
    ///     BeginTransaction() - Begins a transaction.
    ///     CommitTransaction() - Commits the current transaction.
    ///
    /// </summary>

    class SqlAccessWrapper
    {
        /// <summary>
        /// Returns a byte array that contains the current transaction
        /// context.
        /// </summary>
        /// <param name="sqlConnection">
        /// SqlConnection object that represents the instance of SQL Server
        /// from which to obtain the transaction context.
        /// </param>
        /// <returns>
        /// If there is a current transaction context, the return
        /// value is an Object that represents the context.
        /// If there is not a current transaction context, the
        /// value returned is DBNull.Value.
        /// </returns>

        public Object GetTransactionContext(SqlConnection sqlConnection)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = sqlConnection;

            return cmd.ExecuteScalar();

        }

        /// <summary>
        /// Begins the transaction.
        /// </summary>
        /// <param name="sqlConnection">
        /// SqlConnection object that represents the server
        /// on which to run the BEGIN TRANSACTION statement.
        /// </param>

        public void BeginTransaction(SqlConnection sqlConnection)
        {
            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = "BEGIN TRANSACTION";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = sqlConnection;

            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// Commits the transaction.
        /// </summary>
        /// <param name="sqlConnection">
        /// SqlConnection object that represents the instance of SQL Server
        /// on which to run the COMMIT statement.
        /// </param>

        public void CommitTransaction(SqlConnection sqlConnection)
        {
            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = "COMMIT TRANSACTION";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = sqlConnection;

            cmd.ExecuteNonQuery();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //Open a connection to the local instance of SQL Server.

            SqlConnection sqlConnection = new SqlConnection("Integrated Security=true;server=(local)");
            sqlConnection.Open();

            SqlAccessWrapper sql = new SqlAccessWrapper();

            //Create a transaction so that sql.GetTransactionContext() will succeed.
            sql.BeginTransaction(sqlConnection);

            //The transaction context will be stored in this array.
            Byte[] transactionToken;

            Object txObj = sql.GetTransactionContext(sqlConnection);
            if (DBNull.Value != txObj)
            {
                transactionToken = (byte[])txObj;
                Console.WriteLine("Transaction context obtained.\n");
            }

            sql.CommitTransaction(sqlConnection);
        }
    }
}