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

Utility Class to Handle Common Functionality

[ Download Code ]

While this is not necessary I find it useful to separate my common functionality into it's own project. For this example I am using this utility class for my database handling. You could easily integrated thing such as sending emails, logging errors, and string functions to name a few.

In this class I have two methods and a constant string that I have defined for the database connection string. Once again I am taking advantage of the ability to define values in a single local and in the case where I define the database connection string this value is stored in the Web.config file, of which below is an example.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="OracleConnectionString" 
     value="User ID=userid;Password=password;Data Source=datasource" />
  </appSettings>
</configuration>

Now to reference this connection string in you class simply use the System.Configuration namespace. Be sure to import your ODP.NET Oracle namespaces as well.

using System;
using System.Data;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.Configuration;
using System.Collections;


namespace ASPAlliance.Utilities
{
 /// <summary>
 /// Summary description for OracleDatabaseHelper.
 /// </summary>
 public class OracleDatabaseHelper
 {


      // Read the connection strings from the configuration file
      // Note: Modify User Id, Password, Data Source per your
      // database setup this is contained with the Web.config
      public static readonly string CONN_STRING = 
      ConfigurationSettings.AppSettings["OracleConnectionString"];


      /// <summary>
      /// Establishes your database connection
      /// </summary>
      /// <returns>a database connection instance</returns>
      public OracleConnection openDatabaseConnection()
      {
         OracleConnection dbConn = new OracleConnection(CONN_STRING);
         dbConn.Open();


         return dbConn;
      }


      public void closeDatabaseConnection(OracleConnection dbConn)
      {
         // Check if connection is open or closed
         if (dbConn != null)
         {
            if (dbConn.State == ConnectionState.Open)
            {
               dbConn.Close();
               dbConn.Dispose();
            }
         }
      }


 }
}

Now when you are ready to instantiate this class an example would be as so.

ASPAlliance.Utilities.OracleDatabaseHelper dbHelper = 
new ASPAlliance.Utilities.OracleDatabaseHelper();

In the code behind the web form you will notice when I have my database connection wrapped in a try/catch/finally statement. I will not go into the details of this rather if you are not familiar with try-catch-finally statement read Customizing Error Pages and C# Programmers Reference: try-catch-finally.

Example:

 try
         { 


            // Establish the database connection
            dbConn = dbHelper.openDatabaseConnection();


            // Execute the SQL Statement
            cmd.ExecuteNonQuery();
   
         }
         catch(OracleException ex)
         {
            NotificationLabel.Text = ex.Message;
            NotificationLabel.Visible = true;
         }
         finally
         {
            // Close the database connection
            dbHelper.closeDatabaseConnection(dbConn);
         }

At this point you should be able to execute this application in your web browser and store an image and the other relevant information in the database and in this case the database is an Oracle instance. From here the next step is to create another web form which will display the stored images.


View Entire Article

User Comments

Title: Read/Write BLOB data when file > 32K   
Name: Roger Rowe
Date: 2005-03-07 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-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 6:48:59 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search