Click here to Skip to main content
15,896,479 members
Articles / Programming Languages / C#

ADO.NET for the Object-Oriented Programmer – Part Two

Rate me:
Please Sign up or sign in to vote.
4.96/5 (61 votes)
19 Jan 2006CPOL28 min read 339K   3.5K   240  
In this second part of the article series, we will revisit the ADO.NET CRUD operations. Only this time, we will use an application with far better architecture. Once that is done, we will move on to data binding with objects.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ObjectDataBinding.Properties;

namespace ObjectDataBinding.DataAccess
{
    public static class DataProvider
    {
    	#region Declarations

        // Member variables
        private static string m_ConnectionString = String.Empty;

        #endregion

    	#region Constructor

        static DataProvider()
        {
            m_ConnectionString = Settings.Default.ConnectionString;
        }

    	#endregion

    	#region Methods

        /// <summary>
        /// Insert, update, or delete a record in the database.
        /// </summary>
        /// <param name="sqlQuery">The SQL query to run against the database.</param>
        /// <returns>The number of rows affected by the operation.</returns>
        public static int ExecuteNonQuery(string sqlQuery)
        {
            // Create and open a connection
            SqlConnection connection = new SqlConnection(m_ConnectionString);
            connection.Open();

            // Create and configure a command
            SqlCommand command = new SqlCommand(sqlQuery, connection);

            // Execute the command
           int numRowsAffected = command.ExecuteNonQuery();

            // Close and dispose
            command.Dispose();
            connection.Close();
            connection.Dispose();

            // Set return value
            return numRowsAffected;
       }

        /// <summary>
        /// Execute a query that returns a scalar value
        /// </summary>
       /// <param name="sqlQuery">The SQL query to run against the database </param>
        /// <returns>The scalar value returned by the database.</returns>
        public static int ExecuteScalar(string sqlQuery)
        {
            // Create and open a connection
            SqlConnection connection = new SqlConnection(m_ConnectionString);
            connection.Open();

            // Create and configure a command
            SqlCommand command = new SqlCommand(sqlQuery, connection);

            // Execute the command
            int result = Convert.ToInt32(command.ExecuteScalar());

            // Close and dispose
            command.Dispose();
            connection.Close();
            connection.Dispose();

            // Set return value
            return result;
        }

        /// <summary>
        /// Retrieve a data set.
        /// </summary>
        /// <param name="sqlQuery">The SQL Select query to run against the database. </param>
        /// <returns>A populated data set.</returns>
        /// <remarks>This method uses a connection string passed in as an argument
        /// to the constructor for this class.</remarks>
        public static DataSet GetDataSet(string sqlQuery)
        {
            // Create dataset
            DataSet dataSet = new DataSet();

            // Populate dataset
            using (SqlConnection connection = new SqlConnection(m_ConnectionString))
            {
                SqlCommand command = connection.CreateCommand();
                command.CommandText = sqlQuery;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataSet);
            }

            // return dataset
            return dataSet;
        }

        #endregion
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Foresight Systems
United States United States
David Veeneman is a financial planner and software developer. He is the author of "The Fortune in Your Future" (McGraw-Hill 1998). His company, Foresight Systems, develops planning and financial software.

Comments and Discussions