Working with Oracle's BLOB and Microsoft's C#


We used this title for describing technologies , we are working in.

Here we will see
How to store files in oracle database using c# ?  "


Step 1:
          Use file uploader controller to browse directories.

Step 2:
         Before uploading file or its path , we need to understand BLOB of Oracle

         BLOB  ( Binary Large Objects ) : 
                                                  File content store in binary format.In Oracle , BLOB and    CLOB  (Character Large Object) used.Technically BLOB prefered.For more info regarding BLOB click here .


Step 3 : 
      In C# , use System.IO namespace for file operations.
   
Step 4:
        string path = FileUpload1.PostedFile.FileName.ToString();
        string FileName = FileUpload1.FileName.ToString();
        string FileSize = FileUpload1.PostedFile.ContentLength.ToString();
        string FileContent = FileUpload1.PostedFile.ContentType.ToString();
        string filename = Path.GetFileName(path);

        /* FileStream that used to handle reading file path , file mode setting  etc*/        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
        int streamLength = (int)fs.Length;
        BinaryReader br = new BinaryReader(fs);  
        byte[] bytes = br.ReadBytes((Int32)fs.Length);   // Binary reader need to store values in byte array        byte[] _data = File.ReadAllBytes(path);     // This is another way of reading values of direct file

      //Declare Oracle connections and Oracle command
       OracleBlob myLob = new OracleBlob(conn);           
       //Transfer data to server       myLob.Write(_data, 0, streamLength);
       //Perform INSERT query       cmd = new OracleCommand(query, conn);
       OracleParameter myParam = cmd.Parameters.Add("fileContent", OracleDbType.Blob);
       myParam.Value = myLob;
      cmd.ExecuteNonQuery();



Step 5 :

    After storing file into database , you need to retrieve it for dowloading.
   
   - Write " Select query  " for retrieving all details.
   - Get all details in binarty format , for that refer below code,
         
            cmd = new OracleCommand();
            cmd.CommandText = query;           
            cmd.Connection = conn;
            OracleDataReader dr = cmd.ExecuteReader();
            byte[] data = null;
            if (dr.Read())
            {
                if (dr["DOCCONTENT"] != null)   // Column name
                {
                    data = (byte[])dr["DOCCONTENT"];       // Column name
                }
            } 


     -  Read data in byte array
                 
            Response.ContentType = _fileContentType.ToString();
            Response.AddHeader("Content-Disposition", "attachment;filename=\""+fileName+"\"");
            Response.BinaryWrite((byte[])_data);  // Byte array receive data            Response.End();    
  

Happy Coding...........Enjoy./....


                
     



Comments

Popular posts from this blog

Add Serial no in crystal report without coding

File operations in C# (.net)

SQL – Generate decimal numbers sequence for particular number range