无法读取SqlFileStream数据

时间:2014-09-29 05:25:07

标签: c# asp.net sql-server sqlfilestream

我想从Sql数据库中读取Filestream列。阅读后我想在TextBox中显示文件内容。在.txt文件的情况下,它工作正常,但在其他类型,如pfd或doc,它是不可读的。

以下是代码:

SqlConnection objSqlCon = new SqlConnection(constr);
objSqlCon.Open();
SqlTransaction objSqlTran = objSqlCon.BeginTransaction();
SqlCommand objSqlCmd = new SqlCommand("_p_CV_Download", objSqlCon, objSqlTran);
objSqlCmd.CommandType = CommandType.StoredProcedure;

SqlParameter objSqlParam1 = new SqlParameter("@Branch", SqlDbType.NVarChar);
objSqlParam1.Value = Session["Branch"].ToString();
objSqlCmd.Parameters.Add(objSqlParam1);

SqlParameter objSqlParam2 = new SqlParameter("@Doc_No", SqlDbType.VarChar);
objSqlParam2.Value = (dataItem["Doc_No"].FindControl("DocNoLabel") as Label).Text;
objSqlCmd.Parameters.Add(objSqlParam2);

string path = string.Empty;
string fileType = string.Empty;
SqlDataReader sdr;
using (sdr = objSqlCmd.ExecuteReader())
{
    while (sdr.Read())
    {
        path = sdr[0].ToString();
        fileType = sdr[1].ToString();
    }
}
objSqlCmd = new SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()", objSqlCon, objSqlTran);
byte[] objContext = (byte[])objSqlCmd.ExecuteScalar();

SqlFileStream objSqlFileStream = new SqlFileStream(path, objContext, FileAccess.Read, FileOptions.SequentialScan, 0);
byte[] buffer = new byte[(int)objSqlFileStream.Length];
objSqlFileStream.Read(buffer, 0, buffer.Length);
string results = System.Text.Encoding.ASCII.GetString(buffer);
ListBox1.Items.Add(results);
objSqlFileStream.Close();
objSqlTran.Commit();

0 个答案:

没有答案