다음을 통해 공유


GET_FILESTREAM_TRANSACTION_CONTEXT(Transact-SQL)

적용 대상:SQL Server

세션의 현재 트랜잭션 컨텍스트를 나타내는 토큰을 반환합니다. 애플리케이션은 이 토큰을 사용하여 FILESTREAM 파일 시스템 스트리밍 작업을 트랜잭션에 바인딩합니다. FILESTREAM 문서 목록은 Blob(Binary Large Object) 데이터(SQL Server)를 참조하세요.

Transact-SQL 구문 표기 규칙

구문

GET_FILESTREAM_TRANSACTION_CONTEXT()

반환 형식

varbinary(max)

반환 값

NULL 는 트랜잭션이 시작되지 않았거나 취소되거나 커밋된 경우 반환됩니다.

설명

트랜잭션은 명시적이어야 합니다. 다음에 BEGIN TRANSACTION 사용 COMMIT TRANSACTION 하거나 ROLLBACK TRANSACTION.

호출 GET_FILESTREAM_TRANSACTION_CONTEXT하면 호출자에게 트랜잭션 기간 동안 트랜잭션에 대한 파일 시스템 액세스 권한이 부여됩니다. 다른 사용자가 파일 시스템을 통해 트랜잭션에 액세스할 수 있도록 허용하려면 다른 사용자로 실행하는 GET_FILESTREAM_TRANSACTION_CONTEXT 데 사용합니다EXECUTE AS.

예제

다음 예에서는 Transact-SQL 트랜잭션에 GET_FILESTREAM_TRANSACTION_CONTEXT를 사용하여 트랜잭션 컨텍스트를 구합니다.

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