2014-01-23 80 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); 
} 

然後使用在計算中「稅」的變量;

相關問題