【问题标题】:Unable to use sql azure database value in web service无法在 Web 服务中使用 sql azure 数据库值
【发布时间】: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


【解决方案1】:

您需要将 SqlCommand 和 SqlDataReader 与 SqlConnection 结合使用。您不能只是将 SQL 放入一个字符串中并期望它能够工作。

关于如何打开 SQL DB、从表中选择行并将它们的值提取到 .NET 变量(double、int、string 等)中有很多示例。一个随机的例子是here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多