public static void WriteParameterLen(this TdsPackageWriter writer, TdsMetaType.MetaDataWrite metaType, int size, bool isNull) { if (isNull) { return; } //write length of the parameter if (metaType.IsPlp) { writer.WriteInt64(size); } else if (metaType.IsLong) { // text/image/SQLVariant have a 4 byte length, plp datatypes have 8 byte lengths writer.WriteInt32(size); } else if (metaType.NullableType == TdsEnums.SQLDATETIME2 || metaType.NullableType == TdsEnums.SQLTIME || metaType.NullableType == TdsEnums.SQLDATETIMEOFFSET) { writer.WriteByte((byte)size); } else if (metaType.NullableType == TdsEnums.SQLBIGVARBINARY || metaType.NullableType == TdsEnums.SQLBIGCHAR || metaType.NullableType == TdsEnums.SQLNCHAR || metaType.NullableType == TdsEnums.SQLNVARCHAR) //skip varchar { writer.WriteInt16(size); } else { writer.WriteByte((byte)size); // 1 byte for everything else } }
public TdsParameter(string name, double value) { Name = name; Value = value; Size = 8; MetaData = TdsMetaType.SqlFlt8; SqlName = "float"; }
public TdsParameter(string name, float value) { Name = name; Value = value; Size = 4; MetaData = TdsMetaType.SqlFlt4; SqlName = "real"; }
public TdsParameter(string name, long value) { Name = name; Value = value; Size = 8; MetaData = TdsMetaType.SqlInt4; SqlName = "bigint"; }
public TdsParameter(string name, int value) { Name = name; Value = value; Size = 4; MetaData = TdsMetaType.SqlInt3; SqlName = "int"; }
public TdsParameter(string name, short value) { Name = name; Value = value; Size = 2; MetaData = TdsMetaType.SqlInt2; SqlName = "smallint"; }
public TdsParameter(string name, byte value) { Name = name; Value = value; Size = 1; MetaData = TdsMetaType.SqlInt1; SqlName = "tinyint"; }
public TdsParameter(string name, bool value) { Name = name; Value = value; Size = 1; MetaData = TdsMetaType.SqlBitN; SqlName = "bit"; }
public TdsParameter(string name, string value) { Name = name; Value = value; Size = value.Length * 2; MetaData = TdsMetaType.SqlNVarChar; SqlName = $"nvarchar({value.Length})"; }
public TdsParameter(string name, DateTime value) { Name = name; Value = value; Size = 8; MetaData = TdsMetaType.SqlDateTimN; SqlName = "datetime"; }
public TdsParameter(string name, decimal value, byte?scale = null) { Name = name; Value = value; Size = 17; Scale = (byte)(scale ?? (decimal.GetBits(value)[3] >> 16) & 0xff); MetaData = TdsMetaType.SqlNumericN; SqlName = $"decimal(28,{Scale})"; }
public static void WriteTypeInfoLen(this TdsPackageWriter writer, TdsMetaType.MetaDataWrite metaType, int size, bool isNull) { //write length or isNull information about the type if (metaType.IsPlp) { if (isNull) { writer.WriteInt64(unchecked ((long)TdsEnums.SQL_PLP_NULL)); } else { writer.WriteInt64(size); } } else if (metaType.IsLong) { // text/image/SQLVariant have a 4 byte length, plp datatypes have 8 byte lengths if (isNull) { writer.WriteInt32(unchecked ((int)TdsEnums.VARLONGNULL)); } else { writer.WriteInt32(size); } } else if (metaType.NullableType == TdsEnums.SQLDATETIME2 || metaType.NullableType == TdsEnums.SQLTIME || metaType.NullableType == TdsEnums.SQLDATETIMEOFFSET) { if (isNull) { writer.WriteByte(TdsEnums.FIXEDNULL); } else { writer.WriteByte((byte)size); } } else if (metaType.NullableType == TdsEnums.SQLBIGVARBINARY || metaType.NullableType == TdsEnums.SQLBIGCHAR || metaType.NullableType == TdsEnums.SQLNCHAR || metaType.NullableType == TdsEnums.SQLNVARCHAR) //skip varchar { // non-long but variable length column, must be a BIG* type: 2 byte length writer.WriteInt16(isNull ? TdsEnums.VARNULL : size); } else { if (isNull) { writer.WriteByte(TdsEnums.FIXEDNULL); } else { writer.WriteByte((byte)size); // 1 byte for everything else } } }
public static void WriteValue(TdsPackageWriter writer, object?value, TdsMetaType.MetaDataWrite metaData, bool isNull) { // write the value now if (isNull) { return; } switch (value) { case string v: writer.WriteUnicodeString(v); break; case decimal v: writer.WriteSqlDecimal(v, 17); break; case bool v: writer.WriteByte(v ? 1 : 0); break; case DateTime v: writer.WriteDateTime(v); break; case byte v: writer.WriteByte(v); break; case short v: writer.WriteInt16(v); break; case int v: writer.WriteInt32(v); break; case long v: writer.WriteInt64(v); break; case float v: writer.WriteFloat(v); break; case double v: writer.WriteDouble(v); break; } }
public static void WriteTdsTypeInfo(this TdsPackageWriter writer, TdsMetaType.MetaDataWrite metaData, int size, bool isNull, SqlCollations defaultCollation, byte scale) { var mt = metaData; writer.WriteByte(mt.NullableType); writer.WriteTypeInfoLen(mt, size, isNull); //typeinfo varlen if (mt.HasCollation) { writer.WriteCollation2(defaultCollation); } if (mt.HasPrecision) { writer.WriteByte(28); //Max clr precision } if (mt.HasScale) { writer.WriteByte(scale); // } writer.WriteParameterLen(metaData, size, isNull); //len parameter }
public static void WriteTdsTypeInfo(this TdsPackageWriter writer, TdsMetaType.MetaDataWrite metaData, int size, bool isNull) { WriteTdsTypeInfo(writer, metaData, size, isNull, null, 0); }