Best method of assigning NULL value to SqlParameter

前端 未结 7 1321
野的像风
野的像风 2021-01-07 20:53

I have a number of optional input parameters I am using in a C# class method. Since the optional syntax creates a value of \'0\' when the parameter is not used, the SQL inse

相关标签:
7条回答
  • 2021-01-07 21:11

    with small helper class we can create extenction method to add DBNull.Value to sqlparameter

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.SqlClient;
    using System.Data;
    using System.Data.Common;
    
    namespace System.Data.SqlClient
    {
        public static class ExtensionMethods 
    
        {
    
    
    
            public static SqlParameter AddParameter(this SqlParameterCollection parms, SqlParameter param)
            {
                if (param.Value == null)
                {
                    param.Value = DBNull.Value;
                    return parms.Add(param);
                }
                else
                {
                    return parms.Add(param);        
                }
    
            }
    }
    

    usage

    SqlParameter _FraudPackageID = new SqlParameter("@FraudPackageID", System.Data.SqlDbType.BigInt);
     _FraudPackageID.Value = FraudPackageID;
     _FraudPackageID.Direction = System.Data.ParameterDirection.Input;
    
    _cmd.Parameters.AddParameter(_FraudPackageID);
    

    if you assign or pass null as value to sqlparameter this extenction method will automatically converts it to DBNull and reassign to the sqlparameter.

    so no need to worry about what the values we are passing dynamically.

    0 讨论(0)
  • 2021-01-07 21:12

    Consider using the Nullable(T) structure available. It'll let you only set values if you have them, and your SQL Command objects will recognize the nullable value and process accordingly with no hassle on your end.

    0 讨论(0)
  • 2021-01-07 21:15

    You'd need to check each parameter value and use DBNull.Value send null. We have an extension method off object to make this a little easier.

    public static class ObjectExtensions
    {
        public static object OptionalParam<T>(this T value, T optionalValue = default(T))
        {
            return Equals(value,optionalValue) ? (object)DBNull.Value : value;
        }
    }
    

    Usage:

    var sqlInput = new SqlParameter("@itemsRead", input.OptionalParam());
    

    Or if you want to consider a non-default, arbitrary value as the default value:

    var sqlInput = new SqlParameter("@itemsRead", input.OptionalParam(-1));
    
    0 讨论(0)
  • 2021-01-07 21:15

    Another clear way to set a null datetime parameter when necessary

    if(obj.myDate == DateTime.MinValue)
    {
        aCommand.Parameters.Add("dateParameter", SqlDbType.Date).Value = DBNull.Value;
    }
    else
    {
        aCommand.Parameters.Add("dateParameter", SqlDbType.Date).Value = obj.myDate ;
    }
    
    0 讨论(0)
  • 2021-01-07 21:20

    Yes, for the value of the parameter, just use DBNull.Value. For example:

    SqlParameter sqlError = 
        new SqlParameter("@errors", errors == 0 ? (object)DBNull.Value : errors);
    

    Or write a little helper:

    private object ValueOrDBNullIfZero(int val) {
       if ( val == 0 ) return DBNull.Value;
       return val;
    }
    

    Then:

    SqlParameter sqlError = 
        new SqlParameter("@errors", ValueOrDBNullIfZero(errors));
    
    0 讨论(0)
  • 2021-01-07 21:22

    This is the easiest way to assign a Null value to sql parameter

                command.Parameters.AddWithValue("@Comment", string.IsNullOrEmpty(comment) ? (object)DBNull.Value : comment);   
    
    0 讨论(0)
提交回复
热议问题