Condividi tramite


GET_FILESTREAM_TRANSACTION_CONTEXT (Transact-SQL)

Si applica a:SQL Server

Restituisce un token che rappresenta il contesto di transazione corrente di una sessione. Il token viene utilizzato da un'applicazione per associare le operazioni di flusso di file system FILESTREAM alla transazione. Per un elenco di articoli FILESTREAM, vedere Dati blob (Binary Large Object) (DATI BLOB ) (SQL Server).For a list of FILESTREAM articles, see Binary Large Object (BLOB) Data (SQL Server).

Convenzioni relative alla sintassi Transact-SQL

Sintassi

GET_FILESTREAM_TRANSACTION_CONTEXT()

Tipi restituiti

varbinary(max)

Valore restituito

NULL viene restituito se la transazione non è stata avviata o è stata annullata o sottoposta a commit.

Osservazioni:

La transazione deve essere esplicita. Usare BEGIN TRANSACTION seguito da COMMIT TRANSACTION o ROLLBACK TRANSACTION.

Quando si chiama GET_FILESTREAM_TRANSACTION_CONTEXT, al chiamante viene concesso l'accesso del file system alla transazione per la durata della transazione. Per consentire a un altro utente di accedere alla transazione tramite il file system, usare EXECUTE AS per l'esecuzione GET_FILESTREAM_TRANSACTION_CONTEXT come altro utente.

Esempi

Nell'esempio seguente viene usato GET_FILESTREAM_TRANSACTION_CONTEXT in una transazione Transact-SQL per ottenerne il contesto.

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