Database
  Home arrow Database arrow Page 3 - Portable Database Functions
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE

Portable Database Functions
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 40
    2003-12-29

    Table of Contents:
  • Portable Database Functions
  • Tool #1: Quick Execution
  • Tool #2: How Fast Can You Count?
  • Tool#3: I'll Use My Own ID, Thank You
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Portable Database Functions - Tool #2: How Fast Can You Count?


    (Page 3 of 5 )

     

    Quite often, I find it necessary to determine a number if items in a table. This happens when I want to make sure something exists (the count is greater than 0). Trying to perform work on a non-existent result will give me an error, so I must first make sure I have something to work with! Or perhaps I know something exists, and I just need the total. For instance, I know I have lots of green socks in my inventory; I need to know many, and perhaps whether or not I should therefore be ordering more green socks.

     

    Here’s a simple way to do this:

     

    Dim intCount, strSQL

     

    strSQL = "SELECT COUNT( * )

    FROM socks WHERE color = ‘green’"

     

    set objRS = server.CreateObject("ADODB.RecordSet")

    objRs.Open strSQL2, strConnect

           intCount = objRS( 0 )

    objRS.Close()

    Set objRS = Nothing

     

    If intCount < 100 Then

     Response.Write( “You’d better order more green socks!” )

    Else

     Response.Write( “You have lots of green socks” )

    End If

     

    Now, we’re in the same position as before. This is not the most difficult thing to manage, but suppose you have to do it for green, red, orange, yellow, and puce socks all individually? Now it becomes repetitious. So, here we go:

     

    '=====================================
    function getCount( strTable, strCriteria, altString )
    '=====================================
    dim strSQL2, objRS
    strSQL2 = ""
    if trim( altString ) = "" then
    if strTable = "" then call errorMessage( "Invalid table!" )
    strSQL2 = "SELECT COUNT( * ) FROM " & strTable
    if strCriteria <> "" then strSQL2 = strSQL2 & " WHERE " & strCriteria
    else
    strSQL2 = altString
    end if
    'debug( strSQL2 )
    set objRS = server.CreateObject("ADODB.RecordSet")
    objRs.Open strSQL2, Application("MySQL_inventory")
    getCount = objRS( 0 )
    objRS.Close()
    Set objRS = Nothing
    strSQL2 = null
    end function

     

    Now watch how this simplifies our life:

     

    If getCount( “socks”, “color = ‘green’”, “” ) < 100

      Then Response.Write( “Almost out of green socks!” )

    End If

     

    Here’s another example:

     

    If getCount( “users”, “email = ‘me@here.com’”, “” ) = 0

      Then strErrorMessage = “Sorry, that email address

      does not exist in our records.”

    End If

     

     

    There, we’ve done it again. Six lines of code reduced to one function call! 

     

    But you ask: “What if I am a good RDBA and have normalized my tables to the 3rd normal form? ‘strTable’ is insufficient when I’m performing JOINs and the like!”

     

    This is very true, and that’s where the altString parameter comes in. We could create our SQL statement separately, with all the fancy JOINs we need. Something like this:

     

    strSQL = “SELECT COUNT( s.id ) FROM socks s “ & _

             “INNER JOIN colors c ON c.id = s.color_id “ & _

             “WHERE c.description = ‘” & strColor & “’”

     

    if getCount( “”, “”, strSQL ) < 100

      Then Response.Write( “Almost out of green socks!” )

     

    This little function has simplified my life to the extent that I now only have to spend three minutes of my day coding, and the rest of if writing for ASP Free! A slight exaggeration, but it’s very useful, I kid you not.

     

    Now it’s worth noting that in our first function, commitToDB(), one of the parameters (strDB) allowed us to perform the procedure using the default connection, or specify our own. It would be simple to add this feature to the getCount() function: just add the extra parameter, and the default setting within the function.  There are two reasons why it’s not already there:

    1. I copied the function from an application where I’m only referencing one database. I had no need for the optional parameter, so I took it out to save myself writing the extra ‘, “” ’ every time.
    2. So that I could highlight the simplicity of modifying these tools to fit your needs. Aren’t I nice?

    Now let’s take a look at the third tool.

    More Database Articles
    More By Justin Cook


     

    DATABASE ARTICLES

    - Excel Reference
    - Database Programming in C# with MySQL : Usin...
    - Formatting Techniques for Data Access from E...
    - Data Access from Excel VBA
    - Generating a Multiple Table Crystal Report u...
    - ADO and the Command Object
    - On Wiring Up an ADO Data Control
    - Reading and Writing to Files on the Intranet
    - Using ADO Record to Create and Navigate Intr...
    - Using Data Access Pages to Access Data on a ...
    - Using ADO with the SQL Native Client
    - ADO`s Stream Object
    - Opening a Record Object Referencing an Open ...
    - Introducing Jasper (SQL Anywhere 10 Beta)
    - Creating a Database Project in VS 2005





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT