Filestream access has just got simpler
SP1 of the .Net Framework 3.5 brought us a nice
improvement in the usability of filestream data in managed code.
Within the System.Data.SqlTypes namespace there is now a class called
SqlFilestream. This makes it very easy to use filestream data as this inherits
from the Stream class and so can be used to instantite a reader or
writer of your choice.
In this code we create a StreamReader using a SqlFileStream.
SqlConnection
con = new SqlConnection("<your connection string>");
con.Open();
SqlCommand
command = new SqlCommand(
"select Top(1) fsCol.PathName(),
"
+ "GET_FILESTREAM_TRANSACTION_CONTEXT ()
"
+ "from fsTab (nolock)", con);
command.Transaction
= con.BeginTransaction(
System.Data.IsolationLevel.ReadCommitted);
using
(SqlDataReader reader =
command.ExecuteReader())
{
while (reader.Read())
{
// Create the
SqlFileStream
StreamReader rs = new StreamReader
(new SqlFileStream(reader.GetString(0),
reader.GetSqlBinary(1).Value,
FileAccess.Read,
FileOptions.SequentialScan,
0));
// Read the contents as bytes and write them
to the console
while
(!rs.EndOfStream)
{
MessageBox.Show(rs.ReadLine());
}
rs.Close();
}
}
command.Transaction.Commit();
-