Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi
I'm trying to upload files to Oracle database and retrieve it. However, I didn't find it a comprehensive example to follow. There are some codes I didn't understand .
.Please have a look at my codes and help me to do it.
Thank you
Example I followed : http://csharp-guide.blogspot.co.uk/search/label/INSERT%20BLOB[^]>
My code :


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Configuration;
using System.Data;
using System.IO;

public partial class Lecturer_Resources_to_upload : System.Web.UI.Page

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void cmdUpload_Click(object sender, EventArgs e)
    {
        try
        {
            if (fileUploadDocument.PostedFile.ContentLength > 0)
            {
                // Get the File name and Extension
                string FileName = Path.GetFileName(fileUploadDocument.PostedFile.FileName);
                string FileExtension = Path.GetExtension(fileUploadDocument.PostedFile.FileName);
                //
                // Extract the content of the Document into a Byte array
                int intlength = fileUploadDocument.PostedFile.ContentLength;
                Byte[] byteData = new Byte[intlength];
                fileUploadDocument.PostedFile.InputStream.Read(byteData, 0, intlength);
                //
                // Save the file to the DB
                string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
                objConn = new OracleConnection(strConn);
                //
                  StringBuilder strQueryBuilder = new StringBuilder("INSERT INTO RESOURCES(RESOURCES_ID, FILE_NAME, TYPE, RESOURCE_FILE) VALUES (");
                strQueryBuilder.Append("'1', ");
                strQueryBuilder.Append("'" + FileName + "', ");
                strQueryBuilder.Append("'" + FileExtension + "', ");
                strQueryBuilder.Append(" :RESOURCE_FILE)");

                String strQuery = strQueryBuilder.ToString();
                //
                OracleParameter blobParameter = new OracleParameter();
                blobParameter.ParameterName = "resources_file";
                blobParameter.OracleType = OracleType.Blob;
                blobParameter.Direction = ParameterDirection.Input;
                blobParameter.Value = byteData;

                objCmd = new OracleCommand(strQuery, objConn);
                objCmd.Parameters.Add(blobParameter);
                //
                objConn.Open();
                objCmd.ExecuteNonQuery();
                objConn.Close();
                //
                lblMsg.Text = "Document Uploaded Succesfully";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = " Error uploading Document: " + ex.Message.ToString();
        }
    }
}


The codes where Im getting the errors :
C#
StringBuilder strQueryBuilder = new StringBuilder("INSERT INTO RESOURCES(RESOURCES_ID, FILE_NAME, TYPE, RESOURCE_FILE) VALUES (");
               strQueryBuilder.Append("'1', ");
               strQueryBuilder.Append("'" + FileName + "', ");
               strQueryBuilder.Append("'" + FileExtension + "', ");
               strQueryBuilder.Append(" :RESOURCE_FILE)");

               String strQuery = strQueryBuilder.ToString();
               //
               OracleParameter blobParameter = new OracleParameter();
               blobParameter.ParameterName = "Resources_FILE";
               blobParameter.OracleType = OracleType.Blob;
               blobParameter.Direction = ParameterDirection.Input;
               blobParameter.Value = byteData;

               objCmd = new OracleCommand(strQuery, objConn);
               objCmd.Parameters.Add(blobParameter);
               //
               objConn.Open();
               objCmd.ExecuteNonQuery();
               objConn.Close();


The issue I have are these codes above are all underlined Red. It gives me an error that they don't exist in the current context
Posted
Updated 28-Feb-14 6:01am
v4

string in c# is immutable - cant be changed - so the
C#
string += 'blah' 
isn't going to work

I suggest you google 'stringbuilder', and rewrite your query sql using that - btw, your string is 'Query' but your doing an INSERT ? maybe you could pick a better name
 
Share this answer
 
Comments
aa00101 28-Feb-14 1:00am    
the issue are these codes : objConn, strQuery , objCmd,strFileExtension,strFileExtension give me an error that they don't exist in the current context. I dont know what to do to solve it.
Garth J Lancaster 28-Feb-14 1:09am    
look at this :-

// Get the File name and Extension
string FileName = Path.GetFileName(fileUploadDocument.PostedFile.FileName);
string FileExtension = Path.GetExtension(fileUploadDocument.PostedFile.FileName);

Now Look At This

strQuery += "'" + strFileName + "', ";
strQuery += "'" + strFileExtension + "', ";

You're using 'FileName/FileExtension' in one place and 'strFileName/strFileExtension' in the other

C#
// Get the File name and Extension
                string FileName = Path.GetFileName(fileUploadDocument.PostedFile.FileName);
                string FileExtension = Path.GetExtension(fileUploadDocument.PostedFile.FileName);
                //
                // Extract the content of the Document into a Byte array
                int intlength = fileUploadDocument.PostedFile.ContentLength;
                Byte[] byteData = new Byte[intlength];
                fileUploadDocument.PostedFile.InputStream.Read(byteData, 0, intlength);
                //
                // Save the file to the DB
                string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
                objConn = new OracleConnection(strConn);
                //
                 strQuery = "INSERT INTO RESOURCES(RESOURCES_ID, FILE_NAME, TYPE, RESOURCE_FILE) VALUES (";
                strQuery += "'1', ";
                strQuery += "'" + strFileName + "', ";
                strQuery += "'" + strFileExtension + "', ";
                strQuery += " :RESOURCE_FILE)";


look at

string FileName
string FileExtension

vs later on

+ strFileName
+ strFileExtension

these don't invalidate my comment about using Stringbuilder btw
 
Share this answer
 
Comments
aa00101 28-Feb-14 1:25am    
Yes It was before strFileName = Path.GetFileName and strFileExtension = Path.GetExtension, that's how it was in the example I was following but because it was showing an error as the other codes that it doesn't exist,then I decided to change it from str to string but nothing work so far
replace your strQuery build sequence with this

C#
StringBuilder strQueryBuilder = new StringBuilder("INSERT INTO RESOURCES(RESOURCES_ID, FILE_NAME, TYPE, RESOURCE_FILE) VALUES (");
strQueryBuilder.Append("'1', ");
strQueryBuilder.Append("'" + FileName + "', ");
strQueryBuilder.Append("'" + FileExtension + "', ");
strQueryBuilder.Append(" :RESOURCE_FILE)");

String strQuery = strQueryBuilder.ToString();


I just did it with dummy/null defs for FileName and FileExtension and it compiled
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900