我想从我的表中调用值在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 *命令 –