AddWithValue Extension Method

Posted by Blake on 8/13/2012
)

Every once in a while you may run into an ADO.Net data provider that doesn’t support the AddWithValue method on the IDbCommand. If you’re in love with this command and would prefer to have it, then you can implement an extension method to recreate what it does. I’m not sure if the new MySql provider supports AddWithValue, but the older versions didn’t so I used something to this effect myself in some of my hobby projects. I’m going to provide my VB and then a C# conversion (quickly retrieved via Telerik’s converter):

VB.Net

        ''' <summary>
        ''' Adds a parameter into the IDbCommand and sets it's name, value and type.
        ''' </summary>
        ''' <param name="cmd"></param>
        ''' <param name="paramName"></param>
        ''' <param name="paramValue"></param>
        ''' <param name="dbType"></param>
        ''' <remarks></remarks>
        <Extension()> _
        Public Sub AddWithValue(ByVal cmd As IDbCommand, ByVal paramName As String, ByVal paramValue As Object, ByVal dbType As DbType)
            Dim param As DbParameter = cmd.CreateParameter
            param.ParameterName = paramName
            param.DbType = dbType
            param.Value = paramValue
            cmd.Parameters.Add(param)
        End Sub

        ''' <summary>
        ''' Adds a parameter into the IDbCommand and name and value.
        ''' </summary>
        ''' <param name="cmd"></param>
        ''' <param name="paramName"></param>
        ''' <param name="paramValue"></param>
        ''' <remarks></remarks>
        <Extension()> _
        Public Sub AddWithValue(ByVal cmd As IDbCommand, ByVal paramName As String, ByVal paramValue As Object)
            Dim param As DbParameter = cmd.CreateParameter
            param.ParameterName = paramName
            param.Value = paramValue
            cmd.Parameters.Add(param)
        End Sub

        ''' <summary>
        ''' Adds a parameter into the IDbCommand and sets it's name, value and type.
        ''' </summary>
        ''' <param name="cmd"></param>
        ''' <param name="paramName"></param>
        ''' <param name="paramValue"></param>
        ''' <param name="dbType"></param>
        ''' <remarks></remarks>
        <Extension()> _
        Public Sub AddWithValue(ByVal cmd As IDbCommand, ByVal paramName As String, ByVal paramValue As Object, ByVal dbType As DbType, ByVal size As Integer)
            Dim param As DbParameter = cmd.CreateParameter
            param.ParameterName = paramName
            param.DbType = dbType
            param.Value = paramValue
            param.Size = size
            cmd.Parameters.Add(param)
        End Sub

C#

    /// <summary>
    /// Adds a parameter into the IDbCommand and sets it's name, value and type.
    /// </summary>
    /// <param name="cmd"></param>
    /// <param name="paramName"></param>
    /// <param name="paramValue"></param>
    /// <param name="dbType"></param>
    /// <remarks></remarks>
    [Extension()]
    public void AddWithValue(IDbCommand cmd, string paramName, object paramValue, DbType dbType)
    {
        DbParameter param = cmd.CreateParameter;
        param.ParameterName = paramName;
        param.DbType = dbType;
        param.Value = paramValue;
        cmd.Parameters.Add(param);
    }

    /// <summary>
    /// Adds a parameter into the IDbCommand and name and value.
    /// </summary>
    /// <param name="cmd"></param>
    /// <param name="paramName"></param>
    /// <param name="paramValue"></param>
    /// <remarks></remarks>
    [Extension()]
    public void AddWithValue(IDbCommand cmd, string paramName, object paramValue)
    {
        DbParameter param = cmd.CreateParameter;
        param.ParameterName = paramName;
        param.Value = paramValue;
        cmd.Parameters.Add(param);
    }

    /// <summary>
    /// Adds a parameter into the IDbCommand and sets it's name, value and type.
    /// </summary>
    /// <param name="cmd"></param>
    /// <param name="paramName"></param>
    /// <param name="paramValue"></param>
    /// <param name="dbType"></param>
    /// <remarks></remarks>
    [Extension()]
    public void AddWithValue(IDbCommand cmd, string paramName, object paramValue, DbType dbType, int size)
    {
        DbParameter param = cmd.CreateParameter;
        param.ParameterName = paramName;
        param.DbType = dbType;
        param.Value = paramValue;
        param.Size = size;
        cmd.Parameters.Add(param);
    }