lundi 20 avril 2015

Can I Make this methods into a generic method?

i have two methods to load nullable parameters into a SqlCommand;

    public static void LoadParameter<T>(Nullable<T> parameter, string name, SqlCommand cmd) where T : struct
    {
        if (parameter.HasValue)
            cmd.Parameters.AddWithValue(name, parameter);
        else
            cmd.Parameters.AddWithValue(name, DBNull.Value);
    }

and

    public static void LoadParameter(string parameter, string name, SqlCommand cmd)
    {
        if (!string.IsNullOrEmpty(parameter))
            cmd.Parameters.AddWithValue(name, parameter);
        else
            cmd.Parameters.AddWithValue(name, DBNull.Value);
    }

My questions are the next: Can i make only one method to get the same result? Can i add the "no Nullables" parameters to this (new) method and still working? I.E. Int16, Int32, Int64, etc.

Thanks!

Aucun commentaire:

Enregistrer un commentaire