System.Data.SqlClient.MetaType.GetSqlValueFromComVariant C# (CSharp) Method

GetSqlValueFromComVariant() static private method

static private GetSqlValueFromComVariant ( object comVal ) : object
comVal object
return object
        internal static object GetSqlValueFromComVariant(object comVal)
        {
            object sqlVal = null;
            if ((null != comVal) && (DBNull.Value != comVal))
            {
                if (comVal is float)
                    sqlVal = new SqlSingle((float)comVal);
                else if (comVal is string)
                    sqlVal = new SqlString((string)comVal);
                else if (comVal is double)
                    sqlVal = new SqlDouble((double)comVal);
                else if (comVal is System.Byte[])
                    sqlVal = new SqlBinary((byte[])comVal);
                else if (comVal is System.Char)
                    sqlVal = new SqlString(((char)comVal).ToString());
                else if (comVal is System.Char[])
                    sqlVal = new SqlChars((System.Char[])comVal);
                else if (comVal is System.Guid)
                    sqlVal = new SqlGuid((Guid)comVal);
                else if (comVal is bool)
                    sqlVal = new SqlBoolean((bool)comVal);
                else if (comVal is byte)
                    sqlVal = new SqlByte((byte)comVal);
                else if (comVal is Int16)
                    sqlVal = new SqlInt16((Int16)comVal);
                else if (comVal is Int32)
                    sqlVal = new SqlInt32((Int32)comVal);
                else if (comVal is Int64)
                    sqlVal = new SqlInt64((Int64)comVal);
                else if (comVal is Decimal)
                    sqlVal = new SqlDecimal((Decimal)comVal);
                else if (comVal is DateTime)
                {
                    // devnote: Do not use with SqlDbType.Date and SqlDbType.DateTime2. See comment at top of method.
                    sqlVal = new SqlDateTime((DateTime)comVal);
                }
                else if (comVal is XmlReader)
                    sqlVal = new SqlXml((XmlReader)comVal);
                else if (comVal is TimeSpan || comVal is DateTimeOffset)
                    sqlVal = comVal;
#if DEBUG
                else
                    Debug.Assert(false, "unknown SqlType class stored in sqlVal");
#endif
            }
            return sqlVal;
        }

Usage Example

示例#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);
        }