Esempio n. 1
0
 public void ConversionByteFormatException()
 {
     Assert.Throws <FormatException>(() =>
     {
         byte test = _test1.ToSqlByte().Value;
     });
 }
Esempio n. 2
0
 public void ConversionByteFormatException()
 {
     ExceptionAssert.Throws <FormatException>(
         delegate
     {
         byte test = Test1.ToSqlByte().Value;
     });
 }
Esempio n. 3
0
        public void Conversions()
        {
            SqlString String250   = new SqlString("250");
            SqlString String9E300 = new SqlString("9E+300");

            // ToSqlBoolean ()
            Assert.IsTrue((new SqlString("1")).ToSqlBoolean().Value, "#O02");
            Assert.IsTrue(!(new SqlString("0")).ToSqlBoolean().Value, "#O03");
            Assert.IsTrue((new SqlString("True")).ToSqlBoolean().Value, "#O04");
            Assert.IsTrue(!(new SqlString("FALSE")).ToSqlBoolean().Value, "#O05");
            Assert.IsTrue(SqlString.Null.ToSqlBoolean().IsNull, "#O06");

            // ToSqlByte ()
            Assert.AreEqual((byte)250, String250.ToSqlByte().Value, "#O08");

            // ToSqlDateTime
            Assert.AreEqual(10,
                            (new SqlString("2002-10-10")).ToSqlDateTime().Value.Day, "#O11");

            // ToSqlDecimal ()
            Assert.AreEqual((decimal)250, String250.ToSqlDecimal().Value, "#O16");

            // ToSqlDouble
            Assert.AreEqual((SqlDouble)9E+300, String9E300.ToSqlDouble(), "#O19");

            // ToSqlGuid
            SqlString TestGuid = new SqlString("11111111-1111-1111-1111-111111111111");

            Assert.AreEqual(new SqlGuid("11111111-1111-1111-1111-111111111111"), TestGuid.ToSqlGuid(), "#O22");

            // ToSqlInt16 ()
            Assert.AreEqual((short)250, String250.ToSqlInt16().Value, "#O24");

            // ToSqlInt32 ()
            Assert.AreEqual((int)250, String250.ToSqlInt32().Value, "#O27");

            // ToSqlInt64 ()
            Assert.AreEqual((long)250, String250.ToSqlInt64().Value, "#O32");

            // ToSqlMoney ()
            Assert.AreEqual(250.0000M, String250.ToSqlMoney().Value, "#O35");

            // ToSqlSingle ()
            Assert.AreEqual((float)250, String250.ToSqlSingle().Value, "#O38");

            // ToString ()
            Assert.AreEqual("First TestString", Test1.ToString(), "#O41");
        }
Esempio n. 4
0
        public void Conversions()
        {
            SqlString string250   = new SqlString("250");
            SqlString string9E300 = new SqlString("9E+300");

            // ToSqlBoolean ()
            Assert.True((new SqlString("1")).ToSqlBoolean().Value);
            Assert.False((new SqlString("0")).ToSqlBoolean().Value);
            Assert.True((new SqlString("True")).ToSqlBoolean().Value);
            Assert.False((new SqlString("FALSE")).ToSqlBoolean().Value);
            Assert.True(SqlString.Null.ToSqlBoolean().IsNull);

            // ToSqlByte ()
            Assert.Equal((byte)250, string250.ToSqlByte().Value);

            // ToSqlDateTime
            Assert.Equal(10, (new SqlString("2002-10-10")).ToSqlDateTime().Value.Day);

            // ToSqlDecimal ()
            Assert.Equal(250, string250.ToSqlDecimal().Value);

            // ToSqlDouble
            Assert.Equal(9E+300, string9E300.ToSqlDouble());

            // ToSqlGuid
            SqlString TestGuid = new SqlString("11111111-1111-1111-1111-111111111111");

            Assert.Equal(new SqlGuid("11111111-1111-1111-1111-111111111111"), TestGuid.ToSqlGuid());

            // ToSqlInt16 ()
            Assert.Equal((short)250, string250.ToSqlInt16().Value);

            // ToSqlInt32 ()
            Assert.Equal(250, string250.ToSqlInt32().Value);

            // ToSqlInt64 ()
            Assert.Equal(250, string250.ToSqlInt64().Value);

            // ToSqlMoney ()
            Assert.Equal(250.0000M, string250.ToSqlMoney().Value);

            // ToSqlSingle ()
            Assert.Equal(250, string250.ToSqlSingle().Value);

            // ToString ()
            Assert.Equal("First TestString", _test1.ToString());
        }
Esempio n. 5
0
 public void ConversionByteFormatException()
 {
     byte test = Test1.ToSqlByte().Value;
 }
