Guest User

Untitled

a guest
Aug 16th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. SQL Server FileStream how to populate the filestream column
  2. con.Open();
  3. string sql = "INSERT INTO MyFsTable VALUES (@fData, @fName, default)";
  4. SqlCommand cmd = new SqlCommand(sql, con);
  5. cmd.Parameters.Add("@fData", SqlDbType.Image, fileData.Length).Value = fileData;
  6. cmd.Parameters.Add("@fName", SqlDbType.NVarChar).Value = fi.Name;
  7. cmd.ExecuteNonQuery();
  8. con.Close();
  9.  
  10. 5: if (FileUpload1.FileContent.Length > 0)
  11. 6: {
  12. 7: SqlConnection objSqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
  13. 8: objSqlCon.Open();
  14. 9: SqlTransaction objSqlTran = objSqlCon.BeginTransaction();
  15. 10:
  16. 11: SqlCommand objSqlCmd = new SqlCommand("FileAdd",objSqlCon,objSqlTran);
  17. 12: objSqlCmd.CommandType = CommandType.StoredProcedure;
  18. 13:
  19. 14: SqlParameter objSqlParam1 = new SqlParameter("@SystemNumber", SqlDbType.Int);
  20. 15: objSqlParam1.Value = "1";
  21. 16:
  22. 17: SqlParameter objSqlParam2 = new SqlParameter("@FileType", SqlDbType.VarChar,4);
  23. 18: objSqlParam2.Value = System.IO.Path.GetExtension(FileUpload1.FileName);
  24. 19:
  25. 20: SqlParameter objSqlParamOutput = new SqlParameter("@filepath", SqlDbType.VarChar, -1);
  26. 21: objSqlParamOutput.Direction = ParameterDirection.Output;
  27. 22:
  28. 23: objSqlCmd.Parameters.Add(objSqlParam2);
  29. 24: objSqlCmd.Parameters.Add(objSqlParam1);
  30. 25: objSqlCmd.Parameters.Add(objSqlParamOutput);
  31. 26:
  32. 27:
  33. 28: objSqlCmd.ExecuteNonQuery();
  34. 29:
  35. 30: string Path = objSqlCmd.Parameters["@filepath"].Value.ToString();
  36. 31:
  37. 32: objSqlCmd = new SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()", objSqlCon, objSqlTran);
  38. 33:
  39. 34: byte[] objContext = (byte[])objSqlCmd.ExecuteScalar();
  40. 35:
  41. 36:
  42. 37: SqlFileStream objSqlFileStream = new SqlFileStream(Path, objContext, FileAccess.Write);
  43. 38:
  44. 39: objSqlFileStream.Write(buffer, 0, buffer.Length);
  45. 40: objSqlFileStream.Close();
  46. 41:
  47. 42: objSqlTran.Commit();
Add Comment
Please, Sign In to add comment