Пример #1
0
        /// <summary>
        /// Initiates an asynchronous get of the binary file specified by fileId or fileGuid
        /// Intended to be used by an IHttpAsyncHandler
        /// </summary>
        /// <param name="callback">The callback.</param>
        /// <param name="context">The context.</param>
        /// <param name="fileGuid">The file unique identifier.</param>
        /// <param name="fileId">The file identifier.</param>
        /// <returns></returns>
        private IAsyncResult BeginGet(AsyncCallback callback, HttpContext context, Guid fileGuid, int?fileId)
        {
            SqlConnection conn = new SqlConnection(string.Format("{0};Asynchronous Processing=true;", ConfigurationManager.ConnectionStrings["RockContext"].ConnectionString));

            conn.Open();
            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "spCore_BinaryFileGet";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Id", fileId.HasValue ? fileId.Value : 0));
            cmd.Parameters.Add(new SqlParameter("@Guid", fileGuid));

            // store our Command to be later retrieved by EndGet
            context.AddOrReplaceItem("Rock.Model.BinaryFileService:SqlCommand", cmd);

            // start async DB read
            return(cmd.BeginExecuteReader(
                       callback,
                       context,
                       CommandBehavior.SequentialAccess | // doesn't load whole column into memory
                       CommandBehavior.SingleRow |        // performance improve since we only want one row
                       CommandBehavior.CloseConnection)); // close connection immediately after read
        }