public virtual int SetValues(params object[] values) { this.EnsureSubclassOverride(); if (values == null) { throw ADP.ArgumentNull("values"); } int num3 = (values.Length > this.FieldCount) ? this.FieldCount : values.Length; ExtendedClrTypeCode[] codeArray = new ExtendedClrTypeCode[num3]; for (int i = 0; i < num3; i++) { SqlMetaData sqlMetaData = this.GetSqlMetaData(i); codeArray[i] = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(sqlMetaData.SqlDbType, false, values[i], sqlMetaData.Type, this.SmiVersion); if (ExtendedClrTypeCode.Invalid == codeArray[i]) { throw ADP.InvalidCast(); } } for (int j = 0; j < num3; j++) { if (this.SmiVersion >= 210L) { ValueUtilsSmi.SetCompatibleValueV200(this._eventSink, this._recordBuffer, j, this.GetSmiMetaData(j), values[j], codeArray[j], 0, 0, null); } else { ValueUtilsSmi.SetCompatibleValue(this._eventSink, this._recordBuffer, j, this.GetSmiMetaData(j), values[j], codeArray[j], 0); } } return(num3); }
// // ISqlUpdateableRecord Implementation // public virtual int SetValues(params object[] values) { EnsureSubclassOverride(); if (null == values) { throw ADP.ArgumentNull(nameof(values)); } // Allow values array longer than FieldCount, just ignore the extra cells. int copyLength = (values.Length > FieldCount) ? FieldCount : values.Length; ExtendedClrTypeCode[] typeCodes = new ExtendedClrTypeCode[copyLength]; // Verify all data values as acceptable before changing current state. for (int i = 0; i < copyLength; i++) { SqlMetaData metaData = GetSqlMetaData(i); typeCodes[i] = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType( metaData.SqlDbType, false /* isMultiValued */, values[i], metaData.Type); if (ExtendedClrTypeCode.Invalid == typeCodes[i]) { throw ADP.InvalidCast(); } } // Now move the data (it'll only throw if someone plays with the values array between // the validation loop and here, or if an invalid UDT was sent). for (int i = 0; i < copyLength; i++) { ValueUtilsSmi.SetCompatibleValueV200(_eventSink, _recordBuffer, i, GetSmiMetaData(i), values[i], typeCodes[i], 0, 0, null); } return(copyLength); }
/// <include file='../../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/SetValue/*' /> public virtual void SetValue(int ordinal, object value) { EnsureSubclassOverride(); SqlMetaData metaData = GetSqlMetaData(ordinal); ExtendedClrTypeCode typeCode = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType( metaData.SqlDbType, isMultiValued: false, value, metaData.Type, SmiVersion ); if (typeCode == ExtendedClrTypeCode.Invalid) { throw ADP.InvalidCast(); } if (SmiVersion >= SmiContextFactory.KatmaiVersion) { ValueUtilsSmi.SetCompatibleValueV200(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, typeCode, offset: 0, length: 0, peekAhead: null); } else { ValueUtilsSmi.SetCompatibleValue(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, typeCode, offset: 0); } }
private void SetValueFrameworkSpecific(int ordinal, object value) { SqlMetaData metaData = GetSqlMetaData(ordinal); ExtendedClrTypeCode typeCode = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType( metaData.SqlDbType, isMultiValued: false, value, metaData.Type, SmiVersion ); if (typeCode == ExtendedClrTypeCode.Invalid) { throw ADP.InvalidCast(); } if (SmiVersion >= SmiContextFactory.Sql2008Version) { ValueUtilsSmi.SetCompatibleValueV200(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, typeCode, offset: 0, peekAhead: null); } else { ValueUtilsSmi.SetCompatibleValue(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, typeCode, offset: 0); } }
internal static SqlDbType InferSqlDbTypeFromType(Type type) { ExtendedClrTypeCode typeCode = DetermineExtendedTypeCodeFromType(type); if (ExtendedClrTypeCode.Invalid == typeCode) { return(~SqlDbType.BigInt); } return(InferSqlDbTypeFromTypeCode(typeCode)); }
public virtual void SetValue(int ordinal, object value) { EnsureSubclassOverride(); SqlMetaData metaData = GetSqlMetaData(ordinal); ExtendedClrTypeCode typeCode = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType( metaData.SqlDbType, false /* isMultiValued */, value, metaData.Type); if (ExtendedClrTypeCode.Invalid == typeCode) { throw ADP.InvalidCast(); } ValueUtilsSmi.SetCompatibleValueV200(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, typeCode, 0, 0, null); }
// Infer SqlDbType from Type in the general case. Katmai-only (or later) features that need to // infer types should use InferSqlDbTypeFromType_Katmai. internal static SqlDbType InferSqlDbTypeFromType(Type type) { ExtendedClrTypeCode typeCode = DetermineExtendedTypeCodeFromType(type); SqlDbType returnType; if (ExtendedClrTypeCode.Invalid == typeCode) { returnType = InvalidSqlDbType; // Return invalid type so caller can generate specific error } else { returnType = InferSqlDbTypeFromTypeCode(typeCode); } return(returnType); }
/// <include file='../../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/SetValue/*' /> public virtual void SetValue(int ordinal, object value) { SqlMetaData metaData = GetSqlMetaData(ordinal); ExtendedClrTypeCode typeCode = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType( metaData.SqlDbType, isMultiValued: false, value, metaData.Type ); if (typeCode == ExtendedClrTypeCode.Invalid) { throw ADP.InvalidCast(); } ValueUtilsSmi.SetCompatibleValueV200(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, typeCode, offset: 0, length: 0, peekAhead: null); }
private int SetValuesFrameworkSpecific(params object[] values) { if (values == null) { throw ADP.ArgumentNull(nameof(values)); } // Allow values array longer than FieldCount, just ignore the extra cells. int copyLength = (values.Length > FieldCount) ? FieldCount : values.Length; ExtendedClrTypeCode[] typeCodes = new ExtendedClrTypeCode[copyLength]; // Verify all data values as acceptable before changing current state. for (int i = 0; i < copyLength; i++) { SqlMetaData metaData = GetSqlMetaData(i); typeCodes[i] = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType( metaData.SqlDbType, isMultiValued: false, values[i], metaData.Type, SmiVersion ); if (typeCodes[i] == ExtendedClrTypeCode.Invalid) { throw ADP.InvalidCast(); } } // Now move the data (it'll only throw if someone plays with the values array between // the validation loop and here, or if an invalid UDT was sent). for (int i = 0; i < copyLength; i++) { if (SmiVersion >= SmiContextFactory.Sql2008Version) { ValueUtilsSmi.SetCompatibleValueV200(_eventSink, _recordBuffer, i, GetSmiMetaData(i), values[i], typeCodes[i], offset: 0, peekAhead: null); } else { ValueUtilsSmi.SetCompatibleValue(_eventSink, _recordBuffer, i, GetSmiMetaData(i), values[i], typeCodes[i], offset: 0); } } return(copyLength); }
public virtual void SetValue(int ordinal, object value) { this.EnsureSubclassOverride(); SqlMetaData sqlMetaData = this.GetSqlMetaData(ordinal); ExtendedClrTypeCode typeCode = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(sqlMetaData.SqlDbType, false, value, sqlMetaData.Type, this.SmiVersion); if (ExtendedClrTypeCode.Invalid == typeCode) { throw ADP.InvalidCast(); } if (this.SmiVersion >= 210L) { ValueUtilsSmi.SetCompatibleValueV200(this._eventSink, this._recordBuffer, ordinal, this.GetSmiMetaData(ordinal), value, typeCode, 0, 0, null); } else { ValueUtilsSmi.SetCompatibleValue(this._eventSink, this._recordBuffer, ordinal, this.GetSmiMetaData(ordinal), value, typeCode, 0); } }
// // ISqlUpdateableRecord Implementation // public virtual int SetValues( params object[] values ) { EnsureSubclassOverride(); if (null == values) { throw ADP.ArgumentNull( "values" ); } // SQLBUDT #346883 Allow values array longer than FieldCount, just ignore the extra cells. int copyLength = ( values.Length > FieldCount ) ? FieldCount : values.Length; ExtendedClrTypeCode[] typeCodes = new ExtendedClrTypeCode [copyLength]; // Verify all data values as acceptable before changing current state. for (int i=0; i < copyLength; i++) { SqlMetaData metaData = GetSqlMetaData(i); typeCodes[i] = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType( metaData.SqlDbType, false /* isMultiValued */, values[i], metaData.Type, SmiVersion); if ( ExtendedClrTypeCode.Invalid == typeCodes[i] ) { throw ADP.InvalidCast(); } } // Now move the data (it'll only throw if someone plays with the values array between // the validation loop and here, or if an invalid UDT was sent). for( int i=0; i < copyLength; i++ ) { if (SmiVersion >= SmiContextFactory.KatmaiVersion) { ValueUtilsSmi.SetCompatibleValueV200(_eventSink, _recordBuffer, i, GetSmiMetaData(i), values[i], typeCodes[i], 0, 0, null); } else { ValueUtilsSmi.SetCompatibleValue(_eventSink, _recordBuffer, i, GetSmiMetaData(i), values[i], typeCodes[i], 0); } } return copyLength; }
// returns a sqldbtype for the given type code internal static SqlDbType InferSqlDbTypeFromTypeCode(ExtendedClrTypeCode typeCode) { Debug.Assert(typeCode >= ExtendedClrTypeCode.Invalid && typeCode <= ExtendedClrTypeCode.Last, "Someone added a typecode without adding support here!"); return(s_extendedTypeCodeToSqlDbTypeMap[(int)typeCode + 1]); }
// If we know we're only going to use this object to assign to a specific SqlDbType back end object, // we can save some processing time by only checking for the few valid types that can be assigned to the dbType. // This assumes a switch statement over SqlDbType is faster than getting the ClrTypeCode and iterating over a // series of if statements, or using a hash table. // NOTE: the form of these checks is taking advantage of a feature of the JIT compiler that is supposed to // optimize checks of the form '(xxx.GetType() == typeof( YYY ))'. The JIT team claimed at one point that // this doesn't even instantiate a Type instance, thus was the fastest method for individual comparisons. // Given that there's a known SqlDbType, thus a minimal number of comparisons, it's likely this is faster // than the other approaches considered (both GetType().GetTypeCode() switch and hash table using Type keys // must instantiate a Type object. The typecode switch also degenerates into a large if-then-else for // all but the primitive clr types. internal static ExtendedClrTypeCode DetermineExtendedTypeCodeForUseWithSqlDbType( SqlDbType dbType, bool isMultiValued, object value ) { ExtendedClrTypeCode extendedCode = ExtendedClrTypeCode.Invalid; // fast-track null, which is valid for all types if (null == value) { extendedCode = ExtendedClrTypeCode.Empty; } else if (DBNull.Value == value) { extendedCode = ExtendedClrTypeCode.DBNull; } else { switch (dbType) { case SqlDbType.BigInt: if (value.GetType() == typeof(Int64)) { extendedCode = ExtendedClrTypeCode.Int64; } else if (value.GetType() == typeof(SqlInt64)) { extendedCode = ExtendedClrTypeCode.SqlInt64; } break; case SqlDbType.Binary: case SqlDbType.VarBinary: case SqlDbType.Image: case SqlDbType.Timestamp: if (value.GetType() == typeof(byte[])) { extendedCode = ExtendedClrTypeCode.ByteArray; } else if (value.GetType() == typeof(SqlBinary)) { extendedCode = ExtendedClrTypeCode.SqlBinary; } else if (value.GetType() == typeof(SqlBytes)) { extendedCode = ExtendedClrTypeCode.SqlBytes; } else if (value.GetType() == typeof(StreamDataFeed)) { extendedCode = ExtendedClrTypeCode.Stream; } break; case SqlDbType.Bit: if (value.GetType() == typeof(bool)) { extendedCode = ExtendedClrTypeCode.Boolean; } else if (value.GetType() == typeof(SqlBoolean)) { extendedCode = ExtendedClrTypeCode.SqlBoolean; } break; case SqlDbType.Char: case SqlDbType.NChar: case SqlDbType.NText: case SqlDbType.NVarChar: case SqlDbType.Text: case SqlDbType.VarChar: if (value.GetType() == typeof(string)) { extendedCode = ExtendedClrTypeCode.String; } if (value.GetType() == typeof(TextDataFeed)) { extendedCode = ExtendedClrTypeCode.TextReader; } else if (value.GetType() == typeof(SqlString)) { extendedCode = ExtendedClrTypeCode.SqlString; } else if (value.GetType() == typeof(char[])) { extendedCode = ExtendedClrTypeCode.CharArray; } else if (value.GetType() == typeof(SqlChars)) { extendedCode = ExtendedClrTypeCode.SqlChars; } else if (value.GetType() == typeof(char)) { extendedCode = ExtendedClrTypeCode.Char; } break; case SqlDbType.Date: case SqlDbType.DateTime2: case SqlDbType.DateTime: case SqlDbType.SmallDateTime: if (value.GetType() == typeof(DateTime)) { extendedCode = ExtendedClrTypeCode.DateTime; } else if (value.GetType() == typeof(SqlDateTime)) { extendedCode = ExtendedClrTypeCode.SqlDateTime; } break; case SqlDbType.Decimal: if (value.GetType() == typeof(Decimal)) { extendedCode = ExtendedClrTypeCode.Decimal; } else if (value.GetType() == typeof(SqlDecimal)) { extendedCode = ExtendedClrTypeCode.SqlDecimal; } break; case SqlDbType.Real: if (value.GetType() == typeof(Single)) { extendedCode = ExtendedClrTypeCode.Single; } else if (value.GetType() == typeof(SqlSingle)) { extendedCode = ExtendedClrTypeCode.SqlSingle; } break; case SqlDbType.Int: if (value.GetType() == typeof(Int32)) { extendedCode = ExtendedClrTypeCode.Int32; } else if (value.GetType() == typeof(SqlInt32)) { extendedCode = ExtendedClrTypeCode.SqlInt32; } break; case SqlDbType.Money: case SqlDbType.SmallMoney: if (value.GetType() == typeof(SqlMoney)) { extendedCode = ExtendedClrTypeCode.SqlMoney; } else if (value.GetType() == typeof(Decimal)) { extendedCode = ExtendedClrTypeCode.Decimal; } break; case SqlDbType.Float: if (value.GetType() == typeof(SqlDouble)) { extendedCode = ExtendedClrTypeCode.SqlDouble; } else if (value.GetType() == typeof(Double)) { extendedCode = ExtendedClrTypeCode.Double; } break; case SqlDbType.UniqueIdentifier: if (value.GetType() == typeof(SqlGuid)) { extendedCode = ExtendedClrTypeCode.SqlGuid; } else if (value.GetType() == typeof(Guid)) { extendedCode = ExtendedClrTypeCode.Guid; } break; case SqlDbType.SmallInt: if (value.GetType() == typeof(Int16)) { extendedCode = ExtendedClrTypeCode.Int16; } else if (value.GetType() == typeof(SqlInt16)) { extendedCode = ExtendedClrTypeCode.SqlInt16; } break; case SqlDbType.TinyInt: if (value.GetType() == typeof(Byte)) { extendedCode = ExtendedClrTypeCode.Byte; } else if (value.GetType() == typeof(SqlByte)) { extendedCode = ExtendedClrTypeCode.SqlByte; } break; case SqlDbType.Variant: // SqlDbType doesn't help us here, call general-purpose function extendedCode = DetermineExtendedTypeCode(value); // Some types aren't allowed for Variants but are for the general-purpose function. // Match behavior of other types and return invalid in these cases. if (ExtendedClrTypeCode.SqlXml == extendedCode) { extendedCode = ExtendedClrTypeCode.Invalid; } break; case SqlDbType.Udt: throw ADP.DbTypeNotSupported(SqlDbType.Udt.ToString()); case SqlDbType.Time: if (value.GetType() == typeof(TimeSpan)) { extendedCode = ExtendedClrTypeCode.TimeSpan; } break; case SqlDbType.DateTimeOffset: if (value.GetType() == typeof(DateTimeOffset)) { extendedCode = ExtendedClrTypeCode.DateTimeOffset; } break; case SqlDbType.Xml: if (value.GetType() == typeof(SqlXml)) { extendedCode = ExtendedClrTypeCode.SqlXml; } if (value.GetType() == typeof(XmlDataFeed)) { extendedCode = ExtendedClrTypeCode.XmlReader; } else if (value.GetType() == typeof(System.String)) { extendedCode = ExtendedClrTypeCode.String; } break; case SqlDbType.Structured: if (isMultiValued) { if (value is DataTable) { extendedCode = ExtendedClrTypeCode.DataTable; } else if (value is IEnumerable <SqlDataRecord> ) { extendedCode = ExtendedClrTypeCode.IEnumerableOfSqlDataRecord; } else if (value is DbDataReader) { extendedCode = ExtendedClrTypeCode.DbDataReader; } } break; default: // Leave as invalid break; } } return(extendedCode); }
internal static ExtendedClrTypeCode DetermineExtendedTypeCodeForUseWithSqlDbType(SqlDbType dbType, bool isMultiValued, object value, Type udtType, ulong smiVersion) { ExtendedClrTypeCode invalid = ExtendedClrTypeCode.Invalid; if (value == null) { return(ExtendedClrTypeCode.Empty); } if (DBNull.Value == value) { return(ExtendedClrTypeCode.DBNull); } switch (dbType) { case SqlDbType.BigInt: if (!(value.GetType() == typeof(long))) { if (value.GetType() == typeof(SqlInt64)) { return(ExtendedClrTypeCode.SqlInt64); } if (Type.GetTypeCode(value.GetType()) == TypeCode.Int64) { invalid = ExtendedClrTypeCode.Int64; } return(invalid); } return(ExtendedClrTypeCode.Int64); case SqlDbType.Binary: case SqlDbType.Image: case SqlDbType.Timestamp: case SqlDbType.VarBinary: if (!(value.GetType() == typeof(byte[]))) { if (value.GetType() == typeof(SqlBinary)) { return(ExtendedClrTypeCode.SqlBinary); } if (value.GetType() == typeof(SqlBytes)) { invalid = ExtendedClrTypeCode.SqlBytes; } return(invalid); } return(ExtendedClrTypeCode.ByteArray); case SqlDbType.Bit: if (!(value.GetType() == typeof(bool))) { if (value.GetType() == typeof(SqlBoolean)) { return(ExtendedClrTypeCode.SqlBoolean); } if (Type.GetTypeCode(value.GetType()) == TypeCode.Boolean) { invalid = ExtendedClrTypeCode.Boolean; } return(invalid); } return(ExtendedClrTypeCode.Boolean); case SqlDbType.Char: case SqlDbType.NChar: case SqlDbType.NText: case SqlDbType.NVarChar: case SqlDbType.Text: case SqlDbType.VarChar: if (!(value.GetType() == typeof(string))) { if (value.GetType() == typeof(SqlString)) { return(ExtendedClrTypeCode.SqlString); } if (value.GetType() == typeof(char[])) { return(ExtendedClrTypeCode.CharArray); } if (value.GetType() == typeof(SqlChars)) { return(ExtendedClrTypeCode.SqlChars); } if (value.GetType() == typeof(char)) { return(ExtendedClrTypeCode.Char); } if (Type.GetTypeCode(value.GetType()) == TypeCode.Char) { return(ExtendedClrTypeCode.Char); } if (Type.GetTypeCode(value.GetType()) == TypeCode.String) { invalid = ExtendedClrTypeCode.String; } return(invalid); } return(ExtendedClrTypeCode.String); case SqlDbType.DateTime: case SqlDbType.SmallDateTime: break; case SqlDbType.Decimal: if (!(value.GetType() == typeof(decimal))) { if (value.GetType() == typeof(SqlDecimal)) { return(ExtendedClrTypeCode.SqlDecimal); } if (Type.GetTypeCode(value.GetType()) == TypeCode.Decimal) { invalid = ExtendedClrTypeCode.Decimal; } return(invalid); } return(ExtendedClrTypeCode.Decimal); case SqlDbType.Float: if (!(value.GetType() == typeof(SqlDouble))) { if (value.GetType() == typeof(double)) { return(ExtendedClrTypeCode.Double); } if (Type.GetTypeCode(value.GetType()) == TypeCode.Double) { invalid = ExtendedClrTypeCode.Double; } return(invalid); } return(ExtendedClrTypeCode.SqlDouble); case SqlDbType.Int: if (!(value.GetType() == typeof(int))) { if (value.GetType() == typeof(SqlInt32)) { return(ExtendedClrTypeCode.SqlInt32); } if (Type.GetTypeCode(value.GetType()) == TypeCode.Int32) { invalid = ExtendedClrTypeCode.Int32; } return(invalid); } return(ExtendedClrTypeCode.Int32); case SqlDbType.Money: case SqlDbType.SmallMoney: if (!(value.GetType() == typeof(SqlMoney))) { if (value.GetType() == typeof(decimal)) { return(ExtendedClrTypeCode.Decimal); } if (Type.GetTypeCode(value.GetType()) == TypeCode.Decimal) { invalid = ExtendedClrTypeCode.Decimal; } return(invalid); } return(ExtendedClrTypeCode.SqlMoney); case SqlDbType.Real: if (!(value.GetType() == typeof(float))) { if (value.GetType() == typeof(SqlSingle)) { return(ExtendedClrTypeCode.SqlSingle); } if (Type.GetTypeCode(value.GetType()) == TypeCode.Single) { invalid = ExtendedClrTypeCode.Single; } return(invalid); } return(ExtendedClrTypeCode.Single); case SqlDbType.UniqueIdentifier: if (!(value.GetType() == typeof(SqlGuid))) { if (value.GetType() == typeof(Guid)) { invalid = ExtendedClrTypeCode.Guid; } return(invalid); } return(ExtendedClrTypeCode.SqlGuid); case SqlDbType.SmallInt: if (!(value.GetType() == typeof(short))) { if (value.GetType() == typeof(SqlInt16)) { return(ExtendedClrTypeCode.SqlInt16); } if (Type.GetTypeCode(value.GetType()) == TypeCode.Int16) { invalid = ExtendedClrTypeCode.Int16; } return(invalid); } return(ExtendedClrTypeCode.Int16); case SqlDbType.TinyInt: if (!(value.GetType() == typeof(byte))) { if (value.GetType() == typeof(SqlByte)) { return(ExtendedClrTypeCode.SqlByte); } if (Type.GetTypeCode(value.GetType()) == TypeCode.Byte) { invalid = ExtendedClrTypeCode.Byte; } return(invalid); } return(ExtendedClrTypeCode.Byte); case SqlDbType.Variant: invalid = DetermineExtendedTypeCode(value); if (ExtendedClrTypeCode.SqlXml == invalid) { invalid = ExtendedClrTypeCode.Invalid; } return(invalid); case (SqlDbType.SmallInt | SqlDbType.Int): case (SqlDbType.Text | SqlDbType.Int): case (SqlDbType.Xml | SqlDbType.Bit): case (SqlDbType.TinyInt | SqlDbType.Int): return(invalid); case SqlDbType.Xml: if (!(value.GetType() == typeof(SqlXml))) { if (value.GetType() == typeof(string)) { invalid = ExtendedClrTypeCode.String; } return(invalid); } return(ExtendedClrTypeCode.SqlXml); case SqlDbType.Udt: if ((null != udtType) && !(value.GetType() == udtType)) { return(ExtendedClrTypeCode.Invalid); } return(ExtendedClrTypeCode.Object); case SqlDbType.Structured: if (isMultiValued) { if (!(value is DataTable)) { if (value is IEnumerable <SqlDataRecord> ) { return(ExtendedClrTypeCode.IEnumerableOfSqlDataRecord); } if (value is DbDataReader) { invalid = ExtendedClrTypeCode.DbDataReader; } return(invalid); } invalid = ExtendedClrTypeCode.DataTable; } return(invalid); case SqlDbType.Date: case SqlDbType.DateTime2: if (smiVersion < 210L) { return(invalid); } break; case SqlDbType.Time: if ((value.GetType() == typeof(TimeSpan)) && (smiVersion >= 210L)) { invalid = ExtendedClrTypeCode.TimeSpan; } return(invalid); case SqlDbType.DateTimeOffset: if ((value.GetType() == typeof(DateTimeOffset)) && (smiVersion >= 210L)) { invalid = ExtendedClrTypeCode.DateTimeOffset; } return(invalid); default: return(invalid); } if (value.GetType() == typeof(DateTime)) { return(ExtendedClrTypeCode.DateTime); } if (value.GetType() == typeof(SqlDateTime)) { return(ExtendedClrTypeCode.SqlDateTime); } if (Type.GetTypeCode(value.GetType()) == TypeCode.DateTime) { invalid = ExtendedClrTypeCode.DateTime; } return(invalid); }
private static bool CanAccessSetterDirectly( SmiMetaData metaData, ExtendedClrTypeCode setterTypeCode ) { // Make sure no-one adds new ExtendedType, nor SqlDbType without updating this file! Debug.Assert( ExtendedClrTypeCode.First == 0 && (int)ExtendedClrTypeCode.Last == __canAccessSetterDirectly.GetLength(0) - 1, "ExtendedClrTypeCodes does not match with __canAccessSetterDirectly" ); Debug.Assert( SqlDbType.BigInt == 0 && (int)SqlDbType.DateTimeOffset == __canAccessSetterDirectly.GetLength(1) - 1, "SqlDbType does not match with __canAccessSetterDirectly" ); Debug.Assert( ExtendedClrTypeCode.First <= setterTypeCode && ExtendedClrTypeCode.Last >= setterTypeCode ); Debug.Assert( SqlDbType.BigInt <= metaData.SqlDbType && SqlDbType.DateTimeOffset >= metaData.SqlDbType ); bool returnValue = __canAccessSetterDirectly[(int)setterTypeCode, (int)metaData.SqlDbType]; // Additional restrictions to distinguish TVPs and Structured UDTs if (returnValue && (ExtendedClrTypeCode.DataTable == setterTypeCode || ExtendedClrTypeCode.DbDataReader == setterTypeCode || ExtendedClrTypeCode.IEnumerableOfSqlDataRecord == setterTypeCode)) { returnValue = metaData.IsMultiValued; } return returnValue; }
// Implements SqlClient 2.0-compatible SetValue() semantics + Orcas extensions // Assumes caller already validated basic type against the metadata, other than trimming lengths and // checking individual field values (TVPs) internal static void SetCompatibleValueV200( SmiEventSink_Default sink, SmiTypedGetterSetter setters, int ordinal, SmiMetaData metaData, object value, ExtendedClrTypeCode typeCode, int offset, int length, ParameterPeekAheadValue peekAhead ) { // Ensure caller validated compatibility for types handled directly in this method Debug.Assert( (ExtendedClrTypeCode.DataTable != typeCode && ExtendedClrTypeCode.DbDataReader != typeCode && ExtendedClrTypeCode.IEnumerableOfSqlDataRecord != typeCode) || CanAccessSetterDirectly(metaData, typeCode), "Un-validated type '" + typeCode + "' for metaData: " + metaData.SqlDbType); switch(typeCode) { case ExtendedClrTypeCode.DataTable: SetDataTable_Unchecked(sink, setters, ordinal, metaData, (DataTable)value); break; case ExtendedClrTypeCode.DbDataReader: SetDbDataReader_Unchecked(sink, setters, ordinal, metaData, (DbDataReader)value); break; case ExtendedClrTypeCode.IEnumerableOfSqlDataRecord: SetIEnumerableOfSqlDataRecord_Unchecked(sink, setters, ordinal, metaData, (IEnumerable<SqlDataRecord>)value, peekAhead); break; case ExtendedClrTypeCode.TimeSpan: SetTimeSpan_Checked(sink, setters, ordinal, metaData, (TimeSpan)value); break; case ExtendedClrTypeCode.DateTimeOffset: SetDateTimeOffset_Unchecked(sink, setters, ordinal, (DateTimeOffset)value); break; default: SetCompatibleValue(sink, setters, ordinal, metaData, value, typeCode, offset); break; } }
// Implements SqlClient 2.0-compatible SetValue() semantics // Assumes caller already validated type against the metadata, other than trimming lengths internal static void SetCompatibleValue( SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, // metadata for target setter column object value, ExtendedClrTypeCode typeCode, int offset ) { // Ensure either an invalid type, or caller validated compatibility // SqlDbType.Variant and have special handling Debug.Assert( typeCode == ExtendedClrTypeCode.Invalid || typeCode == ExtendedClrTypeCode.SByte || typeCode == ExtendedClrTypeCode.UInt16 || typeCode == ExtendedClrTypeCode.UInt32 || typeCode == ExtendedClrTypeCode.UInt64 || typeCode == ExtendedClrTypeCode.DBNull || typeCode == ExtendedClrTypeCode.Empty || CanAccessSetterDirectly( metaData, typeCode ) || value is DataFeed /* already validated */); switch ( typeCode ) { case ExtendedClrTypeCode.Invalid: throw ADP.UnknownDataType( value.GetType() ); case ExtendedClrTypeCode.Boolean: SetBoolean_Unchecked( sink, setters, ordinal, (Boolean) value ); break; case ExtendedClrTypeCode.Byte: SetByte_Unchecked( sink, setters, ordinal, (Byte) value ); break; case ExtendedClrTypeCode.Char: { char[] charsValue = new char[] {(char) value}; // recur with array type SetCompatibleValue( sink, setters, ordinal, metaData, charsValue, ExtendedClrTypeCode.CharArray, 0 ); break; } case ExtendedClrTypeCode.DateTime: SetDateTime_Checked( sink, setters, ordinal, metaData, (DateTime)value ); break; case ExtendedClrTypeCode.DBNull: SetDBNull_Unchecked( sink, setters, ordinal ); break; case ExtendedClrTypeCode.Decimal: SetDecimal_PossiblyMoney( sink, setters, ordinal, metaData, (Decimal) value ); break; case ExtendedClrTypeCode.Double: SetDouble_Unchecked( sink, setters, ordinal, (Double) value ); break; case ExtendedClrTypeCode.Empty: SetDBNull_Unchecked( sink, setters, ordinal ); break; case ExtendedClrTypeCode.Int16: SetInt16_Unchecked( sink, setters, ordinal, (Int16) value ); break; case ExtendedClrTypeCode.Int32: SetInt32_Unchecked( sink, setters, ordinal, (Int32) value ); break; case ExtendedClrTypeCode.Int64: SetInt64_Unchecked( sink, setters, ordinal, (Int64) value ); break; case ExtendedClrTypeCode.SByte: throw ADP.InvalidCast(); case ExtendedClrTypeCode.Single: SetSingle_Unchecked( sink, setters, ordinal, (Single) value ); break; case ExtendedClrTypeCode.String: SetString_LengthChecked( sink, setters, ordinal, metaData, (string) value, offset ); break; case ExtendedClrTypeCode.UInt16: throw ADP.InvalidCast(); case ExtendedClrTypeCode.UInt32: throw ADP.InvalidCast(); case ExtendedClrTypeCode.UInt64: throw ADP.InvalidCast(); case ExtendedClrTypeCode.Object: SetUdt_LengthChecked( sink, setters, ordinal, metaData, value ); break; case ExtendedClrTypeCode.ByteArray: SetByteArray_LengthChecked( sink, setters, ordinal, metaData, (byte[]) value, offset ); break; case ExtendedClrTypeCode.CharArray: SetCharArray_LengthChecked( sink, setters, ordinal, metaData, (char[]) value, offset ); break; case ExtendedClrTypeCode.Guid: SetGuid_Unchecked( sink, setters, ordinal, (Guid) value ); break; case ExtendedClrTypeCode.SqlBinary: SetSqlBinary_LengthChecked( sink, setters, ordinal, metaData, (SqlBinary) value, offset ); break; case ExtendedClrTypeCode.SqlBoolean: SetSqlBoolean_Unchecked( sink, setters, ordinal, (SqlBoolean) value ); break; case ExtendedClrTypeCode.SqlByte: SetSqlByte_Unchecked( sink, setters, ordinal, (SqlByte) value ); break; case ExtendedClrTypeCode.SqlDateTime: SetSqlDateTime_Checked( sink, setters, ordinal, metaData, (SqlDateTime) value ); break; case ExtendedClrTypeCode.SqlDouble: SetSqlDouble_Unchecked( sink, setters, ordinal, (SqlDouble) value ); break; case ExtendedClrTypeCode.SqlGuid: SetSqlGuid_Unchecked( sink, setters, ordinal, (SqlGuid) value ); break; case ExtendedClrTypeCode.SqlInt16: SetSqlInt16_Unchecked( sink, setters, ordinal, (SqlInt16) value ); break; case ExtendedClrTypeCode.SqlInt32: SetSqlInt32_Unchecked( sink, setters, ordinal, (SqlInt32) value ); break; case ExtendedClrTypeCode.SqlInt64: SetSqlInt64_Unchecked( sink, setters, ordinal, (SqlInt64) value ); break; case ExtendedClrTypeCode.SqlMoney: SetSqlMoney_Checked( sink, setters, ordinal, metaData, (SqlMoney) value ); break; case ExtendedClrTypeCode.SqlDecimal: SetSqlDecimal_Unchecked( sink, setters, ordinal, (SqlDecimal) value ); break; case ExtendedClrTypeCode.SqlSingle: SetSqlSingle_Unchecked( sink, setters, ordinal, (SqlSingle) value ); break; case ExtendedClrTypeCode.SqlString: SetSqlString_LengthChecked( sink, setters, ordinal, metaData, (SqlString) value, offset ); break; case ExtendedClrTypeCode.SqlChars: SetSqlChars_LengthChecked( sink, setters, ordinal, metaData, (SqlChars) value, offset ); break; case ExtendedClrTypeCode.SqlBytes: SetSqlBytes_LengthChecked( sink, setters, ordinal, metaData, (SqlBytes) value, offset ); break; case ExtendedClrTypeCode.SqlXml: SetSqlXml_Unchecked( sink, setters, ordinal, (SqlXml) value ); break; case ExtendedClrTypeCode.Stream: SetStream_Unchecked(sink, setters, ordinal, metaData, (StreamDataFeed) value); break; case ExtendedClrTypeCode.TextReader: SetTextReader_Unchecked(sink, setters, ordinal, metaData, (TextDataFeed) value); break; case ExtendedClrTypeCode.XmlReader: SetXmlReader_Unchecked(sink, setters, ordinal, ((XmlDataFeed) value)._source); break; default: Debug.Assert( false, "Unvalidated extendedtypecode: " + typeCode ); break; } }
internal static SqlDbType InferSqlDbTypeFromTypeCode(ExtendedClrTypeCode typeCode) { return(__extendedTypeCodeToSqlDbTypeMap[((int)typeCode) + 1]); }
// If we know we're only going to use this object to assign to a specific SqlDbType back end object, // we can save some processing time by only checking for the few valid types that can be assigned to the dbType. // This assumes a switch statement over SqlDbType is faster than getting the ClrTypeCode and iterating over a // series of if statements, or using a hash table. // NOTE: the form of these checks is taking advantage of a feature of the JIT compiler that is supposed to // optimize checks of the form '(xxx.GetType() == typeof( YYY ))'. The JIT team claimed at one point that // this doesn't even instantiate a Type instance, thus was the fastest method for individual comparisions. // Given that there's a known SqlDbType, thus a minimal number of comparisions, it's likely this is faster // than the other approaches considered (both GetType().GetTypeCode() switch and hash table using Type keys // must instantiate a Type object. The typecode switch also degenerates into a large if-then-else for // all but the primitive clr types. internal static ExtendedClrTypeCode DetermineExtendedTypeCodeForUseWithSqlDbType( SqlDbType dbType, bool isMultiValued, object value, Type udtType, ulong smiVersion) { ExtendedClrTypeCode extendedCode = ExtendedClrTypeCode.Invalid; // fast-track null, which is valid for all types if (null == value) { extendedCode = ExtendedClrTypeCode.Empty; } else if (DBNull.Value == value) { extendedCode = ExtendedClrTypeCode.DBNull; } else { switch (dbType) { case SqlDbType.BigInt: if (value.GetType() == typeof(Int64)) { extendedCode = ExtendedClrTypeCode.Int64; } else if (value.GetType() == typeof(SqlInt64)) { extendedCode = ExtendedClrTypeCode.SqlInt64; } else if (Type.GetTypeCode(value.GetType()) == TypeCode.Int64) { extendedCode = ExtendedClrTypeCode.Int64; } break; case SqlDbType.Binary: case SqlDbType.VarBinary: case SqlDbType.Image: case SqlDbType.Timestamp: if (value.GetType() == typeof(byte[])) { extendedCode = ExtendedClrTypeCode.ByteArray; } else if (value.GetType() == typeof(SqlBinary)) { extendedCode = ExtendedClrTypeCode.SqlBinary; } else if (value.GetType() == typeof(SqlBytes)) { extendedCode = ExtendedClrTypeCode.SqlBytes; } else if (value.GetType() == typeof(StreamDataFeed)) { extendedCode = ExtendedClrTypeCode.Stream; } break; case SqlDbType.Bit: if (value.GetType() == typeof(bool)) { extendedCode = ExtendedClrTypeCode.Boolean; } else if (value.GetType() == typeof(SqlBoolean)) { extendedCode = ExtendedClrTypeCode.SqlBoolean; } else if (Type.GetTypeCode(value.GetType()) == TypeCode.Boolean) { extendedCode = ExtendedClrTypeCode.Boolean; } break; case SqlDbType.Char: case SqlDbType.NChar: case SqlDbType.NText: case SqlDbType.NVarChar: case SqlDbType.Text: case SqlDbType.VarChar: if (value.GetType() == typeof(string)) { extendedCode = ExtendedClrTypeCode.String; } if (value.GetType() == typeof(TextDataFeed)) { extendedCode = ExtendedClrTypeCode.TextReader; } else if (value.GetType() == typeof(SqlString)) { extendedCode = ExtendedClrTypeCode.SqlString; } else if (value.GetType() == typeof(char[])) { extendedCode = ExtendedClrTypeCode.CharArray; } else if (value.GetType() == typeof(SqlChars)) { extendedCode = ExtendedClrTypeCode.SqlChars; } else if (value.GetType() == typeof(char)) { extendedCode = ExtendedClrTypeCode.Char; } else if (Type.GetTypeCode(value.GetType()) == TypeCode.Char) { extendedCode = ExtendedClrTypeCode.Char; } else if (Type.GetTypeCode(value.GetType()) == TypeCode.String) { extendedCode = ExtendedClrTypeCode.String; } break; case SqlDbType.Date: case SqlDbType.DateTime2: if (smiVersion >= SmiContextFactory.KatmaiVersion) { goto case SqlDbType.DateTime; } break; case SqlDbType.DateTime: case SqlDbType.SmallDateTime: if (value.GetType() == typeof(DateTime)) { extendedCode = ExtendedClrTypeCode.DateTime; } else if (value.GetType() == typeof(SqlDateTime)) { extendedCode = ExtendedClrTypeCode.SqlDateTime; } else if (Type.GetTypeCode(value.GetType()) == TypeCode.DateTime) { extendedCode = ExtendedClrTypeCode.DateTime; } break; case SqlDbType.Decimal: if (value.GetType() == typeof(Decimal)) { extendedCode = ExtendedClrTypeCode.Decimal; } else if (value.GetType() == typeof(SqlDecimal)) { extendedCode = ExtendedClrTypeCode.SqlDecimal; } else if (Type.GetTypeCode(value.GetType()) == TypeCode.Decimal) { extendedCode = ExtendedClrTypeCode.Decimal; } break; case SqlDbType.Real: if (value.GetType() == typeof(Single)) { extendedCode = ExtendedClrTypeCode.Single; } else if (value.GetType() == typeof(SqlSingle)) { extendedCode = ExtendedClrTypeCode.SqlSingle; } else if (Type.GetTypeCode(value.GetType()) == TypeCode.Single) { extendedCode = ExtendedClrTypeCode.Single; } break; case SqlDbType.Int: if (value.GetType() == typeof(Int32)) { extendedCode = ExtendedClrTypeCode.Int32; } else if (value.GetType() == typeof(SqlInt32)) { extendedCode = ExtendedClrTypeCode.SqlInt32; } else if (Type.GetTypeCode(value.GetType()) == TypeCode.Int32) { extendedCode = ExtendedClrTypeCode.Int32; } break; case SqlDbType.Money: case SqlDbType.SmallMoney: if (value.GetType() == typeof(SqlMoney)) { extendedCode = ExtendedClrTypeCode.SqlMoney; } else if (value.GetType() == typeof(Decimal)) { extendedCode = ExtendedClrTypeCode.Decimal; } else if (Type.GetTypeCode(value.GetType()) == TypeCode.Decimal) { extendedCode = ExtendedClrTypeCode.Decimal; } break; case SqlDbType.Float: if (value.GetType() == typeof(SqlDouble)) { extendedCode = ExtendedClrTypeCode.SqlDouble; } else if (value.GetType() == typeof(Double)) { extendedCode = ExtendedClrTypeCode.Double; } else if (Type.GetTypeCode(value.GetType()) == TypeCode.Double) { extendedCode = ExtendedClrTypeCode.Double; } break; case SqlDbType.UniqueIdentifier: if (value.GetType() == typeof(SqlGuid)) { extendedCode = ExtendedClrTypeCode.SqlGuid; } else if (value.GetType() == typeof(Guid)) { extendedCode = ExtendedClrTypeCode.Guid; } break; case SqlDbType.SmallInt: if (value.GetType() == typeof(Int16)) { extendedCode = ExtendedClrTypeCode.Int16; } else if (value.GetType() == typeof(SqlInt16)) { extendedCode = ExtendedClrTypeCode.SqlInt16; } else if (Type.GetTypeCode(value.GetType()) == TypeCode.Int16) { extendedCode = ExtendedClrTypeCode.Int16; } break; case SqlDbType.TinyInt: if (value.GetType() == typeof(Byte)) { extendedCode = ExtendedClrTypeCode.Byte; } else if (value.GetType() == typeof(SqlByte)) { extendedCode = ExtendedClrTypeCode.SqlByte; } else if (Type.GetTypeCode(value.GetType()) == TypeCode.Byte) { extendedCode = ExtendedClrTypeCode.Byte; } break; case SqlDbType.Variant: // SqlDbType doesn't help us here, call general-purpose function extendedCode = DetermineExtendedTypeCode(value); // Some types aren't allowed for Variants but are for the general-purpos function. // Match behavior of other types and return invalid in these cases. if (ExtendedClrTypeCode.SqlXml == extendedCode) { extendedCode = ExtendedClrTypeCode.Invalid; } break; case SqlDbType.Udt: // Validate UDT type if caller gave us a type to validate against if (null == udtType || value.GetType() == udtType ) { extendedCode = ExtendedClrTypeCode.Object; } else { extendedCode = ExtendedClrTypeCode.Invalid; } break; case SqlDbType.Time: if (value.GetType() == typeof(TimeSpan) && smiVersion >= SmiContextFactory.KatmaiVersion) { extendedCode = ExtendedClrTypeCode.TimeSpan; } break; case SqlDbType.DateTimeOffset: if (value.GetType() == typeof(DateTimeOffset) && smiVersion >= SmiContextFactory.KatmaiVersion) { extendedCode = ExtendedClrTypeCode.DateTimeOffset; } break; case SqlDbType.Xml: if (value.GetType() == typeof(SqlXml)) { extendedCode = ExtendedClrTypeCode.SqlXml; } if (value.GetType() == typeof(XmlDataFeed)) { extendedCode = ExtendedClrTypeCode.XmlReader; } else if (value.GetType() == typeof(System.String)) { extendedCode = ExtendedClrTypeCode.String; } break; case SqlDbType.Structured: if (isMultiValued) { if (value is DataTable) { extendedCode = ExtendedClrTypeCode.DataTable; } // Order is important, since some of these types are base types of the others. // Evaluate from most derived to parent types else if (value is IEnumerable <SqlDataRecord> ) { extendedCode = ExtendedClrTypeCode.IEnumerableOfSqlDataRecord; } else if (value is DbDataReader) { extendedCode = ExtendedClrTypeCode.DbDataReader; } } break; default: // Leave as invalid break; } } return(extendedCode); }
internal static SqlDbType InferSqlDbTypeFromTypeCode(ExtendedClrTypeCode typeCode) { return __extendedTypeCodeToSqlDbTypeMap[((int) typeCode) + 1]; }
// returns a sqldbtype for the given type code static internal SqlDbType InferSqlDbTypeFromTypeCode(ExtendedClrTypeCode typeCode) { Debug.Assert(typeCode >= ExtendedClrTypeCode.Invalid && typeCode <= ExtendedClrTypeCode.Last, "Someone added a typecode without adding support here!"); return s_extendedTypeCodeToSqlDbTypeMap[(int)typeCode + 1]; }
internal static void SetCompatibleValue(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, object value, ExtendedClrTypeCode typeCode, int offset) { switch (typeCode) { case ExtendedClrTypeCode.Invalid: throw ADP.UnknownDataType(value.GetType()); case ExtendedClrTypeCode.Boolean: SetBoolean_Unchecked(sink, setters, ordinal, (bool) value); return; case ExtendedClrTypeCode.Byte: SetByte_Unchecked(sink, setters, ordinal, (byte) value); return; case ExtendedClrTypeCode.Char: { char[] chArray2 = new char[] { (char) value }; SetCompatibleValue(sink, setters, ordinal, metaData, chArray2, ExtendedClrTypeCode.CharArray, 0); return; } case ExtendedClrTypeCode.DateTime: SetDateTime_Checked(sink, setters, ordinal, metaData, (DateTime) value); return; case ExtendedClrTypeCode.DBNull: SetDBNull_Unchecked(sink, setters, ordinal); return; case ExtendedClrTypeCode.Decimal: SetDecimal_PossiblyMoney(sink, setters, ordinal, metaData, (decimal) value); return; case ExtendedClrTypeCode.Double: SetDouble_Unchecked(sink, setters, ordinal, (double) value); return; case ExtendedClrTypeCode.Empty: SetDBNull_Unchecked(sink, setters, ordinal); return; case ExtendedClrTypeCode.Int16: SetInt16_Unchecked(sink, setters, ordinal, (short) value); return; case ExtendedClrTypeCode.Int32: SetInt32_Unchecked(sink, setters, ordinal, (int) value); return; case ExtendedClrTypeCode.Int64: SetInt64_Unchecked(sink, setters, ordinal, (long) value); return; case ExtendedClrTypeCode.SByte: throw ADP.InvalidCast(); case ExtendedClrTypeCode.Single: SetSingle_Unchecked(sink, setters, ordinal, (float) value); return; case ExtendedClrTypeCode.String: SetString_LengthChecked(sink, setters, ordinal, metaData, (string) value, offset); return; case ExtendedClrTypeCode.UInt16: throw ADP.InvalidCast(); case ExtendedClrTypeCode.UInt32: throw ADP.InvalidCast(); case ExtendedClrTypeCode.UInt64: throw ADP.InvalidCast(); case ExtendedClrTypeCode.Object: SetUdt_LengthChecked(sink, setters, ordinal, metaData, value); return; case ExtendedClrTypeCode.ByteArray: SetByteArray_LengthChecked(sink, setters, ordinal, metaData, (byte[]) value, offset); return; case ExtendedClrTypeCode.CharArray: SetCharArray_LengthChecked(sink, setters, ordinal, metaData, (char[]) value, offset); return; case ExtendedClrTypeCode.Guid: SetGuid_Unchecked(sink, setters, ordinal, (Guid) value); return; case ExtendedClrTypeCode.SqlBinary: SetSqlBinary_LengthChecked(sink, setters, ordinal, metaData, (SqlBinary) value, offset); return; case ExtendedClrTypeCode.SqlBoolean: SetSqlBoolean_Unchecked(sink, setters, ordinal, (SqlBoolean) value); return; case ExtendedClrTypeCode.SqlByte: SetSqlByte_Unchecked(sink, setters, ordinal, (SqlByte) value); return; case ExtendedClrTypeCode.SqlDateTime: SetSqlDateTime_Checked(sink, setters, ordinal, metaData, (SqlDateTime) value); return; case ExtendedClrTypeCode.SqlDouble: SetSqlDouble_Unchecked(sink, setters, ordinal, (SqlDouble) value); return; case ExtendedClrTypeCode.SqlGuid: SetSqlGuid_Unchecked(sink, setters, ordinal, (SqlGuid) value); return; case ExtendedClrTypeCode.SqlInt16: SetSqlInt16_Unchecked(sink, setters, ordinal, (SqlInt16) value); return; case ExtendedClrTypeCode.SqlInt32: SetSqlInt32_Unchecked(sink, setters, ordinal, (SqlInt32) value); return; case ExtendedClrTypeCode.SqlInt64: SetSqlInt64_Unchecked(sink, setters, ordinal, (SqlInt64) value); return; case ExtendedClrTypeCode.SqlMoney: SetSqlMoney_Checked(sink, setters, ordinal, metaData, (SqlMoney) value); return; case ExtendedClrTypeCode.SqlDecimal: SetSqlDecimal_Unchecked(sink, setters, ordinal, (SqlDecimal) value); return; case ExtendedClrTypeCode.SqlSingle: SetSqlSingle_Unchecked(sink, setters, ordinal, (SqlSingle) value); return; case ExtendedClrTypeCode.SqlString: SetSqlString_LengthChecked(sink, setters, ordinal, metaData, (SqlString) value, offset); return; case ExtendedClrTypeCode.SqlChars: SetSqlChars_LengthChecked(sink, setters, ordinal, metaData, (SqlChars) value, offset); return; case ExtendedClrTypeCode.SqlBytes: SetSqlBytes_LengthChecked(sink, setters, ordinal, metaData, (SqlBytes) value, offset); return; case ExtendedClrTypeCode.SqlXml: SetSqlXml_Unchecked(sink, setters, ordinal, (SqlXml) value); return; } }
internal static void SetCompatibleValueV200(SmiEventSink_Default sink, SmiTypedGetterSetter setters, int ordinal, SmiMetaData metaData, object value, ExtendedClrTypeCode typeCode, int offset, int length, ParameterPeekAheadValue peekAhead) { switch (typeCode) { case ExtendedClrTypeCode.DataTable: SetDataTable_Unchecked(sink, setters, ordinal, metaData, (DataTable) value); return; case ExtendedClrTypeCode.DbDataReader: SetDbDataReader_Unchecked(sink, setters, ordinal, metaData, (DbDataReader) value); return; case ExtendedClrTypeCode.IEnumerableOfSqlDataRecord: SetIEnumerableOfSqlDataRecord_Unchecked(sink, setters, ordinal, metaData, (IEnumerable<SqlDataRecord>) value, peekAhead); return; case ExtendedClrTypeCode.TimeSpan: SetTimeSpan_Checked(sink, setters, ordinal, metaData, (TimeSpan) value); return; case ExtendedClrTypeCode.DateTimeOffset: SetDateTimeOffset_Unchecked(sink, setters, ordinal, (DateTimeOffset) value); return; } SetCompatibleValue(sink, setters, ordinal, metaData, value, typeCode, offset); }
private static void SetDataTable_Unchecked(SmiEventSink_Default sink, SmiTypedGetterSetter setters, int ordinal, SmiMetaData metaData, DataTable value) { setters = setters.GetTypedGetterSetter(sink, ordinal); sink.ProcessMessagesAndThrow(); ExtendedClrTypeCode[] codeArray = new ExtendedClrTypeCode[metaData.FieldMetaData.Count]; for (int i = 0; i < metaData.FieldMetaData.Count; i++) { codeArray[i] = ExtendedClrTypeCode.Invalid; } foreach (DataRow row in value.Rows) { setters.NewElement(sink); sink.ProcessMessagesAndThrow(); for (int j = 0; j < metaData.FieldMetaData.Count; j++) { SmiMetaData data = metaData.FieldMetaData[j]; if (row.IsNull(j)) { SetDBNull_Unchecked(sink, setters, j); } else { object obj2 = row[j]; if (ExtendedClrTypeCode.Invalid == codeArray[j]) { codeArray[j] = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(data.SqlDbType, data.IsMultiValued, obj2, data.Type, 210L); } SetCompatibleValueV200(sink, setters, j, data, obj2, codeArray[j], 0, -1, null); } } } setters.EndElements(sink); sink.ProcessMessagesAndThrow(); }
// VSTFDevDiv#479681 - Data corruption when sending Katmai Date types to the server via TVP // Ensures proper handling on DateTime2 sub type for Sql_Variants and TVPs. internal static void SetCompatibleValueV200( SmiEventSink_Default sink, SmiTypedGetterSetter setters, int ordinal, SmiMetaData metaData, object value, ExtendedClrTypeCode typeCode, int offset, int length, ParameterPeekAheadValue peekAhead, SqlBuffer.StorageType storageType ) { // Ensure caller validated compatibility for types handled directly in this method Debug.Assert((ExtendedClrTypeCode.DataTable != typeCode && ExtendedClrTypeCode.DbDataReader != typeCode && ExtendedClrTypeCode.IEnumerableOfSqlDataRecord != typeCode) || CanAccessSetterDirectly(metaData, typeCode), "Un-validated type '" + typeCode + "' for metaData: " + metaData.SqlDbType); if (typeCode == ExtendedClrTypeCode.DateTime) { if (storageType == SqlBuffer.StorageType.DateTime2) SetDateTime2_Checked(sink, setters, ordinal, metaData, (DateTime)value); else if (storageType == SqlBuffer.StorageType.Date) SetDate_Checked(sink, setters, ordinal, metaData, (DateTime)value); else SetDateTime_Checked(sink, setters, ordinal, metaData, (DateTime)value); } else { SetCompatibleValueV200(sink, setters, ordinal, metaData, value, typeCode, offset, length, peekAhead); } }
private static bool CanAccessSetterDirectly(SmiMetaData metaData, ExtendedClrTypeCode setterTypeCode) { bool flag = __canAccessSetterDirectly[(int) setterTypeCode, (int) metaData.SqlDbType]; if (!flag || (((ExtendedClrTypeCode.DataTable != setterTypeCode) && (ExtendedClrTypeCode.DbDataReader != setterTypeCode)) && (ExtendedClrTypeCode.IEnumerableOfSqlDataRecord != setterTypeCode))) { return flag; } return metaData.IsMultiValued; }
// // Semantics support routines // private static void ThrowIfInvalidSetterAccess( SmiMetaData metaData, ExtendedClrTypeCode setterTypeCode ) { if ( !CanAccessSetterDirectly( metaData, setterTypeCode ) ) { throw ADP.InvalidCast(); } }
internal static void FillCompatibleITypedSettersFromRecord(SmiEventSink_Default sink, ITypedSettersV3 setters, SmiMetaData[] metaData, SqlDataRecord record, SmiDefaultFieldsProperty useDefaultValues) { for (int i = 0; i < metaData.Length; ++i) { if (null != useDefaultValues && useDefaultValues[i]) { continue; } if (record.IsDBNull(i)) { ValueUtilsSmi.SetDBNull_Unchecked(sink, setters, i); } else { switch (metaData[i].SqlDbType) { case SqlDbType.BigInt: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.Int64)); SetInt64_Unchecked(sink, setters, i, record.GetInt64(i)); break; case SqlDbType.Binary: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.SqlBytes)); SetBytes_FromRecord(sink, setters, i, metaData[i], record, 0); break; case SqlDbType.Bit: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.Boolean)); SetBoolean_Unchecked(sink, setters, i, record.GetBoolean(i)); break; case SqlDbType.Char: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.SqlChars)); SetChars_FromRecord(sink, setters, i, metaData[i], record, 0); break; case SqlDbType.DateTime: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.DateTime)); SetDateTime_Checked(sink, setters, i, metaData[i], record.GetDateTime(i)); break; case SqlDbType.Decimal: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.SqlDecimal)); SetSqlDecimal_Unchecked(sink, setters, i, record.GetSqlDecimal(i)); break; case SqlDbType.Float: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.Double)); SetDouble_Unchecked(sink, setters, i, record.GetDouble(i)); break; case SqlDbType.Image: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.SqlBytes)); SetBytes_FromRecord(sink, setters, i, metaData[i], record, 0); break; case SqlDbType.Int: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.Int32)); SetInt32_Unchecked(sink, setters, i, record.GetInt32(i)); break; case SqlDbType.Money: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.SqlMoney)); SetSqlMoney_Unchecked(sink, setters, i, metaData[i], record.GetSqlMoney(i)); break; case SqlDbType.NChar: case SqlDbType.NText: case SqlDbType.NVarChar: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.SqlChars)); SetChars_FromRecord(sink, setters, i, metaData[i], record, 0); break; case SqlDbType.Real: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.Single)); SetSingle_Unchecked(sink, setters, i, record.GetFloat(i)); break; case SqlDbType.UniqueIdentifier: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.Guid)); SetGuid_Unchecked(sink, setters, i, record.GetGuid(i)); break; case SqlDbType.SmallDateTime: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.DateTime)); SetDateTime_Checked(sink, setters, i, metaData[i], record.GetDateTime(i)); break; case SqlDbType.SmallInt: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.Int16)); SetInt16_Unchecked(sink, setters, i, record.GetInt16(i)); break; case SqlDbType.SmallMoney: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.SqlMoney)); SetSqlMoney_Checked(sink, setters, i, metaData[i], record.GetSqlMoney(i)); break; case SqlDbType.Text: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.SqlChars)); SetChars_FromRecord(sink, setters, i, metaData[i], record, 0); break; case SqlDbType.Timestamp: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.SqlBytes)); SetBytes_FromRecord(sink, setters, i, metaData[i], record, 0); break; case SqlDbType.TinyInt: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.Byte)); SetByte_Unchecked(sink, setters, i, record.GetByte(i)); break; case SqlDbType.VarBinary: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.SqlBytes)); SetBytes_FromRecord(sink, setters, i, metaData[i], record, 0); break; case SqlDbType.VarChar: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.String)); SetChars_FromRecord(sink, setters, i, metaData[i], record, 0); break; case SqlDbType.Xml: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.SqlXml)); SetSqlXml_Unchecked(sink, setters, i, record.GetSqlXml(i)); // perf improvement? break; case SqlDbType.Variant: object o = record.GetSqlValue(i); ExtendedClrTypeCode typeCode = MetaDataUtilsSmi.DetermineExtendedTypeCode(o); SetCompatibleValue(sink, setters, i, metaData[i], o, typeCode, 0); break; case SqlDbType.Udt: Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.SqlBytes)); SetBytes_FromRecord(sink, setters, i, metaData[i], record, 0); break; default: Debug.Assert(false, "unsupported DbType:" + metaData[i].SqlDbType.ToString()); throw ADP.NotSupported(); } } } }
private static void SetDataTable_Unchecked( SmiEventSink_Default sink, SmiTypedGetterSetter setters, int ordinal, SmiMetaData metaData, DataTable value ) { // Get the target gettersetter setters = setters.GetTypedGetterSetter(sink, ordinal); sink.ProcessMessagesAndThrow(); // iterate over all records // if first record was obtained earlier, use it prior to pulling more ExtendedClrTypeCode[] cellTypes = new ExtendedClrTypeCode[metaData.FieldMetaData.Count]; for(int i=0; i<metaData.FieldMetaData.Count; i++) { cellTypes[i] = ExtendedClrTypeCode.Invalid; } foreach(DataRow row in value.Rows) { setters.NewElement(sink); sink.ProcessMessagesAndThrow(); // Set all columns in the record for(int i=0; i<metaData.FieldMetaData.Count; i++) { SmiMetaData fieldMetaData = metaData.FieldMetaData[i]; if (row.IsNull(i)) { SetDBNull_Unchecked(sink, setters, i); } else { object cellValue = row[i]; // Only determine cell types for first row, to save expensive if (ExtendedClrTypeCode.Invalid == cellTypes[i]) { cellTypes[i] = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType( fieldMetaData.SqlDbType, fieldMetaData.IsMultiValued, cellValue, fieldMetaData.Type, // SmiContextFactory.KatmaiVersion ); } SetCompatibleValueV200(sink, setters, i, fieldMetaData, cellValue, cellTypes[i], 0, NoLengthLimit, null); } } } setters.EndElements(sink); sink.ProcessMessagesAndThrow(); }
public virtual int SetValues(params object[] values) { this.EnsureSubclassOverride(); if (values == null) { throw ADP.ArgumentNull("values"); } int num3 = (values.Length > this.FieldCount) ? this.FieldCount : values.Length; ExtendedClrTypeCode[] codeArray = new ExtendedClrTypeCode[num3]; for (int i = 0; i < num3; i++) { SqlMetaData sqlMetaData = this.GetSqlMetaData(i); codeArray[i] = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(sqlMetaData.SqlDbType, false, values[i], sqlMetaData.Type, this.SmiVersion); if (ExtendedClrTypeCode.Invalid == codeArray[i]) { throw ADP.InvalidCast(); } } for (int j = 0; j < num3; j++) { if (this.SmiVersion >= 210L) { ValueUtilsSmi.SetCompatibleValueV200(this._eventSink, this._recordBuffer, j, this.GetSmiMetaData(j), values[j], codeArray[j], 0, 0, null); } else { ValueUtilsSmi.SetCompatibleValue(this._eventSink, this._recordBuffer, j, this.GetSmiMetaData(j), values[j], codeArray[j], 0); } } return num3; }