Click here to Skip to main content
15,886,519 members
Articles / Programming Languages / SQL

Stop writing connection management code every time you access the database

Rate me:
Please Sign up or sign in to vote.
3.88/5 (18 votes)
6 Jan 20068 min read 161.8K   2.3K   80  
A simple class library for database access without the pervasive and often-fragile connection management details.
/*
 * AMS.ADO Class Library
 * Version 1.0
 * 
 * Written by Alvaro Mendez
 * Copyright (c) 2005. All Rights Reserved.
 * 
 * The AMS.ADO namespace contains ICommand interface.
 * 
 * The code is thoroughly documented, however, if you have any questions, 
 * feel free to email me at alvaromendez@consultant.com.  Also, if you 
 * decide to this in a commercial application I would appreciate an email 
 * message letting me know.
 *
 * This code may be used in compiled form in any way you desire. This
 * file may be redistributed unmodified by any means providing it is 
 * not sold for profit without the authors written consent, and 
 * providing that this notice and the authors name and all copyright 
 * notices remains intact. This file and the accompanying source code 
 * may not be hosted on a website or bulletin board without the author's 
 * written permission.
 * 
 * This file is provided "as is" with no expressed or implied warranty.
 * The author accepts no liability for any damage/loss of business that
 * this product may cause.
 *
 * Last Updated: Dec. 22, 2005
 */

using System;
using System.Data;

namespace AMS.ADO
{
    /// <summary>
    ///   Generic representation of an SQL command to be executed against a database. </summary>
    public interface ICommand : IDbCommand
    {
        #region Properties

        /// <summary>
        ///   Gets or sets the connection string to use when connecting to the 
        ///   database where the command will be executed. </summary>
        string ConnectionString
        {
            get;
            set;
        }

        #endregion // Properties

        #region Execute Methods

        /// <summary>
        ///   Opens a temporary database connection, executes the query, and 
        ///   returns the results inside a DataSet object. </summary>
        /// <param name="tables">
        ///   The array of names of the source tables to use for table mapping. </param>
        /// <returns>
        ///   The return value is a DataSet object. </returns>
        DataSet ExecuteDataSet(params string[] tables);

        /// <summary>
        ///   Opens a temporary database connection, executes the query, and 
        ///   appends the results to a DataSet object. </summary>
        /// <param name="ds">
        ///   The DataSet object to be filled with the results of the cmd. </param>
        /// <param name="tables">
        ///   The array of names of the source tables to use for table mapping. </param>
        /// <returns>
        ///   The return value is the same DataSet object passed in as a parameter. </returns>
        DataSet ExecuteDataSet(DataSet ds, params string[] tables);

        /// <summary>
        ///   Calls <see cref="IDbCommand.ExecuteScalar" /> and casts the result to a string. </summary>
        /// <returns>
        ///   The return value is the first column of the first row in the result set
        ///   converted to a string.  If the first column of the first row is DBNull.Value, the return
        ///   value is an empty string.  If the result set is empty, the return value is null. </returns>
        string ExecuteString();

        /// <summary>
        ///   Calls <see cref="IDbCommand.ExecuteScalar" /> and casts the result to an int. </summary>
        /// <returns>
        ///   The return value is the first column of the first row in the result set
        ///   cast to an int.  If the first column of the first row is DBNull.Value or cannot be
        ///   converted to an int, the return value is 0.  If the result set is empty, 
        ///   the return value is 0.  </returns>
        int ExecuteInt();

        #endregion // Execute Methods
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I've done extensive work with C++, MFC, COM, and ATL on the Windows side. On the Web side, I've worked with VB, ASP, JavaScript, and COM+. I've also been involved with server-side Java, which includes JSP, Servlets, and EJB, and more recently with ASP.NET/C#.

Comments and Discussions