共用方式為


GET_FILESTREAM_TRANSACTION_CONTEXT (Transact-SQL)

適用於:SQL Server

傳回代表工作階段之目前交易內容的 Token。 應用程式會使用此 Token 將 FILESTREAM 檔案系統資料流作業繫結至此交易。 如需 FILESTREAM 文章的清單,請參閱二進位大型物件 (Blob) 資料 (SQL Server)。

Transact-SQL 語法慣例

語法

GET_FILESTREAM_TRANSACTION_CONTEXT()

傳回類型

varbinary(max)

返回值

NULL 如果交易尚未啟動,或已取消或認可,則會傳回。

備註

交易必須是明確的。 後面接 BEGIN TRANSACTIONCOMMIT 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);
        }
    }
}