GetSqlValueFromComVariant() static private method

static private GetSqlValueFromComVariant ( object comVal ) : object
comVal object
return object
Exemplo n.º 1
0
        internal static object GetSqlValue(_SqlMetaData metaData, object comVal)
        {
            SqlDbType type   = metaData.type;
            object    sqlVal = null;
            bool      isNull = (comVal == null) || (Convert.IsDBNull(comVal));

            switch (type)
            {
            case SqlDbType.BigInt:
                sqlVal = isNull ? SqlInt64.Null : new SqlInt64((Int64)comVal);
                break;

            case SqlDbType.Binary:
            case SqlDbType.Timestamp:
            case SqlDbType.Image:
            case SqlDbType.VarBinary:
            // HACK!!!  We have an internal type for smallvarbinarys stored on TdsEnums.  We
            // store on TdsEnums instead of SqlDbType because we do not want to expose
            // this type to the user!
            case TdsEnums.SmallVarBinary:
                sqlVal = isNull ? SqlBinary.Null : new SqlBinary((byte[])comVal);
                break;

            case SqlDbType.Bit:
                sqlVal = isNull ? SqlBoolean.Null : new SqlBoolean((bool)comVal);
                break;

            case SqlDbType.Char:
            case SqlDbType.VarChar:
            case SqlDbType.Text:
            case SqlDbType.NChar:
            case SqlDbType.NVarChar:
            case SqlDbType.NText:
                if (isNull)
                {
                    sqlVal = SqlString.Null;
                }
                else
                {
                    if (null != metaData.collation)
                    {
                        int lcid = TdsParser.Getlcid(metaData.collation);
                        SqlCompareOptions options = TdsParser.GetSqlCompareOptions(metaData.collation);
                        sqlVal = new SqlString((string)comVal, lcid, options);
                    }
                    else
                    {
                        sqlVal = new SqlString((string)comVal);
                    }
                }
                break;

            case SqlDbType.DateTime:
            case SqlDbType.SmallDateTime:
                sqlVal = isNull ? SqlDateTime.Null : new SqlDateTime((DateTime)comVal);
                break;

            case SqlDbType.Money:
            case SqlDbType.SmallMoney:
                sqlVal = isNull ? SqlMoney.Null : new SqlMoney((Decimal)comVal);
                break;

            case SqlDbType.Real:
                sqlVal = isNull ? SqlSingle.Null : new SqlSingle((float)comVal);
                break;

            case SqlDbType.Float:
                sqlVal = isNull ? SqlDouble.Null : new SqlDouble((double)comVal);
                break;

            case SqlDbType.Decimal:
                sqlVal = isNull ? SqlDecimal.Null : new SqlDecimal((decimal)comVal);
                break;

            case SqlDbType.Int:
                sqlVal = isNull ? SqlInt32.Null : new SqlInt32((int)comVal);
                break;

            case SqlDbType.SmallInt:
                sqlVal = isNull ? SqlInt16.Null : new SqlInt16((Int16)comVal);
                break;

            case SqlDbType.TinyInt:
                sqlVal = isNull ? SqlByte.Null : new SqlByte((byte)comVal);
                break;

            case SqlDbType.UniqueIdentifier:
                sqlVal = isNull ? SqlGuid.Null : new SqlGuid((Guid)comVal);
                break;

            case SqlDbType.Variant:
                sqlVal = isNull ? DBNull.Value : MetaType.GetSqlValueFromComVariant(comVal);
                break;

            default:
                Debug.Assert(false, "unknown SqlDbType!  Can't create SQL type");
                break;
            }

            return(sqlVal);
        }