Exemplo n.º 1
0
        public void SaveThumbnail(int id, string fileName, System.IO.Stream strm)
        {
            var product = this.DB.Products.Where(a => a.ProductID == id).FirstOrDefault();

            if (product == null)
            {
                throw new Exception("Product is not found");
            }

            TransactionOptions topts = new System.Transactions.TransactionOptions();

            topts.Timeout        = TimeSpan.FromSeconds(60);
            topts.IsolationLevel = System.Transactions.IsolationLevel.Serializable;
            using (TransactionScope trxScope = new TransactionScope(TransactionScopeOption.Required, topts))
                using (DbConnection conn = DBConnectionFactory.GetRIAppDemoConnection())
                {
                    System.IO.BinaryReader br = new System.IO.BinaryReader(strm);
                    byte[]     bytes          = br.ReadBytes(64 * 1024);
                    string     fldname        = "ThumbNailPhoto";
                    BlobStream bstrm          = new BlobStream(conn as SqlConnection, "[SalesLT].[Product]", fldname, string.Format("WHERE [ProductID]={0}", id));
                    bstrm.InitColumn();
                    bstrm.Open();
                    while (bytes != null && bytes.Length > 0)
                    {
                        bstrm.Write(bytes, 0, bytes.Length);
                        bytes = br.ReadBytes(64 * 1024);;
                    }
                    bstrm.Close();
                    br.Close();
                    trxScope.Complete();
                }

            product.ThumbnailPhotoFileName = fileName;
            this.DB.SubmitChanges();
        }
Exemplo n.º 2
0
        public void SaveThumbnail2(int id, string fileName, Func <Stream, Task> copy)
        {
            var product = DB.Products.Where(a => a.ProductID == id).FirstOrDefault();

            if (product == null)
            {
                throw new Exception("Product is not found");
            }

            var topts = new TransactionOptions();

            topts.Timeout        = TimeSpan.FromSeconds(60);
            topts.IsolationLevel = IsolationLevel.Serializable;
            using (var trxScope = new TransactionScope(TransactionScopeOption.Required, topts))
                using (var conn = DBConnectionFactory.GetRIAppDemoConnection())
                {
                    var fldname = "ThumbNailPhoto";
                    var bstrm   = new BlobStream(conn as SqlConnection, "[SalesLT].[Product]", fldname,
                                                 string.Format("WHERE [ProductID]={0}", id));
                    bstrm.InitColumn();
                    bstrm.Open();
                    try
                    {
                        if (!copy(bstrm).Wait(10000))
                        {
                            throw new Exception("Write stream timeout");
                        }
                    }
                    finally
                    {
                        bstrm.Close();
                    }
                    trxScope.Complete();
                }

            product.ThumbnailPhotoFileName = fileName;
            DB.SaveChanges();
        }