SqlParameter DbType.Date & # 8594; SqlDbType.DateTime Conversion

I use the generic IDbCommand functions (in non-SqlCommand helper methods) to set SqlCommand.DbType to DbType.Date, like this:

        var param = command.CreateParameter();

        param.DbType = DbType.Date;
        param.ParameterName = field;
        param.Value = ToDb(val);

        command.Parameters.Add(param);

And the resulting param.DbType is redefined before DbType.DateTime. (I intentionally want SqlDbType.Date, because the column / index has the Sql Server Type is Date, not DateTime.) Of course, when I decompile, I see that SqlParameter.DbType defines the calls to MetaType.GetMetaTypeFromDbType that has this:

internal static MetaType GetMetaTypeFromDbType(DbType target)
{
  switch (target)
  {
    ... 

    case DbType.Date:
    case DbType.DateTime:
      return MetaType.MetaDateTime;
  }
}

So the forced conversion is intentional, and instead I have to do something hacked, like this:

        var param = command.CreateParameter();

        var sqlParam = param as SqlParameter;
        if (sqlParam != null)
        {
            sqlParam.SqlDbType = SqlDbType.Date;
        }
        else
        {
            param.DbType = DbType.Date;
        }

        param.ParameterName = field;
        param.Value = ToDb(val);

        command.Parameters.Add(param);

- ? Sql Server/framework, , , , , DbType.Date DbType.DateTime? - ?

SqlParameter?

( , , , - , ? !)

+4
1
0

Source: https://habr.com/ru/post/1666024/


All Articles