2014-01-23 79 views
0

我想从我的表中调用值在sql azure中的值,因此我做了我的web服务和我之间的连接sql azure。我添加了一个ConnectionString的进入我的web.config建立连接操作'*'不能应用于类型'int'和'System.Data.SqlClient.SqlCommand'的操作数

<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> 

然后我试图通过我的ConnectionString在我的web服务文件链接蔚蓝。

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 

我添加一个DataReader从我的SQL Azure的读取数据并调用的值,并使用它像下面所示

public double CarTaxwithOMV(int carValue) 
    { 

     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 
     con.Open(); 
     SqlCommand taxafter30k = new SqlCommand("Select taxafter30k from TaxValue"); 
     SqlCommand taxafter50k = new SqlCommand("Select taxafter50k from TaxValue"); 
     SqlDataReader dr; 

     dr = taxafter30k.ExecuteReader(); 
     dr = taxafter50k.ExecuteReader(); 

     if (dr.Read()) 
     { 
      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)); 
      } 
      con.Close(); 
      return totalcartaxOMV; 
     } 
    } 

然而,在我的

totalcartaxOMV = ((20000 + ((carValue - 20000) * taxafter30k))); 

我接收到该错误

operation '*' cannot be applied to operand of type 'int' and 'System.Data.SqlClient.SqlCommand' 

Unf幸运的是,这似乎是连接和从我的SQL Azure获取数据并使用它的唯一方法。

+1

不,试图用一个整数多个'SqlCommand'是*不是获取数据的唯一方法。您需要*执行*命令,然后从中提取数据。您已经执行了这两个命令,并将结果存储在'dr'中......尽管这意味着您要从'taxAfter30k'丢弃数据。然后再次,你实际上并没有在'dr'中使用数据......你只是在'dr.Read()'中引用它,但没有得到这些值。基本上这个代码被严重破坏了,你应该非常仔细地看它。例如,您可能希望有一个可以提取两列的* single *命令 –

回答

1

你不能使用taxafter30k,它的SqlCommand的

你的代码应该是

totalcartaxOMV = ((20000 + ((carValue - 20000) * Convert.ToDouble(
dr["taxafter30k"]))); 

SqlCommand taxafter30k = new SqlCommand("Select taxafter30k from TaxValue"); 
    SqlCommand taxafter50k = new SqlCommand("Select taxafter50k from TaxValue"); 
    SqlDataReader dr; 

    double taxafter30kNew = Convert.ToDouble(taxafter30k.ExecuteScalar()); 
    double taxafter30kNew = Convert.ToDouble(taxafter50k.ExecuteScalar()); 


     double totalcartaxOMV = 0d; 

     if (carValue <= 20000) 
     { 
      totalcartaxOMV = carValue; 
     } 
     else if (carValue > 20000 && carValue <= 50000) 
     { 
      totalcartaxOMV = ((20000 + ((carValue - 20000) * taxafter30kNew))); 
     } 
     else if (carValue > 50000) 
     { 
      totalcartaxOMV = (20000 + 42000 + ((carValue - 50000) * taxafter50kNew)); 
     } 
     con.Close(); 
     return totalcartaxOMV; 
0

您需要taxafter30k更改为

(double)dr["taxafter30k"]

0

这条线:

dr = taxafter30k.ExecuteReader(); 

将返回,你必须通过浏览结果集,而不是一个结果。 使用Intellisense,您应该能够轻松找到所需的方法或还原为其他执行方法。

对于单个标量结果使用:

dr = taxafter30k.ExecuteScalar(); 

你必须相应地调整你的查询。

0

“taxafter30k” 取而代之的是一个SqlDataReader。 您需要检索SqlDataReader的值(假设它是一个int):

int tax = 0; 
if(taxafer30k.Read()) 
{ 
    tax = taxafter30k.GetInt32(0); 
} 

然后使用在计算中“税”的变量;

相关问题