【发布时间】:2011-12-16 05:21:12
【问题描述】:
我第一次尝试使用 filestream 在文件系统上使用 varbinary(MAX) 列类型的 DB 存储 pdf 文件。
我已按照以下步骤操作。
- 在 SQL Server 2008 R2 上启用了文件流功能。
- 为 BLOB 存储创建文件组
- 创建的表包含 varbinary(max) 类型的 blob 列
现在,我想使用文件上传控件来选择文件,当点击上传按钮时,它应该保存 pdf 文件。另外,如何找回文件?
我试过下面的代码
protected void btnFSupload_Click(object sender, EventArgs e)
{
SqlConnection cn = null;
SqlTransaction tx = null;
SqlCommand cmd = null;
SqlCommand cmd2 = null;
bool bCommit = false;
try
{
// read in the file to be saved as a blob in the database
FileStream input = new FileStream(@"D:\swami.pdf", FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[(int)input.Length];
input.Read(buffer, 0, buffer.Length);
cn = new SqlConnection("server=at-hetal01\\sqlexpress;Initial Catalog=practice;Integrated Security=true;");
cn.Open();
tx = cn.BeginTransaction();
cmd = new SqlCommand("dbo.stp_AddBLOB", cn, tx);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlDataReader r = cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow);
r.Read();
string id = r[0].ToString();
string path = r[1].ToString();
r.Close();
// get the transaction context
cmd2 = new SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()", cn, tx);
Object obj = cmd2.ExecuteScalar();
byte[] txCtx = (byte[])obj;
// open the filestream to the blob
SafeFileHandle handle = OpenSqlFilestream(path,DESIRED_ACCESS_WRITE,SQL_FILESTREAM_OPEN_NO_FLAGS,txCtx,(UInt32)txCtx.Length,0);
// open a Filestream to write the blob
FileStream output = new FileStream(handle,FileAccess.Write,buffer.Length,false);
output.Write(buffer,0,buffer.Length);
output.Close();
if (handle != null && !handle.IsClosed)
handle.Close();
bCommit = true;
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if (cn != null)
{
switch (bCommit)
{
case true:
tx.Commit();
break;
case false:
tx.Rollback();
break;
}
cn.Close();
}
}
}
上面的代码显示错误如下
操作系统在“D:\DB\FS\d11132f8-c2a8-452d-ae0c-208164a550d7”上尝试“NtCreateFile”时返回错误“0xc000003a({Path Not Found}路径 %hs 不存在。)” \beb8e1f1-8116-440b-870b-7cef4281a15d\0000001c-000000e4-010d'。该语句已终止。
那么,这有什么线索吗?
【问题讨论】:
-
谁能帮我解决这个问题?
标签: pdf filestream