示例#1
0
        public void Parse()
        {
            try {
                SqlDouble.Parse(null);
                Assert.Fail("#L01");
            } catch (ArgumentNullException e) {
                Assert.AreEqual(typeof(ArgumentNullException), e.GetType(), "#L02");
            }

            try {
                SqlDouble.Parse("not-a-number");
                Assert.Fail("#L03");
            } catch (FormatException e) {
                Assert.AreEqual(typeof(FormatException), e.GetType(), "#L04");
            }

            try {
                SqlDouble.Parse("9e400");
                Assert.Fail("#L05");
            } catch (OverflowException e) {
                Assert.AreEqual(typeof(OverflowException), e.GetType(), "#L06");
            }

            Assert.AreEqual((double)150, SqlDouble.Parse("150").Value, "#L07");
        }
示例#2
0
        public void Parse()
        {
            try
            {
                SqlDouble.Parse(null);
                Assert.False(true);
            }
            catch (ArgumentNullException e)
            {
                Assert.Equal(typeof(ArgumentNullException), e.GetType());
            }

            try
            {
                SqlDouble.Parse("not-a-number");
                Assert.False(true);
            }
            catch (FormatException e)
            {
                Assert.Equal(typeof(FormatException), e.GetType());
            }

            try
            {
                SqlDouble.Parse("9e400");
                Assert.False(true);
            }
            catch (OverflowException e)
            {
                Assert.Equal(typeof(OverflowException), e.GetType());
            }

            Assert.Equal(150, SqlDouble.Parse("150").Value);
        }
示例#3
0
        public override Expression Build()
        {
            object value = null;

            switch (_typeName)
            {
            case ("SqlBoolean"):
                value = SqlBoolean.Parse(_stringValue);
                break;

            case ("SqlInt32"):
                value = SqlInt32.Parse(_stringValue);
                break;

            case ("SqlString"):
                value = new SqlString(_stringValue);
                break;

            case ("SqlDouble"):
                value = SqlDouble.Parse(_stringValue);
                break;

            case ("SqlDateTime"):
                value = SqlDateTime.Parse(_stringValue);
                break;
            }
            Expression result = Expression.Constant(value, typeof(object));

            return(result);
        }
示例#4
0
        public void Parse()
        {
            Assert.Throws <ArgumentNullException>(() => SqlDouble.Parse(null));

            Assert.Throws <FormatException>(() => SqlDouble.Parse("not-a-number"));

            Assert.Throws <OverflowException>(() => SqlDouble.Parse("9e400"));
            Assert.Equal(150, SqlDouble.Parse("150").Value);
        }
