SQL Server FileStream如何填充文件流列

发布于 2024-12-13 02:44:54 字数 2638 浏览 2 评论 0原文

我遇到了几种将数据插入 SQL Server(对于 FileStream)的不同方法。插入 FileStream 对象的最佳方法是什么?以下方法之间的主要区别是,一种方法直接进行插入,另一种方法放置 FileStream 对象的占位符。

一种方法是他们通过 C# 直接插入文档:

链接:文件流

  con.Open();
  string sql = "INSERT INTO MyFsTable VALUES (@fData, @fName, default)";
  SqlCommand cmd = new SqlCommand(sql, con);
  cmd.Parameters.Add("@fData", SqlDbType.Image, fileData.Length).Value = fileData;
  cmd.Parameters.Add("@fName", SqlDbType.NVarChar).Value = fi.Name;
  cmd.ExecuteNonQuery();
  con.Close();

另一种方法是插入一行,但将文档(FileStream 列)保留为空。我必须输入一个虚拟值,因为当 FileStream 列为空时,我的“获取文件路径”调用返回 Null:

链接:文件流

  5: if (FileUpload1.FileContent.Length > 0)
   6: {
   7:     SqlConnection objSqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
   8:     objSqlCon.Open();
   9:     SqlTransaction objSqlTran = objSqlCon.BeginTransaction();
  10:  
  11:     SqlCommand objSqlCmd = new SqlCommand("FileAdd",objSqlCon,objSqlTran);
  12:     objSqlCmd.CommandType = CommandType.StoredProcedure;
  13:  
  14:     SqlParameter objSqlParam1 = new SqlParameter("@SystemNumber", SqlDbType.Int);
  15:     objSqlParam1.Value = "1";
  16:  
  17:     SqlParameter objSqlParam2 = new SqlParameter("@FileType", SqlDbType.VarChar,4);
  18:     objSqlParam2.Value = System.IO.Path.GetExtension(FileUpload1.FileName);
  19:  
  20:     SqlParameter objSqlParamOutput = new SqlParameter("@filepath", SqlDbType.VarChar, -1);
  21:     objSqlParamOutput.Direction = ParameterDirection.Output;
  22:  
  23:     objSqlCmd.Parameters.Add(objSqlParam2);
  24:     objSqlCmd.Parameters.Add(objSqlParam1);
  25:     objSqlCmd.Parameters.Add(objSqlParamOutput);
  26:  
  27:  
  28:     objSqlCmd.ExecuteNonQuery();
  29:  
  30:     string Path = objSqlCmd.Parameters["@filepath"].Value.ToString();
  31:  
  32:     objSqlCmd = new SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()", objSqlCon, objSqlTran);
  33:  
  34:     byte[] objContext = (byte[])objSqlCmd.ExecuteScalar();
  35:     
  36:  
  37:     SqlFileStream objSqlFileStream = new SqlFileStream(Path, objContext, FileAccess.Write);
  38:     
  39:     objSqlFileStream.Write(buffer, 0, buffer.Length);
  40:     objSqlFileStream.Close();
  41:  
  42:     objSqlTran.Commit();

I have come a across a few different methods for inserting data into SQL Server (For FileStream). What is the best method for inserting the FileStream objects? The main difference between the approaches below being one directly did the insert and the other put in a place holder for the FileStream object.

One approach is that they were directly inserting the document via C# through an insert:

Link: FileStream

  con.Open();
  string sql = "INSERT INTO MyFsTable VALUES (@fData, @fName, default)";
  SqlCommand cmd = new SqlCommand(sql, con);
  cmd.Parameters.Add("@fData", SqlDbType.Image, fileData.Length).Value = fileData;
  cmd.Parameters.Add("@fName", SqlDbType.NVarChar).Value = fi.Name;
  cmd.ExecuteNonQuery();
  con.Close();

Another approach they inserted a row, but left the document (FileStream Column) null. I had to put in a dummy value because when the FileStream column was null my Get File Path call returned Null:

Link: FileStream

  5: if (FileUpload1.FileContent.Length > 0)
   6: {
   7:     SqlConnection objSqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
   8:     objSqlCon.Open();
   9:     SqlTransaction objSqlTran = objSqlCon.BeginTransaction();
  10:  
  11:     SqlCommand objSqlCmd = new SqlCommand("FileAdd",objSqlCon,objSqlTran);
  12:     objSqlCmd.CommandType = CommandType.StoredProcedure;
  13:  
  14:     SqlParameter objSqlParam1 = new SqlParameter("@SystemNumber", SqlDbType.Int);
  15:     objSqlParam1.Value = "1";
  16:  
  17:     SqlParameter objSqlParam2 = new SqlParameter("@FileType", SqlDbType.VarChar,4);
  18:     objSqlParam2.Value = System.IO.Path.GetExtension(FileUpload1.FileName);
  19:  
  20:     SqlParameter objSqlParamOutput = new SqlParameter("@filepath", SqlDbType.VarChar, -1);
  21:     objSqlParamOutput.Direction = ParameterDirection.Output;
  22:  
  23:     objSqlCmd.Parameters.Add(objSqlParam2);
  24:     objSqlCmd.Parameters.Add(objSqlParam1);
  25:     objSqlCmd.Parameters.Add(objSqlParamOutput);
  26:  
  27:  
  28:     objSqlCmd.ExecuteNonQuery();
  29:  
  30:     string Path = objSqlCmd.Parameters["@filepath"].Value.ToString();
  31:  
  32:     objSqlCmd = new SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()", objSqlCon, objSqlTran);
  33:  
  34:     byte[] objContext = (byte[])objSqlCmd.ExecuteScalar();
  35:     
  36:  
  37:     SqlFileStream objSqlFileStream = new SqlFileStream(Path, objContext, FileAccess.Write);
  38:     
  39:     objSqlFileStream.Write(buffer, 0, buffer.Length);
  40:     objSqlFileStream.Close();
  41:  
  42:     objSqlTran.Commit();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声情话 2024-12-20 02:44:54

请参阅 FILESTREAM MVC:从 SQL Server 下载和上传图像 示例展示了如何以高效、面向流的方式上传和下载文件流。您所追求的方法,分配整个上传文件大小的缓冲区(我假设您在提供内容时也做了同样的事情)效率非常低,您的 ASP 进程内存将被如此大的字节[]撕成碎片运营。

See FILESTREAM MVC: Download and Upload images from SQL Server for an example showing how to upload and download filestream in an efficient, stream oriented manner. The approach you're pursuing, allocating a buffer the size of the entire uploaded file (and I assume you do the same on serving the content too) is very inefficient, your ASP process memory will be shred to pieces by such large byte[] operations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文