Overview

Today, we will see how to upload files, whether it's a Word file, pdf, zip files etc., and save them in the Oracle database as well as retrieve those files and download those. This code is useful when you need to upload various documents in an organization, including process documents, news and so on. Other users will have to download those files and see the contents. To see the content of what you have uploaded, you have to save it in Binary format. Let's start:

uploaded

Step 1 : Lets create a table first

First, you need to install the Oracle Database on your machine and create databases in your system. The Oracle SQL Developer is a GUI to access those databases.

Once you are through with the installation and creation of the database, it is time to add a connection.

Note:

Before installing Oracle, you need to add TNS names in tnsnames.ora in order to communicate with your databases.

Go to your system drive where you had installed the Oracle. This is my path:

path

Open tnsname.ora file and add a TNS entry there.

code

Now, configure the connection in your Oracle SQL Developer.

SQL

Click on Connect. It will add a connection successfully. We have connected to our database successfully .

database

Now, let's create a table.

table

Look at the Data type in Oracle carefully. I made ID as primary key. Oracle doesn’t identify the GUI. So, you have to write a query for this .

The name I gave is Varchar2 with a length of 4000 Bytes (maximum).
Content type is again varchar2 which is again 4000 Bytes long (maximum).

Data is stored in BLOB. ( Remember in the previous article in SQL server we had been storing data in Binary Format). Here, in Oracle, we will be saving Data in BLOB .

Once we have created the table, it's time to create id as autoincrement.

Just type this command,

command

It will create a sequence, doc_id. Now, just see by selecting the statement if your sequence has been created or not .

command

Step 2 : Open Visual Studio

Open Visual Studio File->New Website.

New Website

Select ASP.NET Empty Website and Give suitable name as DocumentSaveInBinary,

DocumentSaveInBinary

Now, let's create FileUpload Control as,

code

  1. <asp:FileUpload ID="FileUpload1" runat="server" />
  2. <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" CssClass="btn-primary" />
Now, create Gridview with Download Link button, so that we can download respective documents or Files .

code
  1. <asp:GridView ID="GridView1" runat="server"
  2. AutoGenerateColumns="false" CssClass="table">
  3. <Columns>
  4. <asp:BoundField DataField="Name" HeaderText="File Name" />
  5. <asp:TemplateField ItemStyle-HorizontalAlign="Center">
  6. <ItemTemplate>
  7. <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile"
  8. CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
  9. </ItemTemplate>
  10. </asp:TemplateField>
  11. </Columns>
  12. </asp:GridView>
Here you will see that inside Gridview used <asp:BoundField/> Showing HeaderText as FileName in gridview. In that <Itemtemplate></Itemtemplate> Inside Itemtemplate you need to bind Link button with ID=”lnkDownload” OnClick=”DownloadFile”.

So my final Document.aspx code is,
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DocumentUpload.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head id="Head1" runat="server">
  5. <title></title>
  6. <link rel="Stylesheet" href="Styles/bootstrap.min.css" style="" />
  7. <link rel="Stylesheet" href="Styles/bootstrap.css" />
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div class="container">
  12. <asp:FileUpload ID="FileUpload1" runat="server" />
  13. <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" CssClass="btn-primary" />
  14. <hr />
  15. <asp:GridView ID="GridView1" runat="server"
  16. AutoGenerateColumns="false" CssClass="table">
  17. <Columns>
  18. <asp:BoundField DataField="Name" HeaderText="File Name" />
  19. <asp:TemplateField ItemStyle-HorizontalAlign="Center">
  20. <ItemTemplate>
  21. <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile"
  22. CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
  23. </ItemTemplate>
  24. </asp:TemplateField>
  25. </Columns>
  26. </asp:GridView>
  27. </div>
  28. </form>
  29. </body>
  30. </html>
Step 3 : Now let's see CS Code Part

Our File Upload Code is :

code

As you see in the above code, we are saving our table FileName,Contentype; here the content types are word, pdf, image and so on and then we are saving the posted file in binary format by using Stream. File which you had uploaded in Fileupload Control and is converted in the binary file through BinaryReader .

Here, I have added a connection string in the code .

Now, while adding, you need to insert the id. See, what I had inserted: sequencename.next val

When we were inserting in SQL, we were using “@” . Here, in Oracle, we are using “:”:
  1. string constr = "User ID=axis_nsdl;Password=a$xis_4321;Data Source=TSTDPSEC";
  2. using (OracleConnection con = new OracleConnection(constr))
  3. {
  4. string query = "insert into tblFiles values (doc_id.nextval,:Name, :ContentType, :Data)";
  5. using (OracleCommand cmd = new OracleCommand(query))
  6. {
  7. cmd.Connection = con;
  8. // cmd.Parameters.AddWithValue(":id", Number);
  9. cmd.Parameters.AddWithValue(":Name", filename);
  10. cmd.Parameters.AddWithValue(":ContentType", contentType);
  11. cmd.Parameters.AddWithValue(":Data", bytes);
  12. con.Open();
  13. cmd.ExecuteNonQuery();
  14. con.Close();
  15. }
  16. }
  17. }
  18. }