示例#5
0
        public static void InsertLatestStockPrice(string symbol)
        {
            try
            {
                PermissionSet perms = new PermissionSet(PermissionState.None);
                string        url   = "http://finance.yahoo.com/d/quotes.csv?s=" + symbol +
                                      "&f=sl1d1t1c1ov";
                WebPermission webPerm = new WebPermission(NetworkAccess.Connect, url);
                perms.AddPermission(webPerm);

                SqlClientPermission sqlPerm = new SqlClientPermission(
                    PermissionState.None);
                sqlPerm.Add("context connection=true", "",
                            KeyRestrictionBehavior.AllowOnly);
                perms.AddPermission(sqlPerm);
                perms.PermitOnly();
                string[] data = HttpFileReader.ReadFile(url);
                string[] cols = data[0].Split(new char[] { ',' });

                string   date      = cols[2].Substring(1, cols[2].Length - 2);
                string   time      = cols[3].Substring(1, cols[3].Length - 2);
                DateTime tradetime = DateTime.Parse(date + " " + time);

                double    price     = Double.Parse(cols[1]);
                double    change    = Double.Parse(cols[4]);
                SqlDouble openprice = cols[5] == "N/A" ? SqlDouble.Null :
                                      SqlDouble.Parse(cols[5]);
                int volume = Int32.Parse(cols[6]);

                using (SqlConnection cn = new SqlConnection("context connection=true"))
                {
                    cn.Open();
                    string     cmdStr = "INSERT INTO StockPrices VALUES (@symbol, @price, @tradetime, @change, @openprice, @volume)";
                    SqlCommand cmd    = new SqlCommand(cmdStr, cn);
                    cmd.Parameters.AddWithValue("@symbol", symbol);
                    cmd.Parameters.AddWithValue("@price", price);
                    cmd.Parameters.AddWithValue("@tradetime", tradetime);
                    cmd.Parameters.AddWithValue("@change", change);
                    cmd.Parameters.AddWithValue("@openprice", openprice);
                    cmd.Parameters.AddWithValue("@volume", volume);
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception e)
            {
                SqlPipe pipe = SqlContext.Pipe;
                pipe.Send(e.Message);
            }
        }
示例#6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="s"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static PDouble Parse(string s, PValueType type)
        {
            PDouble sp;

            if (string.IsNullOrEmpty(s))
            {
                sp = new PDouble(type);
            }
            else
            {
                sp = SqlDouble.Parse(s);
            }

            return(sp);
        }
示例#7
0
        public static SqlDoubleCollection Parse(SqlString s)
        {
            if (s.IsNull)
            {
                return(Null);
            }

            return(new SqlDoubleCollection(SqlFormatting.ParseCollection <Double?>(s.Value,
                                                                                   t => !t.Equals(SqlFormatting.NullText, StringComparison.InvariantCultureIgnoreCase) ? SqlDouble.Parse(t).Value : default(Double?))));
        }
示例#8
0
 public override object FromStringValue(string xml)
 {
     return(SqlDouble.Parse(xml));
 }
示例#9
0
        public static PDouble Parse(string s, PValueType type)
        {
            var sp = string.IsNullOrEmpty(s) ? new PDouble(type) : SqlDouble.Parse(s);

            return(sp);
        }
示例#10
0
        private void AddToParameterCollection(Literal literal, string parameterName, SqlDataTypeOption sqlDataTypeOption, IList <Literal> sqlDataTypeParameters)
        {
            SqlParameter sqlParameter    = new SqlParameter();
            string       literalValue    = literal.Value;
            object       parsedValue     = null;
            SqlDbType    paramType       = SqlDbType.VarChar;
            bool         parseSuccessful = true;

            switch (sqlDataTypeOption)
            {
            case SqlDataTypeOption.Binary:
                paramType = SqlDbType.Binary;
                try
                {
                    parsedValue = TryParseBinaryLiteral(literalValue, VariableName, SqlDbType.Binary, literal.StartLine);
                }
                catch (ParameterizationFormatException)
                {
                    if (IsCodeSenseRequest)
                    {
                        parseSuccessful = false;
                        AddCodeSenseErrorItem(MessageHelper.MessageType.BINARY_LITERAL_PREFIX_MISSING_ERROR, literal, literal.Value, VariableName, SqlDbType.Binary.ToString());
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, SqlDbType.Binary.ToString(), C_SHARP_BYTE_ARRAY, literalValue, literal.StartLine, e);
                }
                break;

            case SqlDataTypeOption.VarBinary:
                paramType = SqlDbType.VarBinary;
                try
                {
                    parsedValue = TryParseBinaryLiteral(literalValue, VariableName, SqlDbType.VarBinary, literal.StartLine);
                    ExtractSize(sqlDataTypeParameters, sqlParameter);
                }
                catch (ParameterizationFormatException)
                {
                    if (IsCodeSenseRequest)
                    {
                        parseSuccessful = false;
                        string sqlDataTypeString = GetSqlDataTypeStringOneParameter(SqlDbType.VarBinary, sqlDataTypeParameters);
                        AddCodeSenseErrorItem(MessageHelper.MessageType.BINARY_LITERAL_PREFIX_MISSING_ERROR, literal, literalValue, VariableName, sqlDataTypeString);
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    string sqlDataTypeString = GetSqlDataTypeStringOneParameter(SqlDbType.VarBinary, sqlDataTypeParameters);
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, sqlDataTypeString, C_SHARP_BYTE_ARRAY, literalValue, literal.StartLine, e);
                }
                break;


            //Integer literals of form 24.0 will not be supported
            case SqlDataTypeOption.BigInt:
                paramType = SqlDbType.BigInt;
                long parsedLong;
                literalValue = IsNegative ? "-" + literalValue : literalValue;

                if (long.TryParse(literalValue, out parsedLong))
                {
                    parsedValue = parsedLong;
                }
                else
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, SqlDbType.BigInt.ToString(), "Int64", literalValue, literal.StartLine, null);
                }

                break;

            case SqlDataTypeOption.Int:
                paramType = SqlDbType.Int;
                int parsedInt;
                literalValue = IsNegative ? "-" + literalValue : literalValue;

                if (int.TryParse(literalValue, out parsedInt))
                {
                    parsedValue = parsedInt;
                }
                else
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, SqlDbType.Int.ToString(), "Int32", literalValue, literal.StartLine, null);
                }

                break;

            case SqlDataTypeOption.SmallInt:
                paramType = SqlDbType.SmallInt;
                short parsedShort;
                literalValue = IsNegative ? "-" + literalValue : literalValue;

                if (short.TryParse(literalValue, out parsedShort))
                {
                    parsedValue = parsedShort;
                }
                else
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, SqlDbType.SmallInt.ToString(), "Int16", literalValue, literal.StartLine, null);
                }

                break;

            case SqlDataTypeOption.TinyInt:
                paramType = SqlDbType.TinyInt;
                byte parsedByte;
                literalValue = IsNegative ? "-" + literalValue : literalValue;

                if (byte.TryParse(literalValue, out parsedByte))
                {
                    parsedValue = parsedByte;
                }
                else
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, SqlDbType.TinyInt.ToString(), "Byte", literalValue, literal.StartLine, null);
                }

                break;


            case SqlDataTypeOption.Real:
                paramType    = SqlDbType.Real;
                literalValue = IsNegative ? "-" + literalValue : literalValue;

                try
                {
                    parsedValue = SqlSingle.Parse(literalValue);
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, SqlDbType.Real.ToString(), "SqlSingle", literalValue, literal.StartLine, e);
                }

                break;

            case SqlDataTypeOption.Float:
                paramType    = SqlDbType.Float;
                literalValue = IsNegative ? "-" + literalValue : literalValue;

                try
                {
                    parsedValue = SqlDouble.Parse(literalValue);
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, SqlDbType.Float.ToString(), "SqlDouble", literalValue, literal.StartLine, e);
                }

                break;


            case SqlDataTypeOption.Decimal:
            case SqlDataTypeOption.Numeric:
                paramType = SqlDbType.Decimal;
                ExtractPrecisionAndScale(sqlDataTypeParameters, sqlParameter);
                literalValue = IsNegative ? "-" + literalValue : literalValue;

                try
                {
                    parsedValue = SqlDecimal.Parse(literalValue);
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    string sqlDecimalDataType = sqlDataTypeParameters != null ? (SqlDbType.Decimal + "(" + sqlDataTypeParameters[0] + ", " + sqlDataTypeParameters[1] + ")") : "";
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, sqlDecimalDataType, "SqlDecimal", literalValue, literal.StartLine, e);
                }

                break;

            case SqlDataTypeOption.Money:
                paramType    = SqlDbType.Money;
                literalValue = IsNegative ? "-" + literalValue : literalValue;

                try
                {
                    parsedValue = SqlMoney.Parse(literalValue);
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, SqlDbType.Money.ToString(), "SqlMoney", literalValue, literal.StartLine, e);
                }

                break;

            case SqlDataTypeOption.SmallMoney:
                paramType    = SqlDbType.SmallMoney;
                literalValue = IsNegative ? "-" + literalValue : literalValue;

                try
                {
                    parsedValue = SqlMoney.Parse(literalValue);
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, SqlDbType.SmallMoney.ToString(), "SqlMoney", literalValue, literal.StartLine, e);
                }

                break;

            case SqlDataTypeOption.DateTime:
                paramType = SqlDbType.DateTime;

                try
                {
                    parsedValue = ParseDateTime(literalValue);
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.DATE_TIME_ERROR_MESSAGE, literal, VariableName, SqlDbType.DateTime.ToString(), "DateTime", literalValue, literal.StartLine, e);
                }

                break;

            case SqlDataTypeOption.SmallDateTime:
                paramType = SqlDbType.SmallDateTime;

                try
                {
                    parsedValue = ParseDateTime(literalValue);
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.DATE_TIME_ERROR_MESSAGE, literal, VariableName, SqlDbType.SmallDateTime.ToString(), "DateTime", literalValue, literal.StartLine, e);
                }

                break;

            case SqlDataTypeOption.DateTime2:
                paramType = SqlDbType.DateTime2;

                try
                {
                    parsedValue = ParseDateTime(literalValue);
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.DATE_TIME_ERROR_MESSAGE, literal, VariableName, SqlDbType.DateTime2.ToString(), "DateTime", literalValue, literal.StartLine, e);
                }

                ExtractPrecision(sqlDataTypeParameters, sqlParameter);
                break;

            case SqlDataTypeOption.Date:
                paramType = SqlDbType.Date;

                try
                {
                    parsedValue = ParseDate(literalValue);
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.DATE_TIME_ERROR_MESSAGE, literal, VariableName, SqlDbType.Date.ToString(), "DateTime", literalValue, literal.StartLine, e);
                }

                break;

            case SqlDataTypeOption.DateTimeOffset:
                paramType = SqlDbType.DateTimeOffset;

                try
                {
                    parsedValue = ParseDateTimeOffset(literalValue);
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.DATE_TIME_ERROR_MESSAGE, literal, VariableName, SqlDbType.DateTimeOffset.ToString(), "DateTimeOffset", literalValue, literal.StartLine, e);
                }

                ExtractPrecision(sqlDataTypeParameters, sqlParameter);
                break;


            case SqlDataTypeOption.Time:
                paramType = SqlDbType.Time;

                try
                {
                    parsedValue = TimeSpan.Parse(literalValue);
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, SqlDbType.Time.ToString(), "TimeSpan", literalValue, literal.StartLine, e);
                }

                ExtractPrecision(sqlDataTypeParameters, sqlParameter);
                break;

            case SqlDataTypeOption.Char:
                paramType = SqlDbType.Char;
                ExtractSize(sqlDataTypeParameters, sqlParameter);
                break;

            case SqlDataTypeOption.VarChar:
                paramType = SqlDbType.VarChar;
                ExtractSize(sqlDataTypeParameters, sqlParameter);
                break;

            case SqlDataTypeOption.NChar:
                paramType = SqlDbType.NChar;
                ExtractSize(sqlDataTypeParameters, sqlParameter);
                break;

            case SqlDataTypeOption.NVarChar:
                paramType = SqlDbType.NVarChar;
                ExtractSize(sqlDataTypeParameters, sqlParameter);
                break;

            case SqlDataTypeOption.UniqueIdentifier:
                paramType = SqlDbType.UniqueIdentifier;

                try
                {
                    parsedValue = SqlGuid.Parse(literalValue);
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, SqlDbType.UniqueIdentifier.ToString(), "SqlGuid", literalValue, literal.StartLine, e);
                }

                break;

            case SqlDataTypeOption.Bit:
                paramType = SqlDbType.Bit;

                try
                {
                    parsedValue = Byte.Parse(literalValue);
                }
                catch (Exception e)
                {
                    parseSuccessful = false;
                    HandleError(MessageHelper.MessageType.ERROR_MESSAGE, literal, VariableName, SqlDbType.Bit.ToString(), "Byte", literalValue, literal.StartLine, e);
                }
                break;

            default:
                break;
            }

            if (parseSuccessful)
            {
                sqlParameter.ParameterName = parameterName;
                sqlParameter.SqlDbType     = paramType;
                sqlParameter.Value         = parsedValue ?? literalValue;
                sqlParameter.Direction     = ParameterDirection.Input;
                Parameters.Add(sqlParameter);
            }
        }
