示例#1
0
 private static void SaveDocImageFile(string clientPath, string serverPath, byte[] serverTxn)
 {
     const int BlockSize = 1024 * 512;
     using (FileStream source = new FileStream(clientPath, FileMode.Open, FileAccess.Read))
     {
         using (SqlFileStream dest = new SqlFileStream(serverPath, serverTxn, FileAccess.Write))
         {
             byte[] buffer = new byte[BlockSize];
             int bytesRead;
             while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
             {
                 dest.Write(buffer, 0, bytesRead);
                 dest.Flush();
             }
             dest.Close();
         }
         source.Close();
     }
 }
 private void SaveDocImageByteArray(byte[] pDataBytes, string pServerPath, byte[] pServerTxn)
 {
     const int BlockSize = 1024 * 512;
     using (MemoryStream memStream = new MemoryStream(pDataBytes))
     //using (FileStream source = new FileStream(pFileNameAndPath, FileMode.Open, FileAccess.Read))
     {
         using (SqlFileStream dest = new SqlFileStream(pServerPath, pServerTxn, FileAccess.Write))
         {
             byte[] buffer = new byte[BlockSize];
             int bytesRead;
             while ((bytesRead = memStream.Read(buffer, 0, buffer.Length)) > 0)
             {
                 dest.Write(buffer, 0, bytesRead);
                 dest.Flush();
             }
             dest.Close();
         }
         //source.Close();
     }
 }
        public void Update(TradeRqmtConfirmBlobDto pData)
        {
            const string updateTSql =
                @"UPDATE TRADE_RQMT_CONFIRM_BLOB
                SET TRADE_RQMT_CONFIRM_ID = @TRADE_RQMT_CONFIRM_ID, IMAGE_FILE_EXT = @IMAGE_FILE_EXT
                WHERE TRADE_RQMT_CONFIRM_ID = @TRADE_RQMT_CONFIRM_ID
                SELECT TOP(1) DOC_BLOB.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() 
                FROM TRADE_RQMT_CONFIRM_BLOB
                WHERE TRADE_RQMT_CONFIRM_ID = @TRADE_RQMT_CONFIRM_ID";

            using (TransactionScope ts = new TransactionScope())
            {
                using (SqlConnection conn = new SqlConnection(sqlConnStr))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand(updateTSql, conn))
                    {
                        //cmd.Parameters.Add("@ID", SqlDbType.Int).Value = pData.Id;
                        cmd.Parameters.Add("@TRADE_RQMT_CONFIRM_ID", SqlDbType.VarChar).Value = pData.TradeRqmtConfirmId;
                        cmd.Parameters.Add("@IMAGE_FILE_EXT", SqlDbType.VarChar).Value = DBUtils.ValueStringOrDBNull(pData.ImageFileExt.ToUpper());
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                // Get the pointer for file 
                                string path = reader.GetString(0);
                                byte[] transactionContext = reader.GetSqlBytes(1).Buffer;

                                // Create the SqlFileStream
                                using (Stream fileStream = new SqlFileStream(path, transactionContext, FileAccess.Write, FileOptions.SequentialScan, allocationSize: 0))
                                {
                                    // Write a byte array to the file. This will replace any data in the file.
                                    //fileStream.WriteByte(0x01);
                                    fileStream.Write(pData.DocBlob, 0, pData.DocBlob.Length);
                                }
                            }
                        }
                    }
                }
                ts.Complete();
            }
        }
        public void TestOverwriteFilestream(Int32 pImageId, byte[] pImageDataBytes)
        {
            const string InsertTSql = @"
                SELECT TOP(1) DocImage.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() 
                FROM InboundImages
                WHERE ImageId = @ImageId";
            using (SqlConnection conn = new SqlConnection(sqlConnStr))
            {
                conn.Open();

                //SqlCommand command = new SqlCommand("SELECT TOP(1) Photo.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() FROM employees", connection);
                //SqlCommand command = new SqlCommand(InsertTSql, conn);
                using (SqlCommand cmd = new SqlCommand(InsertTSql, conn))
                {
                    SqlTransaction tran = conn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                    cmd.Transaction = tran;

                    cmd.Parameters.Add("@ImageId", SqlDbType.Int).Value = pImageId;
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            // Get the pointer for file 
                            string path = reader.GetString(0);
                            byte[] transactionContext = reader.GetSqlBytes(1).Buffer;

                            // Create the SqlFileStream
                            using (Stream fileStream = new SqlFileStream(path, transactionContext, FileAccess.Write, FileOptions.SequentialScan, allocationSize: 0))
                            {
                                // Write a single byte to the file. This will
                                // replace any data in the file.
                                //fileStream.WriteByte(0x01);
                                fileStream.Write(pImageDataBytes, 0, pImageDataBytes.Length);
                            }
                        }
                    }
                    tran.Commit();
                }
            }
        }
示例#5
0
        public int SavePhoto(int productId, byte[] imageFile, string createdBy)
        {
            int photoId = 0;
            string filePath = null;
            byte[] txContext = null;
            int width, height;
            byte[] hash;

            using (var memStream = new MemoryStream(imageFile))
            {
                using (var b = new Bitmap(memStream))
                {
                    width = b.Width;
                    height = b.Height;
                }
            }

            using (var hashGenerator = MD5.Create())
            {
                hash = hashGenerator.ComputeHash(imageFile);
            }

            using (var connection = new SqlConnection(
                ConfigurationManager.ConnectionStrings["PhotosDB"].ConnectionString))
            {
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                {
                    using (var cmd = new SqlCommand("SavePhoto", connection, transaction))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add("@productId", SqlDbType.Int).Value = productId;
                        cmd.Parameters.Add("@createdBy", SqlDbType.NVarChar, 50).Value = createdBy;

                        cmd.Parameters.Add("@widthInPixels", SqlDbType.Int).Value = width;
                        cmd.Parameters.Add("@heightInPixels", SqlDbType.Int).Value = height;
                        cmd.Parameters.Add("@lengthInBytes", SqlDbType.Int).Value = imageFile.Length;

                        cmd.Parameters.Add("@contentType", SqlDbType.VarChar, 255).Value = MimeType.GetMimeType(imageFile, null);
                        cmd.Parameters.Add("@md5Checksum", SqlDbType.VarBinary, 16).Value = hash;

                        using (var sdr = cmd.ExecuteReader())
                        {
                            if (sdr.Read())
                            {
                                filePath = sdr.GetValue<string>("PathName", String.Empty);
                                photoId = sdr.GetValue<int>("Id");
                                txContext = (byte[])sdr["txContext"];
                            }
                        }
                    }

                    if (!String.IsNullOrWhiteSpace(filePath))
                    {
                        using (var sqlStream = new SqlFileStream(filePath, txContext, FileAccess.Write))
                        {
                            sqlStream.Write(imageFile, 0, imageFile.Length);
                        }
                    }

                    transaction.Commit();
                }
            }

            return photoId;
        }
示例#6
0
 private static void WriteSqlFileStream(string pServerPath, byte[] pServerTxn, Stream source)
 {
     using (var dest = new SqlFileStream(pServerPath, pServerTxn, FileAccess.Write))
     {
         const int BlockSize = 1024*512;
         var buffer = new byte[BlockSize];
         int bytesRead;
         while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
         {
             dest.Write(buffer, 0, bytesRead);
             dest.Flush();
         }
         dest.Close();
     }
 }