SQL Server FileStream how to populate the filestream column

冷暖自知 提交于 2019-12-23 12:28:02

问题


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();

回答1:


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.



来源:https://stackoverflow.com/questions/7973829/sql-server-filestream-how-to-populate-the-filestream-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!