[英]How to store data to variable from SQL query in ASP net?
So here is my code. 这是我的代码。 What I'm trying to do store double rate_num value to number variable.
我正在尝试将double rate_num值存储到数字变量中。 But it is still getting an error.
但是它仍然出现错误。
String conn = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
string query = "select " + rate_from + " from Exchange_rate where ERates_Status = 'Active'";
SqlConnection sqlcon = new SqlConnection(conn);
sqlcon.Open();
SqlCommand cmd = new SqlCommand(query, sqlcon);
SqlDataReader reader = cmd.ExecuteReader();
double number = "";
while (reader.Read())
{
double rate_num = reader.GetDouble(0);
number = rate_num
}
number;
sqlcon.Close();
I have two findings here: 我在这里有两个发现:
number += rate_num;
I guess you want a sum of the rates. 我想您想要这些费用的总和。 Since number is defined as a string you will get a string as result where all rates are concatenated.
由于数字被定义为字符串,因此您将得到一个字符串,其中所有费率都串联在一起。 Define number as double:
将数字定义为双精度:
double number = 0;
Second is the line after the while-loop: 第二个是while循环之后的行:
number;
What do you want to archive here? 您要在这里存档什么? This line will not compile neither if number is a string nor double.
如果number是字符串或double,则此行将不会编译。 If this code is part of a method you pssibly want to return the calculated sum of rates.
如果此代码是方法的一部分,则可能要返回计算出的比率总和。 So you could
所以你可以
return number;
But first close your DB connection 但是首先关闭您的数据库连接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.