Read and Write BLOB Data to a Database Table with ODP.NET
page 4 of 7
by Steven Swafford
Feedback
Average Rating: 
Views (Total / Last 10 Days): 101938/ 172

The Code Behind the Web Form Which Allows Your Data Collection

[ Download Code ]

There are two things that I must bring you attention to before you take a look at the source code.

1. I am using ODP.NET for the database communication. Take the time to read a previous article titled Using Oracle Data Provide for .NET
2. I have also wrote a utilities class to handle common functionality. This utility class will be discussed further in the article.

// Framework Import(s)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.IO;
using System.Text;


// Application Import(s)
using ASPAlliance.Utilities;


namespace ASPAllianceArticles
{
 /// <summary>
 /// Summary description for Article570.
 /// </summary>
 public class Article570 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button UploadButton;
  protected System.Web.UI.WebControls.Label NotificationLabel;
  protected System.Web.UI.WebControls.RequiredFieldValidator UploadValidator;
  protected System.Web.UI.HtmlControls.HtmlInputFile FileToUpload;
  protected System.Web.UI.WebControls.TextBox AuthorTextBox;
  protected System.Web.UI.WebControls.TextBox DescriptionTextBox;
  protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
  protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;


  #region Static Constants


  // Static Constants
  private const string GENERAL_EXCEPTION_MESSAGE = "An exception has occured: ";
  private const string INVALID_FILE_FORMAT = "The file format you attempted to upload" +
   " is not a valid format! Only JPEG. GIF, and BMP file formats are allowed.";


  #endregion
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // Put user code to initialize the page here
  }


  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {    
   this.UploadButton.Click += new System.EventHandler(this.UploadButton_Click);
   this.Load += new System.EventHandler(this.Page_Load);


  }
  #endregion


      #region Upload Button Event


  private void UploadButton_Click(object sender, System.EventArgs e)
  {


   OracleConnection dbConn = null;
         string author = AuthorTextBox.Text.Trim();
         string imageDescription = DescriptionTextBox.Text.Trim();
   int imgLength = 0;
   string imgContentType = null;
   string imgFileName = null;
   string sqlStmt = null;
   bool success = false;


         ASPAlliance.Utilities.OracleDatabaseHelper dbHelper =
            new ASPAlliance.Utilities.OracleDatabaseHelper();
 
         try
         {
            // Establish the database connection
            dbConn = dbHelper.openDatabaseConnection();


      // Image attributes
   Stream imgStream = FileToUpload.PostedFile.InputStream;
   imgLength = FileToUpload.PostedFile.ContentLength;
   imgContentType = FileToUpload.PostedFile.ContentType;
      imgFileName = FileToUpload.PostedFile.FileName;


      // validate the file type is acceptable
   if (imgContentType == "image/jpeg" || imgContentType == "image/gif" || 
   imgContentType == "image/pjpeg"
     || imgContentType == "image/bmp")
    {


    Byte[] ImageContent = new byte[imgLength]; int intStatus; intStatus = 
    imgStream.Read(ImageContent, 0, imgLength);


    // define the sql to perform the database insert
    sqlStmt = "INSERT INTO smstestblob (id, photo, author," +
      "description ) VALUES (smstestblobid_seq.nextval, :1, :2, :3)";


    // Establish a new OracleCommand
    OracleCommand cmd = new OracleCommand();


    // Set command to create your SQL statement
    cmd.CommandText = sqlStmt;


    // Set the OracleCommand to your database connection
    cmd.Connection = dbConn;
        
    // Set the command type to text
    cmd.CommandType = CommandType.Text;


    OracleParameter paramImage = new OracleParameter("image",
      OracleDbType.Blob);
    paramImage.Value = ImageContent;
    paramImage.Direction = ParameterDirection.Input;
    cmd.Parameters.Add(paramImage);


    OracleParameter paramAuthor = new OracleParameter("author",
      OracleDbType.Varchar2, 100);
    paramAuthor.Value = author;
    paramAuthor.Direction = ParameterDirection.Input;
    cmd.Parameters.Add(paramAuthor);


    OracleParameter paramDescription = new OracleParameter("imgDesc",
      OracleDbType.Varchar2, 500);
    paramDescription.Value = imageDescription;
    paramDescription.Direction = ParameterDirection.Input;
    cmd.Parameters.Add(paramDescription);


    // Execute the SQL Statement
    cmd.ExecuteNonQuery();
   
    NotificationLabel.Text = "Upload Successful";
    NotificationLabel.Visible = true;
    success = true;
    }
    else
    {
     NotificationLabel.Text = INVALID_FILE_FORMAT;
     NotificationLabel.Visible = true;
    }
   }
         catch(OracleException ex)
         {
            NotificationLabel.Text = GENERAL_EXCEPTION_MESSAGE + ex.Message;
            NotificationLabel.Visible = true;
         }
         finally
         {
            // Close the database connection
            dbHelper.closeDatabaseConnection(dbConn);
         }


   // redirect only if the boolean value of success is true
   if (success)
   {
    Response.Redirect("Article570_1.aspx");
   }


  }


      #endregion


 }
}

Now I will discuss the utility class that I previously spoke of.


View Entire Article

User Comments

Title: Read/Write BLOB data when file > 32K   
Name: Roger Rowe
Date: 3/7/2005 1:03:00 PM
Comment:
Go see this article:
http://support.microsoft.com/default.aspx?scid=kb;en-us;322796
This shows you how to insert larger data - your version will die for data larger than 32K - a PLSQL limitation).






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 8/31/2014 12:16:06 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search