【发布时间】:2014-02-13 20:16:05
【问题描述】:
我在 sql azure 中创建了一个数据库。我正在尝试调用我在我的 sql azure 中放入的值并将其返回到我的 Web 服务中。我在我的 web.config 文件中添加了到 sql azure 的连接,如下所示
<connectionStrings>
<add name="ConnectionString"connectionString="Server=tcp:vvigan1a71.database.windows.net,1433;Database=(myname);User ID=(myid);Password=(myownpassword);Trusted_Connection=False;Encrypt=True;" />
</connectionStrings>
在我的网络服务文件(asmx)中,我尝试像下面这样建立连接。
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
我还声明了 2 个不同的变量,我将使用它来调用来自 sql azure 的值,如下所示
double taxafter30k = Convert.ToDouble("Select taxafter30k from TaxValue");
double taxafter50k = Convert.ToDouble("Select taxafter50k from TaxValue");
我试图将上面声明的方法调用到我的网络服务中
double totalcartaxOMV = 0d;
if (carValue <= 20000)
{
totalcartaxOMV = carValue;
}
else if (carValue > 20000 && carValue <= 50000)
{
totalcartaxOMV = ((20000 + ((carValue - 20000) * taxafter30k)));
}
else if (carValue > 50000)
{
totalcartaxOMV = (20000 + 42000 + ((carValue - 50000) * taxafter50k));
}
return totalcartaxOMV;
}
但是当我尝试运行时出现此错误?
System.FormatException: Input string was not in a correct format.
at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Convert.ToDouble(String value)
at Tax.CarTax.CarTaxwithOMV(Int32 carValue)
我无法使用字符串,因为我在任何一个字符串声明中都将整个 web 方法声明为双精度。因此,我在这里做错了什么来阻止我的 web 服务从 sql azure 调用数据吗?
编辑:网络方法
[WebMethod]
public double CarTaxwithOMV(int carValue)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
double taxafter30k = Convert.ToDouble("Select taxafter30k from TaxValue");
double taxafter50k = Convert.ToDouble("Select taxafter50k from TaxValue");
double totalcartaxOMV = 0d;
if (carValue <= 20000)
{
totalcartaxOMV = carValue;
}
else if (carValue > 20000 && carValue <= 50000)
{
totalcartaxOMV = ((20000 + ((carValue - 20000) * taxafter30k)));
}
else if (carValue > 50000)
{
totalcartaxOMV = (20000 + 42000 + ((carValue - 50000) * taxafter50k));
}
return totalcartaxOMV;
}
【问题讨论】:
-
请发布整个 Tax.CarTax.CarTaxwithOMV() 方法。例外是在该方法内。提示:Visual Studio 很好地支持的实时调试是你永远的第一好朋友。还有代码“double taxafter30k = Convert.ToDouble("Select taxafter30k from TaxValue");"不是您实际上所说的正确...原因不是该字符串是双精度的。
-
代码 --> double taxafter30k = Convert.ToDouble("Select taxafter30k from TaxValue"); --> 实际上不会在 Azure DB 上进行选择。
-
不,不是这样:/ 我在
string taxafter30k = ("Select taxafter30k from TaxValue");之前试过这个,但在我的totalcartaxOMV = ((20000 + ((carValue - 20000) * taxafter30k)));中他们会说operation '*' cannot be applied to operands of type int and string:(
标签: c# sql web-services azure type-conversion