Esempio n. 6
0
    /// <summary>
    ///  Checks whether given string is a certain type of "numeric"
    /// </summary>
    /// <param name="field">Varchar string</param>
    /// <param name="sqltype">A type to compare</param>
    /// <returns></returns>
    public static SqlBoolean fnIsNumeric(SqlString field, string sqltype)
    {
        var    result       = new SqlBoolean(0); // default False
        string errorMessage = string.Empty;

        // Determine base type and any decimal precision parameters
        if (sqltype == null)
        {
            return(result);
        }
        var type          = sqltype.ToString().Trim().ToLower();
        var typePrecision = string.Empty;

        if (type.Contains("("))
        {
            typePrecision = type.Substring(type.IndexOf("(") + 1).Replace(")", "").Trim();
            type          = type.Substring(0, type.IndexOf("("));
        }

        try
        {
            switch (type)
            {
            case "bigint":
                var sqlBigInt = new SqlInt64();
                sqlBigInt = field.ToSqlInt64();
                if (sqlBigInt.IsNull == false)
                {
                    result = true;
                }
                break;

            case "bit":
                if (field.Value.Contains("+") || field.Value.Contains("-"))
                {
                    result = false;
                }
                else
                {
                    var sqlBit = new SqlByte();
                    sqlBit = field.ToSqlByte();
                    if (sqlBit.IsNull == false && (sqlBit == 0 || sqlBit == 1))
                    {
                        result = true;
                    }
                }
                break;

            case "decimal":
            case "numeric":
                // Support format decimal(x,0) or decimal(x,y) where true only if number fits in precision x,y
                // Precision = maximum number of digits to the left of the decimal point
                // If decimal(x,y) supplied, maximum precision = x - y
                var sqlDecimal = new SqlDecimal();
                sqlDecimal = field.ToSqlDecimal();
                if (sqlDecimal.IsNull == false)
                {
                    result = true;
                    if (typePrecision.Length > 0)
                    {
                        var parameters = typePrecision.Split(",".ToCharArray());
                        if (parameters.Length > 0)
                        {
                            int precision = 0;
                            int.TryParse(parameters[0], out precision);
                            if (precision > 0)
                            {
                                if (parameters.Length > 1)
                                {
                                    int scale = 0;
                                    int.TryParse(parameters[1], out scale);
                                    precision = precision - scale;
                                }
                                var    x = " " + sqlDecimal.Value.ToString().Replace("-", "") + ".";
                                string decPrecisionDigitCount = x.Substring(0, x.IndexOf(".")).Trim();
                                if (decPrecisionDigitCount.Length > precision)
                                {
                                    result = false;
                                }
                            }
                        }
                    }
                }
                break;

            case "float":
                var sqlFloat = new SqlDouble();
                sqlFloat = field.ToSqlDouble();
                if (sqlFloat.IsNull == false)
                {
                    result = true;
                }
                break;

            case "int":
                var sqlInt = new SqlInt32();
                sqlInt = field.ToSqlInt32();
                if (sqlInt.IsNull == false)
                {
                    result = true;
                }
                break;

            case "money":
                var sqlMoney = new SqlMoney();
                sqlMoney = field.ToSqlMoney();
                if (sqlMoney.IsNull == false)
                {
                    result = true;
                }
                break;

            case "real":
                var SqlSingle = new SqlSingle();
                SqlSingle = field.ToSqlSingle();
                if (SqlSingle.IsNull == false)
                {
                    result = true;
                }
                break;

            case "smallint":
                var sqlSmallInt = new SqlInt16();
                sqlSmallInt = field.ToSqlInt16();
                if (sqlSmallInt.IsNull == false)
                {
                    result = true;
                }
                break;

            case "smallmoney":
                var sqlSmallMoney = new SqlMoney();
                sqlSmallMoney = field.ToSqlMoney();
                if (sqlSmallMoney.IsNull == false)
                {
                    // Ensure that it will fit in a 4-byte small money
                    if (sqlSmallMoney.Value >= -214748.3648m && sqlSmallMoney.Value <= 214748.3647m)
                    {
                        result = true;
                    }
                }
                break;

            case "tinyint":
                var sqlTinyInt = new SqlByte();
                sqlTinyInt = field.ToSqlByte();
                if (sqlTinyInt.IsNull == false)
                {
                    result = true;
                }
                break;

            default:
                errorMessage = "Unrecognized format";
                break;
            }
        }
        catch (Exception)
        {
            if (string.IsNullOrEmpty(errorMessage) == false)
            {
                result = SqlBoolean.Null;
            }
        }
        return(result);
    }