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(); } }
private static void play_video() { string video_name; Console.WriteLine("Play Video =========="); list_video(); Console.Write("Insert Video Name : "); video_name = Console.ReadLine(); Console.Write("Retrieving Video ..."); using (TransactionScope transactionScope = new TransactionScope()) { SqlConnection sqlConnection = connect_database(); using (SqlCommand command = new SqlCommand()) { command.Connection = sqlConnection; command.CommandType = System.Data.CommandType.Text; command.CommandText = String.Format("SELECT FileData.PathName() As Path, GET_FILESTREAM_TRANSACTION_CONTEXT() As TransactionContext FROM {0} WHERE description=@video_name", table_name); command.Parameters.AddWithValue("@video_name", video_name); try { SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { string filePath = (string) reader["Path"]; byte[] transactionContext = (byte[]) reader["TransactionContext"]; SqlFileStream sqlFileStream = new SqlFileStream(filePath,transactionContext,FileAccess.Read); byte[] video_data = new byte[sqlFileStream.Length]; sqlFileStream.Read(video_data,0,Convert.ToInt32(sqlFileStream.Length)); sqlFileStream.Close(); string filename = @"C:\Users\UA\Documents\Project\Database Systems\Program\temp\" + video_name; FileStream fs = new FileStream(filename,FileMode.Create,FileAccess.Write,FileShare.Write); fs.Write(video_data,0,video_data.Length); fs.Flush(); fs.Close(); Console.WriteLine("Successfull"); Console.Write("Playing Video ..."); Process process = new Process(); process.StartInfo.FileName = filename; process.Start(); } } catch (Exception e) { Console.WriteLine("{0}", e); } disconnect_database(sqlConnection); transactionScope.Complete(); } } }
private static Image LoadDocImage(string filePath, byte[] txnToken, string imageType) { Image docImage = null; Bitmap bmap; using (SqlFileStream sfs = new SqlFileStream(filePath, txnToken, FileAccess.Read)) { if (imageType.Equals("TIF")) docImage = Image.FromStream(sfs); else //if (imageType.Equals("PDF")) { //MemoryStream memStream = new MemoryStream(); //storeStream.SetLength(sfs.Length); //sfs.CopyTo(memStream); //docImage = Image.FromStream(memStream, false, false); //System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter(); //byte[] m_Bytes = ReadToEnd(memStream); //docImage = (Image)converter.ConvertFrom(m_Bytes); FileStream fstream = new FileStream(TEMP_FILENAME, FileMode.Create); sfs.CopyTo(fstream, 4096); //docImage = Image.FromFile(TEMP_FILENAME); docImage = Image.FromStream(fstream); //docImage = (Image)bmap; } sfs.Close(); } return docImage; }
private static void LoadDocImageIntoFile(string filePath, byte[] txnToken, string imageType) { using (SqlFileStream sfs = new SqlFileStream(filePath, txnToken, FileAccess.Read)) { string fileName = TEMP_FILENAME + imageType; using (FileStream fstream = new FileStream(fileName, FileMode.Create)) { sfs.CopyTo(fstream, 4096); } sfs.Close(); } }
private MensajeDto RecuperarDocumento(string nombre, string rutaDestino) { var mensajeConnString = RecuperarElconnectionStrings("ArchivosDb"); if (mensajeConnString.Error) { return mensajeConnString; } string CadConexion = mensajeConnString.Valor; using (SqlConnection conexionBD = new SqlConnection(CadConexion)) { try { conexionBD.Open(); if (conexionBD.State == ConnectionState.Open) { using (SqlTransaction transaccion = conexionBD.BeginTransaction()) { byte[] arrayContexto = null; string ubicacionFichero = string.Empty; string cadSql = string.Empty; //obtengo el PathName() cadSql = "SELECT DocumentoFile.PathName() from dbo.[ArchivosMovimientos] where NombreArchivo=@nombre"; using (SqlCommand cmdUbicacion = new SqlCommand(cadSql, conexionBD, transaccion)) { cmdUbicacion.Parameters.AddWithValue("@Nombre", nombre); object objUbicacion = cmdUbicacion.ExecuteScalar(); if (objUbicacion != null) { ubicacionFichero = objUbicacion.ToString(); } } if (!string.IsNullOrEmpty(ubicacionFichero)) { //todas las operaciones FILESTREAM BLOB deben ocurrir dentro del contexto de una transaccion cadSql = "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()"; using (SqlCommand cmdContexto = new SqlCommand(cadSql, conexionBD, transaccion)) { object objContexto = cmdContexto.ExecuteScalar(); if (objContexto != null) { arrayContexto = (byte[])objContexto; } } //obtener el handle que sera pasado al WIN32 API FILE using (SqlFileStream sqlFS = new SqlFileStream(ubicacionFichero, arrayContexto, FileAccess.Read)) { byte[] datosFichero = new byte[sqlFS.Length]; sqlFS.Read(datosFichero, 0, (int)sqlFS.Length); var rutaCompleta = Path.Combine(rutaDestino, nombre); File.WriteAllBytes(rutaCompleta, datosFichero); sqlFS.Close(); } } transaccion.Commit(); } } } catch (UnauthorizedAccessException ex) { return new MensajeDto() { Error = true, MensajeDelProceso = "No tiene autorizacion para acceder al recurso: " + ex.Message }; } catch (Exception ex) { return new MensajeDto() { Error = true, MensajeDelProceso = "Error: " + ex.Message }; } finally { if ((conexionBD != null) && (conexionBD.State == ConnectionState.Open)) { conexionBD.Close(); } } } return new MensajeDto() { Error = false, MensajeDelProceso = "Archivo generado", Valor = nombre }; }
public byte[] TestGetByteArray(int pImageId) { const string SelectTSql = @" SELECT Description, ImageType, DocImage.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() FROM InboundImages WHERE ImageId = @ImageId"; //Image docImage; string serverPath; byte[] serverTxn; byte[] returnVal; using (TransactionScope ts = new TransactionScope()) { using (SqlConnection conn = new SqlConnection(sqlConnStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(SelectTSql, conn)) { cmd.Parameters.Add("@ImageId", SqlDbType.Int).Value = pImageId; using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleRow)) { rdr.Read(); //desc = rdr.GetSqlString(0).Value; //fileext = rdr.GetSqlString(1).Value; serverPath = rdr.GetSqlString(2).Value; serverTxn = rdr.GetSqlBinary(3).Value; rdr.Close(); } } using (SqlFileStream sfs = new SqlFileStream(serverPath, serverTxn, FileAccess.Read)) { using (MemoryStream memStream = new MemoryStream()) { sfs.CopyTo(memStream); returnVal = memStream.ToArray(); } sfs.Close(); } } ts.Complete(); } return returnVal; }
public TradeRqmtConfirmBlobDto Get(Int32 pTradeRqmtConfirmId) { const string selectTSqlCmd = @"SELECT ID, TRADE_RQMT_CONFIRM_ID, IMAGE_FILE_EXT, DOC_BLOB.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() FROM TRADE_RQMT_CONFIRM_BLOB WHERE TRADE_RQMT_CONFIRM_ID = @TRADE_RQMT_CONFIRM_ID"; TradeRqmtConfirmBlobDto trqmtConfirmBlobResult = new TradeRqmtConfirmBlobDto(); string serverPath; byte[] serverTxn; byte[] blobByteArray; using (TransactionScope ts = new TransactionScope()) { using (SqlConnection conn = new SqlConnection(sqlConnStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(selectTSqlCmd, conn)) { cmd.Parameters.Add("@TRADE_RQMT_CONFIRM_ID", SqlDbType.Int).Value = pTradeRqmtConfirmId; using (SqlDataReader dataReader = cmd.ExecuteReader()) { if (dataReader.HasRows) { while (dataReader.Read()) { serverPath = dataReader.GetSqlString(3).Value; serverTxn = dataReader.GetSqlBinary(4).Value; using (SqlFileStream sqlFileStr = new SqlFileStream(serverPath, serverTxn, FileAccess.Read)) { using (MemoryStream memStream = new MemoryStream()) { sqlFileStr.CopyTo(memStream); blobByteArray = memStream.ToArray(); } sqlFileStr.Close(); } trqmtConfirmBlobResult.Id = DBUtils.HandleInt32IfNull(dataReader["ID"].ToString()); trqmtConfirmBlobResult.TradeRqmtConfirmId = DBUtils.HandleInt32IfNull(dataReader["TRADE_RQMT_CONFIRM_ID"].ToString()); trqmtConfirmBlobResult.ImageFileExt = dataReader["IMAGE_FILE_EXT"].ToString(); trqmtConfirmBlobResult.DocBlob = blobByteArray; } } } } } ts.Complete(); } return trqmtConfirmBlobResult; }
public void CaptureVaultedDocsBlob(VaultedDocs vaultedDoc) { string sql = String.Format("SELECT ID,DOC_BLOB.PathName(),GET_FILESTREAM_TRANSACTION_CONTEXT() FROM {0}VAULTED_DOCS_BLOB{1} WHERE ID={2}" , DbContext.SCHEMA_NAME, DbContext.NO_LOCK,vaultedDoc.VaultedDocsBlobId); using (TransactionScope ts = new TransactionScope()) { using (SqlConnection conn = new SqlConnection(_connectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(sql, conn)) { using (SqlDataReader dataReader = cmd.ExecuteReader()) { if (dataReader.HasRows) { while (dataReader.Read()) { using (SqlFileStream sqlFileStr = new SqlFileStream(dataReader.GetSqlString(1).Value, dataReader.GetSqlBinary(2).Value, FileAccess.Read)) { using (MemoryStream memStream = new MemoryStream()) { sqlFileStr.CopyTo(memStream); vaultedDoc.VaultedDocsBlobDocBlob = memStream.ToArray(); } sqlFileStr.Close(); } } } } } } ts.Complete(); } }
private static bool TransferFile(string clientFilePath, string serverFilePath, byte[] txnToken) { if (File.Exists(clientFilePath)) { File.Delete(clientFilePath); } SqlFileStream sfs = new SqlFileStream(serverFilePath, txnToken, FileAccess.Read); FileStream fStream = new FileStream(clientFilePath, FileMode.CreateNew); sfs.CopyTo(fStream, 8192); sfs.Close(); fStream.Close(); return true; }
private static byte[] ReadBytesFromSqlFileStream(string serverPath, byte[] serverTxn) { byte[] blobByteArray; using (var sqlFileStr = new SqlFileStream(serverPath, serverTxn, FileAccess.Read)) { using (var memStream = new MemoryStream()) { sqlFileStr.CopyTo(memStream); blobByteArray = memStream.ToArray(); } sqlFileStr.Close(); } return blobByteArray; }
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(); } }
private static Image LoadPhotoImage(string filePath, byte[] txnToken) { Image photo; using (SqlFileStream sfs = new SqlFileStream(filePath, txnToken, FileAccess.Read)) { photo = Image.FromStream(sfs); sfs.Close(); } return photo; }