17.07.2015 Views

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3: Accessing <strong>FILESTREAM</strong> Data from Client Applications4. Read/Write <strong>FILESTREAM</strong> data – Read or write <strong>FILESTREAM</strong> data using the file I/Ofunctions.5. Close the <strong>FILESTREAM</strong> data file – Gracefully close the <strong>FILESTREAM</strong> data file.6. Commit/rollback the transaction – Commit or roll back the transaction when youhave finished.Let's examine each <strong>of</strong> these steps in detail.Step 1: Starting a transactionAll streaming access to <strong>FILESTREAM</strong> data, even just read access, must be done withinthe context <strong>of</strong> a database transaction so, before accessing the <strong>FILESTREAM</strong> data, yourapplication should start a <strong>SQL</strong> <strong>Server</strong> transaction.<strong>The</strong> exact manner in which you start a transaction on the current database connectionwill vary depending on the client library in use (ADO.NET, Open Database Connectivity,etc.). <strong>The</strong>re is nothing <strong>FILESTREAM</strong>-specific in this step, you can just begin a transactionin the same way as for a T-<strong>SQL</strong> DML operation.We will see more detailed examples and fully functional sample code later in thischapter but, for now, Listing 3-1 shows a simple example <strong>of</strong> how to start a transactionon an ADO.NET connection object using C#.// Create a SqlConnection objectSqlConnection con = new SqlConnection("database=NorthPole;server=(local);integrated security=sspi;");//Open a connection to the databasecon.Open();//Start a transactionSqlTransaction transaction = con.BeginTransaction("ItemTran");Listing 3-1:Using C# to start a transaction on an ADO.NET connection object.91

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!