Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
Van toepassing op:SQL Server
Hiermee wordt een token geretourneerd dat de huidige transactiecontext van een sessie vertegenwoordigt. Het token wordt door een toepassing gebruikt om STREAMING-bewerkingen van het FILESTREAM-bestandssysteem te binden aan de transactie. Zie Binary Large Object (Blob) Data (SQL Server) voor een lijst met FILESTREAM-artikelen.
Transact-SQL syntaxis-conventies
Syntaxis
GET_FILESTREAM_TRANSACTION_CONTEXT()
Retourtypen
varbinary(max)
Retourwaarde
NULL wordt geretourneerd als de transactie niet is gestart of is geannuleerd of doorgevoerd.
Opmerkingen
De transactie moet expliciet zijn. Gebruik BEGIN TRANSACTION gevolgd door COMMIT TRANSACTION of ROLLBACK TRANSACTION.
Wanneer u belt GET_FILESTREAM_TRANSACTION_CONTEXT, krijgt de beller toegang tot het bestandssysteem voor de transactie gedurende de duur van de transactie. Als u een andere gebruiker toegang wilt geven tot de transactie via het bestandssysteem, gebruikt EXECUTE AS u deze om als de andere gebruiker uit te voeren GET_FILESTREAM_TRANSACTION_CONTEXT .
Voorbeelden
In het volgende voorbeeld wordt GET_FILESTREAM_TRANSACTION_CONTEXT gebruikgemaakt van een Transact-SQL transactie om de transactiecontext te verkrijgen.
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);
}
}
}