我想從我的表中調用值在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獲取數據並使用它的唯一方法。
不,試圖用一個整數多個'SqlCommand'是*不是獲取數據的唯一方法。您需要*執行*命令,然後從中提取數據。您已經執行了這兩個命令,並將結果存儲在'dr'中......儘管這意味着您要從'taxAfter30k'丟棄數據。然後再次,你實際上並沒有在'dr'中使用數據......你只是在'dr.Read()'中引用它,但沒有得到這些值。基本上這個代碼被嚴重破壞了,你應該非常仔細地看它。例如,您可能希望有一個可以提取兩列的* single *命令 –