示例#11
0
    public static SqlBoolean InitMethod([SqlFacet(MaxSize = -1)] SqlString sqlDataValue, SqlString sqlDataType)
    {
        String          dataValue     = sqlDataValue.ToString().Trim();
        String          dataType      = sqlDataType.ToString().Trim();
        GroupCollection typeGroups    = splitType.Match(dataType).Groups;
        String          typeText      = typeGroups["type"].Value;
        String          typePrecision = typeGroups["precision"].Value;
        String          typeScale     = typeGroups["scale"].Value;

        try {
            switch (typeText.ToLower())
            {
            case "bit":
                SqlBoolean.Parse(dataValue);
                break;

            case "tinyint":
                SqlByte.Parse(dataValue);
                break;

            case "smallint":
                SqlInt16.Parse(dataValue);
                break;

            case "int":
                SqlInt32.Parse(dataValue);
                break;

            case "bigint":
                SqlInt64.Parse(dataValue);
                break;

            case "smallmoney":
                if (NumberOfDecimals(dataValue) > 4)
                {
                    throw new OverflowException();
                }
                SqlMoney smallmoneyValue = SqlMoney.Parse(dataValue);
                if (SqlMoney.LessThan(smallmoneyValue, smallmoneyMinValue) || SqlMoney.GreaterThan(smallmoneyValue, smallmoneyMaxValue))
                {
                    throw new OverflowException();
                }
                break;

            case "money":
                if (NumberOfDecimals(dataValue) > 4)
                {
                    throw new OverflowException();
                }
                SqlMoney.Parse(dataValue);
                break;

            case "decimal":
            case "numeric":
                if (NumberOfDecimals(dataValue) > Convert.ToInt32(typeScale))
                {
                    throw new OverflowException();
                }
                SqlDecimal.Parse(dataValue);
                break;

            case "real":
                SqlSingle singleValue = SqlSingle.Parse(dataValue);
                if (singleValue.ToString().Length != dataValue.Length + (NumberOfWholes(dataValue) == 0 ? 1 : 0))
                {
                    throw new OverflowException();
                }
                break;

            case "float":
                SqlDouble doubleValue = SqlDouble.Parse(dataValue);
                if (doubleValue.ToString().Length != dataValue.Length + (NumberOfWholes(dataValue) == 0 ? 1 : 0))
                {
                    throw new OverflowException();
                }
                break;

            case "date":
            case "datetime":
            case "datetime2":
                SqlDateTime.Parse(dataValue);
                break;

            case "char":
            case "varchar":
            case "nchar":
            case "nvarchar":
                if (typePrecision != "max" && dataValue.Length > Convert.ToInt32(typePrecision))
                {
                    throw new OverflowException();
                }
                break;

            case "uniqueidentifier":
                SqlGuid.Parse(dataValue);
                break;

            case "geometry":
                SqlGeometry.Parse(dataValue);
                break;

            case "geography":
                SqlGeography.Parse(dataValue);
                break;

            // we do not handle these at this time
            case "xml":
            case "time":
            case "datetimeoffset":
            case "hierarchyid":
            case "image":
            case "text":
            case "ntext":
            case "rowversion":
            case "sql_variant":
            case "table":
            case "timestamp":
            case "varbinary":
            default:
                break;
            }
            return(SqlBoolean.True);
        }
        catch {
            return(SqlBoolean.False);
        }
    }