Esempio n. 1
0
        private static object GetDecimalValue(TdsResponseStream stream, TdsType type, FormatToken.Format f)
        {
            int len = stream.Read();

            if (len > 0)
            {
                var scale = Math.Pow(10, f.Scale ?? 0);

                bool sign  = stream.Read() != 0;
                var  bytes = stream.ReadBytes(len - 1);
                var  bi    = new BigInteger(bytes.Reverse().ToArray());

                var d = (decimal)bi;
                if (sign)
                {
                    d = -d;
                }

                return(d / (decimal)scale);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 2
0
        private static object GetMoneyValue(TdsResponseStream stream, TdsType type)
        {
            int len;

            if (type == TdsType.TDS_MONEY)
            {
                len = 8;
            }
            else if (type == TdsType.TDS_MONEYN)
            {
                len = stream.Read();
            }
            else
            {
                len = 4;
            }

            long?x = null;

            if (len == 4)
            {
                x = stream.ReadInt();
            }
            else if (len == 8)
            {
                int hi = stream.ReadInt();
                int lo = stream.ReadInt();
                x = lo | (hi << 32);
            }

            return(x.HasValue ? new decimal(x.Value) * 0.0001m : (decimal?)null);
        }
Esempio n. 3
0
        public static int GetFormatLen(TdsType type, string name, Encoding encoder)
        {
            var t    = _types[type];
            int size = 8;

            if (name != null)
            {
                size += encoder.GetByteCount(name);
            }

            switch (t.FormatFormat)
            {
            case FormatFormat.Empty:
                break;

            case FormatFormat.LenOneByte:
                size += 1;
                break;

            case FormatFormat.LenFourBytes:
                size += 4;
                break;

            case FormatFormat.Decimal:
                size += 3;
                break;

            case FormatFormat.Image:
            default:
                throw new NotImplementedException($"Unsupported type {type}");
            }

            return(size);
        }
Esempio n. 4
0
        public TdsParameter(string parameterName, TdsType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
        {
            metaParameter = new TdsMetaParameter(parameterName, size, isNullable, precision, scale, value);

            TdsType       = dbType;
            Direction     = direction;
            SourceColumn  = sourceColumn;
            SourceVersion = sourceVersion;
        }
Esempio n. 5
0
 public TypeInfo(TdsType type, bool fixedLen, bool nullable, bool converted, FormatFormat ff, DataFormat df)
 {
     Type         = type;
     FixedLen     = fixedLen;
     Nullable     = nullable;
     Converted    = converted;
     FormatFormat = ff;
     DataFormat   = df;
 }
Esempio n. 6
0
        private static object GetDatetimeValue(TdsResponseStream stream, TdsType type)
        {
            int len;
            int dayTicks;
            int timeTicks;
            int minutes;

            if (type == TdsType.TDS_DATETIMEN)
            {
                len = stream.Read();
            }
            else if (type == TdsType.TDS_SHORTDATE)
            {
                len = 4;
            }
            else
            {
                len = 8;
            }

            switch (len)
            {
            case 8:
                dayTicks  = stream.ReadInt();
                timeTicks = stream.ReadInt();
                return(new System.Data.SqlTypes.SqlDateTime(dayTicks, timeTicks).Value);

            case 4:
                dayTicks = stream.ReadShort();
                minutes  = stream.ReadShort();
                return(new DateTime(1900, 1, 1).AddDays(dayTicks).AddMinutes(minutes));

            default:
            case 0:
                return(null);
            }
        }
		public TdsParameter (string parameterName, TdsType dbType, int size) 
			: this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
		{
		}
		// When the TdsType is set, we also set the DbType, as well as the SQL Server
		// string representation of the type name.  If the TdsType is not convertible
		// to a DbType, throw an exception.
		private void SetTdsType (TdsType type)
		{
			string exception = String.Format ("No mapping exists from TdsType {0} to a known DbType.", type);

			switch (type) {
			case TdsType.BigInt:
				MetaParameter.TypeName = "bigint";
				dbType = DbType.Int64;
				break;
			case TdsType.Binary:
				MetaParameter.TypeName = "binary";
				dbType = DbType.Binary;
				break;
			case TdsType.Timestamp:
				MetaParameter.TypeName = "timestamp";
				dbType = DbType.Binary;
				break;
			case TdsType.VarBinary:
				MetaParameter.TypeName = "varbinary";
				dbType = DbType.Binary;
				break;
			case TdsType.Bit:
				MetaParameter.TypeName = "bit";
				dbType = DbType.Boolean;
				break;
			case TdsType.Char:
				MetaParameter.TypeName = "char";
				dbType = DbType.AnsiStringFixedLength;
				break;
			case TdsType.DateTime:
				MetaParameter.TypeName = "datetime";
				dbType = DbType.DateTime;
				break;
			case TdsType.SmallDateTime:
				MetaParameter.TypeName = "smalldatetime";
				dbType = DbType.DateTime;
				break;
			case TdsType.Decimal:
				MetaParameter.TypeName = "decimal";
				dbType = DbType.Decimal;
				break;
			case TdsType.Float:
				MetaParameter.TypeName = "float";
				dbType = DbType.Double;
				break;
			case TdsType.Image:
				MetaParameter.TypeName = "image";
				dbType = DbType.Binary;
				break;
			case TdsType.Int:
				MetaParameter.TypeName = "int";
				dbType = DbType.Int32;
				break;
			case TdsType.Money:
				MetaParameter.TypeName = "money";
				dbType = DbType.Currency;
				break;
			case TdsType.SmallMoney:
				MetaParameter.TypeName = "smallmoney";
				dbType = DbType.Currency;
				break;
			case TdsType.NChar:
				MetaParameter.TypeName = "nchar";
				dbType = DbType.StringFixedLength;
				break;
			case TdsType.NText:
				MetaParameter.TypeName = "ntext";
				dbType = DbType.String;
				break;
			case TdsType.NVarChar:
				MetaParameter.TypeName = "nvarchar";
				dbType = DbType.String;
				break;
			case TdsType.Real:
				MetaParameter.TypeName = "real";
				dbType = DbType.Single;
				break;
			case TdsType.SmallInt:
				MetaParameter.TypeName = "smallint";
				dbType = DbType.Int16;
				break;
			case TdsType.Text:
				MetaParameter.TypeName = "text";
				dbType = DbType.AnsiString;
				break;
			case TdsType.VarChar:
				MetaParameter.TypeName = "varchar";
				dbType = DbType.AnsiString;
				break;
			case TdsType.TinyInt:
				MetaParameter.TypeName = "tinyint";
				dbType = DbType.Byte;
				break;
			case TdsType.UniqueIdentifier:
				MetaParameter.TypeName = "uniqueidentifier";
				dbType = DbType.Guid;
				break;
			case TdsType.Variant:
				MetaParameter.TypeName = "sql_variant";
				dbType = DbType.Object;
				break;
			default:
				throw new ArgumentException (exception);
			}
			sybaseType = type;
		}
		// When the DbType is set, we also set the TdsType, as well as the SQL Server
		// string representation of the type name.  If the DbType is not convertible
		// to an TdsType, throw an exception.
		private void SetDbType (DbType type)
		{
			string exception = String.Format ("No mapping exists from DbType {0} to a known TdsType.", type);

			switch (type) {
			case DbType.AnsiString:
				MetaParameter.TypeName = "varchar";
				sybaseType = TdsType.VarChar;
				break;
			case DbType.AnsiStringFixedLength:
				MetaParameter.TypeName = "char";
				sybaseType = TdsType.Char;
				break;
			case DbType.Binary:
				MetaParameter.TypeName = "varbinary";
				sybaseType = TdsType.VarBinary;
				break;
			case DbType.Boolean:
				MetaParameter.TypeName = "bit";
				sybaseType = TdsType.Bit;
				break;
			case DbType.Byte:
				MetaParameter.TypeName = "tinyint";
				sybaseType = TdsType.TinyInt;
				break;
			case DbType.Currency:
				sybaseType = TdsType.Money;
				MetaParameter.TypeName = "money";
				break;
			case DbType.Date:
			case DbType.DateTime:
				MetaParameter.TypeName = "datetime";
				sybaseType = TdsType.DateTime;
				break;
			case DbType.Decimal:
				MetaParameter.TypeName = "decimal";
				sybaseType = TdsType.Decimal;
				break;
			case DbType.Double:
				MetaParameter.TypeName = "float";
				sybaseType = TdsType.Float;
				break;
			case DbType.Guid:
				MetaParameter.TypeName = "uniqueidentifier";
				sybaseType = TdsType.UniqueIdentifier;
				break;
			case DbType.Int16:
				MetaParameter.TypeName = "smallint";
				sybaseType = TdsType.SmallInt;
				break;
			case DbType.Int32:
				MetaParameter.TypeName = "int";
				sybaseType = TdsType.Int;
				break;
			case DbType.Int64:
				MetaParameter.TypeName = "bigint";
				sybaseType = TdsType.BigInt;
				break;
			case DbType.Object:
				MetaParameter.TypeName = "sql_variant";
				sybaseType = TdsType.Variant;
				break;
			case DbType.Single:
				MetaParameter.TypeName = "real";
				sybaseType = TdsType.Real;
				break;
			case DbType.String:
				MetaParameter.TypeName = "nvarchar";
				sybaseType = TdsType.NVarChar;
				break;
			case DbType.StringFixedLength:
				MetaParameter.TypeName = "nchar";
				sybaseType = TdsType.NChar;
				break;
			case DbType.Time:
				MetaParameter.TypeName = "datetime";
				sybaseType = TdsType.DateTime;
				break;
			default:
				throw new ArgumentException (exception);
			}
			dbType = type;
		}
Esempio n. 10
0
        public object GetTdsValue(int i)
        {
            TdsType type  = (TdsType)(schemaTable.Rows [i]["ProviderType"]);
            object  value = GetValue(i);

            switch (type)
            {
            case TdsType.BigInt:
                if (value == DBNull.Value)
                {
                    return(TdsInt64.Null);
                }
                return((TdsInt64)((long)value));

            case TdsType.Binary:
            case TdsType.Image:
            case TdsType.VarBinary:
            case TdsType.Timestamp:
                if (value == DBNull.Value)
                {
                    return(TdsBinary.Null);
                }
                return((TdsBinary)((byte[])value));

            case TdsType.Bit:
                if (value == DBNull.Value)
                {
                    return(TdsBoolean.Null);
                }
                return((TdsBoolean)((bool)value));

            case TdsType.Char:
            case TdsType.NChar:
            case TdsType.NText:
            case TdsType.NVarChar:
            case TdsType.Text:
            case TdsType.VarChar:
                if (value == DBNull.Value)
                {
                    return(TdsString.Null);
                }
                return((TdsString)((string)value));

            case TdsType.DateTime:
            case TdsType.SmallDateTime:
                if (value == DBNull.Value)
                {
                    return(TdsDateTime.Null);
                }
                return((TdsDateTime)((DateTime)value));

            case TdsType.Decimal:
                if (value == DBNull.Value)
                {
                    return(TdsDecimal.Null);
                }
                if (value is TdsBigDecimal)
                {
                    return(TdsDecimal.FromTdsBigDecimal((TdsBigDecimal)value));
                }
                return((TdsDecimal)((decimal)value));

            case TdsType.Float:
                if (value == DBNull.Value)
                {
                    return(TdsDouble.Null);
                }
                return((TdsDouble)((double)value));

            case TdsType.Int:
                if (value == DBNull.Value)
                {
                    return(TdsInt32.Null);
                }
                return((TdsInt32)((int)value));

            case TdsType.Money:
            case TdsType.SmallMoney:
                if (value == DBNull.Value)
                {
                    return(TdsMoney.Null);
                }
                return((TdsMoney)((decimal)value));

            case TdsType.Real:
                if (value == DBNull.Value)
                {
                    return(TdsSingle.Null);
                }
                return((TdsSingle)((float)value));

            case TdsType.UniqueIdentifier:
                if (value == DBNull.Value)
                {
                    return(TdsGuid.Null);
                }
                return((TdsGuid)((Guid)value));

            case TdsType.SmallInt:
                if (value == DBNull.Value)
                {
                    return(TdsInt16.Null);
                }
                return((TdsInt16)((short)value));

            case TdsType.TinyInt:
                if (value == DBNull.Value)
                {
                    return(TdsByte.Null);
                }
                return((TdsByte)((byte)value));
            }

            throw new InvalidOperationException("The type of this column is unknown.");
        }
		public TdsParameter Add (string parameterName, TdsType sybaseType, int size, string sourceColumn)
		{
			return Add (new TdsParameter (parameterName, sybaseType, size, sourceColumn));
		}
		public TdsParameter (string parameterName, TdsType dbType, int size, string sourceColumn) 
			: this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, sourceColumn, DataRowVersion.Current, null)
		{
		}
Esempio n. 13
0
 public TdsParameter(string parameterName, TdsType dbType, int size, string sourceColumn)
     : this(parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, sourceColumn, DataRowVersion.Current, null)
 {
 }
Esempio n. 14
0
 public TdsParameter(string parameterName, TdsType dbType, int size)
     : this(parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
 {
 }
Esempio n. 15
0
        // When the TdsType is set, we also set the DbType, as well as the SQL Server
        // string representation of the type name.  If the TdsType is not convertible
        // to a DbType, throw an exception.
        private void SetTdsType(TdsType type)
        {
            string exception = String.Format("No mapping exists from TdsType {0} to a known DbType.", type);

            switch (type)
            {
            case TdsType.BigInt:
                MetaParameter.TypeName = "bigint";
                dbType = DbType.Int64;
                break;

            case TdsType.Binary:
                MetaParameter.TypeName = "binary";
                dbType = DbType.Binary;
                break;

            case TdsType.Timestamp:
                MetaParameter.TypeName = "timestamp";
                dbType = DbType.Binary;
                break;

            case TdsType.VarBinary:
                MetaParameter.TypeName = "varbinary";
                dbType = DbType.Binary;
                break;

            case TdsType.Bit:
                MetaParameter.TypeName = "bit";
                dbType = DbType.Boolean;
                break;

            case TdsType.Char:
                MetaParameter.TypeName = "char";
                dbType = DbType.AnsiStringFixedLength;
                break;

            case TdsType.DateTime:
                MetaParameter.TypeName = "datetime";
                dbType = DbType.DateTime;
                break;

            case TdsType.SmallDateTime:
                MetaParameter.TypeName = "smalldatetime";
                dbType = DbType.DateTime;
                break;

            case TdsType.Decimal:
                MetaParameter.TypeName = "decimal";
                dbType = DbType.Decimal;
                break;

            case TdsType.Float:
                MetaParameter.TypeName = "float";
                dbType = DbType.Double;
                break;

            case TdsType.Image:
                MetaParameter.TypeName = "image";
                dbType = DbType.Binary;
                break;

            case TdsType.Int:
                MetaParameter.TypeName = "int";
                dbType = DbType.Int32;
                break;

            case TdsType.Money:
                MetaParameter.TypeName = "money";
                dbType = DbType.Currency;
                break;

            case TdsType.SmallMoney:
                MetaParameter.TypeName = "smallmoney";
                dbType = DbType.Currency;
                break;

            case TdsType.NChar:
                MetaParameter.TypeName = "nchar";
                dbType = DbType.StringFixedLength;
                break;

            case TdsType.NText:
                MetaParameter.TypeName = "ntext";
                dbType = DbType.String;
                break;

            case TdsType.NVarChar:
                MetaParameter.TypeName = "nvarchar";
                dbType = DbType.String;
                break;

            case TdsType.Real:
                MetaParameter.TypeName = "real";
                dbType = DbType.Single;
                break;

            case TdsType.SmallInt:
                MetaParameter.TypeName = "smallint";
                dbType = DbType.Int16;
                break;

            case TdsType.Text:
                MetaParameter.TypeName = "text";
                dbType = DbType.AnsiString;
                break;

            case TdsType.VarChar:
                MetaParameter.TypeName = "varchar";
                dbType = DbType.AnsiString;
                break;

            case TdsType.TinyInt:
                MetaParameter.TypeName = "tinyint";
                dbType = DbType.Byte;
                break;

            case TdsType.UniqueIdentifier:
                MetaParameter.TypeName = "uniqueidentifier";
                dbType = DbType.Guid;
                break;

            case TdsType.Variant:
                MetaParameter.TypeName = "sql_variant";
                dbType = DbType.Object;
                break;

            default:
                throw new ArgumentException(exception);
            }
            sybaseType = type;
        }
Esempio n. 16
0
        // When the DbType is set, we also set the TdsType, as well as the SQL Server
        // string representation of the type name.  If the DbType is not convertible
        // to an TdsType, throw an exception.
        private void SetDbType(DbType type)
        {
            string exception = String.Format("No mapping exists from DbType {0} to a known TdsType.", type);

            switch (type)
            {
            case DbType.AnsiString:
                MetaParameter.TypeName = "varchar";
                sybaseType             = TdsType.VarChar;
                break;

            case DbType.AnsiStringFixedLength:
                MetaParameter.TypeName = "char";
                sybaseType             = TdsType.Char;
                break;

            case DbType.Binary:
                MetaParameter.TypeName = "varbinary";
                sybaseType             = TdsType.VarBinary;
                break;

            case DbType.Boolean:
                MetaParameter.TypeName = "bit";
                sybaseType             = TdsType.Bit;
                break;

            case DbType.Byte:
                MetaParameter.TypeName = "tinyint";
                sybaseType             = TdsType.TinyInt;
                break;

            case DbType.Currency:
                sybaseType             = TdsType.Money;
                MetaParameter.TypeName = "money";
                break;

            case DbType.Date:
            case DbType.DateTime:
                MetaParameter.TypeName = "datetime";
                sybaseType             = TdsType.DateTime;
                break;

            case DbType.Decimal:
                MetaParameter.TypeName = "decimal";
                sybaseType             = TdsType.Decimal;
                break;

            case DbType.Double:
                MetaParameter.TypeName = "float";
                sybaseType             = TdsType.Float;
                break;

            case DbType.Guid:
                MetaParameter.TypeName = "uniqueidentifier";
                sybaseType             = TdsType.UniqueIdentifier;
                break;

            case DbType.Int16:
                MetaParameter.TypeName = "smallint";
                sybaseType             = TdsType.SmallInt;
                break;

            case DbType.Int32:
                MetaParameter.TypeName = "int";
                sybaseType             = TdsType.Int;
                break;

            case DbType.Int64:
                MetaParameter.TypeName = "bigint";
                sybaseType             = TdsType.BigInt;
                break;

            case DbType.Object:
                MetaParameter.TypeName = "sql_variant";
                sybaseType             = TdsType.Variant;
                break;

            case DbType.Single:
                MetaParameter.TypeName = "real";
                sybaseType             = TdsType.Real;
                break;

            case DbType.String:
                MetaParameter.TypeName = "nvarchar";
                sybaseType             = TdsType.NVarChar;
                break;

            case DbType.StringFixedLength:
                MetaParameter.TypeName = "nchar";
                sybaseType             = TdsType.NChar;
                break;

            case DbType.Time:
                MetaParameter.TypeName = "datetime";
                sybaseType             = TdsType.DateTime;
                break;

            default:
                throw new ArgumentException(exception);
            }
            dbType = type;
        }
Esempio n. 17
0
 public TdsParameter Add(string parameterName, TdsType sybaseType, int size, string sourceColumn)
 {
     return(Add(new TdsParameter(parameterName, sybaseType, size, sourceColumn)));
 }
Esempio n. 18
0
 public TdsParameter Add(string parameterName, TdsType sybaseType, int size)
 {
     return(Add(new TdsParameter(parameterName, sybaseType, size)));
 }
		public TdsParameter (string parameterName, TdsType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value) 
		{
			metaParameter = new TdsMetaParameter (parameterName, size, isNullable, precision, scale, value);

			TdsType = dbType;
			Direction = direction;
			SourceColumn = sourceColumn;
			SourceVersion = sourceVersion;
		}
		public TdsParameter Add (string parameterName, TdsType sybaseType, int size)
		{
			return Add (new TdsParameter (parameterName, sybaseType, size));
		}