WEB开发网
开发学院数据库MSSQL Server 使用SQL Server 2008的FILESTREAM特性管理文件 阅读

使用SQL Server 2008的FILESTREAM特性管理文件

 2009-02-26 10:21:54 来源:WEB开发网   
核心提示: 清单2 使用FILESTREAM读取数据,这个click事件处理程序从数据库读取FILESTREAM列中的内容,使用SQL Server 2008的FILESTREAM特性管理文件(7),private void btnReadFile_Click(object sender, Event

清单2 使用FILESTREAM读取数据。这个click事件处理程序从数据库读取FILESTREAM列中的内容。

private void btnReadFile_Click(object sender, EventArgs e)
{
string connectionString =  ConfigurationManager.ConnectionStrings
["fileStreamDB"].ConnectionString;               
using (SqlConnection connection = new
SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
//Get the PathName of the File from the database
command.CommandText = "SELECT Picture.PathName(), "
+ "GET_FILESTREAM_TRANSACTION_CONTEXT() FROM Product "
+ "WHERE ProductID = 1";
SqlTransaction transaction =
connection.BeginTransaction(IsolationLevel.ReadCommitted);
command.Transaction = transaction;
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{                       
string path = reader.GetString(0);
SqlFileStream stream = new SqlFileStream(path,
(byte[])reader.GetValue(1),FileAccess.Read,
FileOptions.SequentialScan, 0);                       
lstResults.Items.Clear();
int length = (int) stream.Length;
byte[] contents = new byte[length];
stream.Read(contents,0,length);                    
string results = System.Text.Encoding.ASCII.GetString
(contents);
lstResults.Items.Add(results);
stream.Close();
}
}
transaction.Commit();
}
}

上一页  2 3 4 5 6 7 8 9  下一页

Tags:使用 SQL Server

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接