ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ForDatabase

For use with old school ado.net database command parameters. This basically converts the string to System.DBNull.Value if the string is null else it returns the string.

Source

public static class StringExtensions
{
    public static object ForDatabase(this string str)
    {
        if (str == null)
        {
            return System.DBNull.Value;
        }

        return str;
    }
}

Example

SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.Add(new SqlParameter("@Email",email.ForDatabase());

Author: Tomas Tomasson

Submitted on: 17 jan. 2011

Language: C#

Type: System.String

Views: 4478