10/12/09

Wrapper class for Database connectivity and query execution

Have you ever tried to to wrap up code when dealing with DataSets, SqlDataAdapters, SqlConnections and SqlTransactions??? It can be really tricky sometimes.

You can download the wrapper class here
It comes along with a test project, to check whether it meets your requirements. Make sure you change the Connection String in the app.config to point to your Database
 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="cnString" connectionString="YOUR CONNECTION STRING HERE" />
    </connectionStrings>
</configuration>
Also change the Application.Run(FORM TO RUN) to check out the corresponding form and method calls of the wrapper class

        /// <summary>
        /// The main entry point for the application.
        /// In Application.Run(new executeNonQuery_handle()) enter Command_Handle,     //Connection_handle,   //DataSet_handle, executeNonQuery_handle,
        /// Parameter_handle, SqlDataReader_handle to check out the corresponding form
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new DataSet_handle());
        }


Prortion of the wrapper class, concerning DataSets, can be seen below

    public static DataSet getDataSet(string commandText, CommandType commandType, SqlConnection connection, int isCaseSensitive,  string dataSetName, int enforceConstraints, params SqlParameter[] sqlParameters)
    {
        DataSet ds = null;
        SqlDataAdapter ad = null;

        try
        {
            ad = new SqlDataAdapter();
            ad.SelectCommand = getSqlCommand(commandText,commandType, sqlParameters);
            if (connection != null)
            {
                ad.SelectCommand.Connection = connection;
            }
            else
            {
                ad.SelectCommand.Connection = getConnection();
            }

            ds = new DataSet();
           
            if (isCaseSensitive != -1)
            {
                ds.CaseSensitive = Convert.ToBoolean(isCaseSensitive);
            }
            if (!dataSetName.Equals(string.Empty))
            {
                ds.DataSetName = dataSetName;
            }
            if (enforceConstraints != -1)
            {
                ds.EnforceConstraints = Convert.ToBoolean(enforceConstraints);
            }

            ad.Fill(ds);
        }
        catch(Exception ex)
        {
            throw ex;
        }
        finally
        {
            ad.Dispose();
        }
        return ds;
    }


    public static DataSet getDataSet(string commandText, params SqlParameter[] sqlParameters)
    {
        return getDataSet(commandText, CommandType.StoredProcedure, null, -1, string.Empty, -1, sqlParameters);
    }

    public static DataSet getDataSet(SqlCommand command, CommandType commandType, SqlConnection connection, int isCaseSensitive, string dataSetName, int enforceConstraints, params SqlParameter[] sqlParameters)
    {
        DataSet ds = null;
        SqlDataAdapter ad = null;

        try
        {
            ad = new SqlDataAdapter(command);
            ad.SelectCommand.CommandType = commandType;
            if (connection != null)
            {
                ad.SelectCommand.Connection = connection;
            }
            else
            {
                ad.SelectCommand.Connection = getConnection();
            }

            foreach (SqlParameter _param in sqlParameters)
            {
                ad.SelectCommand.Parameters.Add(_param);
            }

            ds = new DataSet();
            if (isCaseSensitive != -1)
            {
                ds.CaseSensitive = Convert.ToBoolean(isCaseSensitive);
            }
            if (dataSetName != string.Empty)
            {
                ds.DataSetName = dataSetName;
            }
            if (enforceConstraints != -1)
            {
                ds.EnforceConstraints = Convert.ToBoolean(enforceConstraints);
            }

            ad.Fill(ds);
        }
        //The value parameter is null
        catch (ArgumentNullException ex)
        {
            throw ex;
        }
        //The SqlParameter specified in the value parameter is already added to this or another SqlParameterCollection
        catch (ArgumentException ex)
        {
            throw ex;
        }
        //The parameter passed was not a SqlParameter
        catch (InvalidCastException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            ad.Dispose();
        }

        return ds;
    }

    public static DataSet getDataSet(SqlCommand command, params SqlParameter[] sqlParameters)
    {
        return getDataSet(command, CommandType.StoredProcedure, null, -1, string.Empty, -1, sqlParameters);
    }