Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Berlaku untuk:SQL Server
Mengembalikan token yang mewakili konteks transaksi sesi saat ini. Token digunakan oleh aplikasi untuk mengikat operasi streaming sistem file FILESTREAM ke transaksi. Untuk daftar artikel FILESTREAM, lihat Data Objek Besar Biner (Blob) (SQL Server).
Sintaks
GET_FILESTREAM_TRANSACTION_CONTEXT()
Tipe pengembalian
varbinary(max)
Mengembalikan nilai
NULL dikembalikan jika transaksi belum dimulai, atau telah dibatalkan atau dilakukan.
Keterangan
Transaksi harus eksplisit. Gunakan BEGIN TRANSACTION diikuti oleh COMMIT TRANSACTION atau ROLLBACK TRANSACTION.
Ketika Anda memanggil GET_FILESTREAM_TRANSACTION_CONTEXT, pemanggil diberikan akses sistem file ke transaksi selama durasi transaksi. Untuk mengizinkan pengguna lain mengakses transaksi melalui sistem file, gunakan EXECUTE AS untuk menjalankan GET_FILESTREAM_TRANSACTION_CONTEXT sebagai pengguna lain.
Contoh
Contoh berikut menggunakan GET_FILESTREAM_TRANSACTION_CONTEXT dalam transaksi Transact-SQL untuk mendapatkan konteks transaksi.
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);
}
}
}