简体   繁体   English

如何从ASP.net中的SQL查询将数据存储到变量?

[英]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.

相关问题 如何使用 ASP.NET C# 将 SQL 查询的结果存储在会话变量中以便以后使用 - How to store the result of SQL query in Session Variable using ASP.NET C# to use it later ASP.NET MVC 使用 WCF 从 SQL Server 获取数据:如何在控制器中放置临时数据存储 - ASP.NET MVC get data from SQL Server with WCF : how to put temporary data store in controller 如何将来自mysql查询的数据存储在变量中 - how to store data from mysql query in a variable 如何从html表单获取数据并将其存储在ASP.Net MVC视图中的ac#变量中 - how to get data from html form and store it in a c# variable in ASP.Net MVC view 如何将Json数据存储到ASP.NET中的Sql表中 - How to Store Json data into Sql table in asp.net 如何在asp.net的SQL表中存储IEnumerable &lt;&gt;数据值? - How to store IEnumerable<> data values in SQL table in asp.net? 如何在变量中存储SQL查询 - How to store sql query in variable 如何基于数据/ id关系从SQL查询中提取数据。 使用ASP.net WebMatrix - How to pull data from a SQL Query based on data/id relationship. Using ASP.net WebMatrix 如何将列表从ASP.NET Core存储到SQL服务器? - How to store list in SQL Server from ASP.NET Core? 如何在asp.net中存储用户数据? - how to store user data in asp.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM