Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Gilt für:SQL Server
Gibt ein Token zurück, das den aktuellen Transaktionskontext einer Sitzung darstellt. Eine Anwendung verwendet dieses Token, um FILESTREAM-Dateisystem-Streamingvorgänge an die Transaktion zu binden. Eine Liste der FILESTREAM-Artikel finden Sie unter Binary Large Object (Blob) Data (SQL Server).For a list of FILESTREAM articles, see Binary Large Object (Blob) Data (SQL Server)
Transact-SQL-Syntaxkonventionen
Syntax
GET_FILESTREAM_TRANSACTION_CONTEXT()
Rückgabetypen
varbinary(max)
Rückgabewert
NULL wird zurückgegeben, wenn die Transaktion nicht gestartet wurde oder abgebrochen oder zugesichert wurde.
Hinweise
Die Transaktion muss explizit sein. Verwenden Sie BEGIN TRANSACTION gefolgt von COMMIT TRANSACTION oder ROLLBACK TRANSACTION.
Beim Aufrufen GET_FILESTREAM_TRANSACTION_CONTEXTwird dem Aufrufer der Dateisystemzugriff auf die Transaktion für die Dauer der Transaktion gewährt. Um einem anderen Benutzer den Zugriff auf die Transaktion über das Dateisystem zu ermöglichen, verwenden Sie EXECUTE AS die Ausführung GET_FILESTREAM_TRANSACTION_CONTEXT als anderer Benutzer.
Beispiele
Im folgenden Beispiel wird GET_FILESTREAM_TRANSACTION_CONTEXT in einer Transact-SQL-Transaktion verwendet, um den Transaktionskontext abzurufen.
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);
}
}
}