GET_FILESTREAM_TRANSACTION_CONTEXT(Transact-SQL)

适用范围:SQL Server

返回表示会话的当前事务上下文的标记。 应用程序使用此标记可将 FILESTREAM 文件系统流式处理操作绑定到该事务。 有关 FILESTREAM 文章的列表,请参阅二进制大型对象(Blob)数据(SQL Server)。

Transact-SQL 语法约定

语法

GET_FILESTREAM_TRANSACTION_CONTEXT()

返回类型

varbinary(max)

返回值

如果事务尚未启动或已取消或已提交,则返回

备注

事务必须为显式的。 使用 BEGIN TRANSACTION 后跟 COMMIT TRANSACTIONROLLBACK TRANSACTION

调用 GET_FILESTREAM_TRANSACTION_CONTEXT时,调用方在事务持续时间内被授予对事务的文件系统访问权限。 若要允许其他用户通过文件系统访问事务,请使用 EXECUTE AS 其他用户身份运行 GET_FILESTREAM_TRANSACTION_CONTEXT

示例

下面的示例在 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);
        }
    }
}