如何从表中检索值到变量并显示到文本框 [英] how to retrieve value from table into a variable and display to textbox
问题描述
例如:
命令:从公司选择总和(薪水),即工资总额= 10000
i希望将总和存储到变量X中,即X = 10000
我想将1000显示到文本框
< br $> b $ b
尝试
{
SqlConnection con = new SqlConnection(con_str);
con.Open();
SqlCommand cmd10 = new SqlCommand( 选择SUM(salary_amt)公司,con);
decimal x = cmd10.ExecuteNonQuery();
Label1.text = Total +。ToString();
con.Close();
}
catch (例外情况)
{
Response.Write(ex.Message);
}
解决方案1和解决方案2都为您提供了一些链接,可以引导您找到一些适用于你。
作为一个exaplanation为什么你的代码不工作 - 这个:
cmd10.ExecuteNonQuery( );
除了查询所影响的行数之外不会返回任何内容,因此执行 NonQuery 。它通常用于更新和删除语句。要查询您期望查询结果(Select-Query)的内容,您必须使用ExecuteReader(..),如解决方案1和2的链接示例中所示。
看看这里
以下几篇文章将帮助您完成任务。
http://stackoverflow.com/questions/16565035/how-do-i- get-values-from-a-sql-database-into-textboxes-using-c [ ^ ]
http:/ /www.c-sharpcorner.com/forums /thread/150572/retrieving-data-from-database-into-textbox-in-C-Sharp-using.aspx [ ^ ]
for example :
command : select sum(Salary) from company i.e salary total=10000
i want to store the total sum into a variable X i.e X=10000
and i want to display that 1000 to textbox
try
{
SqlConnection con = new SqlConnection(con_str);
con.Open();
SqlCommand cmd10 = new SqlCommand("select SUM(salary_amt) company ", con);
decimal x = cmd10.ExecuteNonQuery();
Label1.text="Total"+.ToString();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Both Solution 1 and Solution 2 provide you with links that lead you to some example code that will work for you.
As an exaplanation why your code is not working - this:
cmd10.ExecuteNonQuery();
doesn't return anything but the number of rows affected by the query, hence "ExecuteNonQuery". It's usually used for Update- and Delete-Statements. To query something where you expect a query result (a Select-Query) you have to use ExecuteReader(..), as you can see in the linked examples of Solution 1 and 2.
Have look Here
Below are few posts, that will help you to complete your task.
http://stackoverflow.com/questions/16565035/how-do-i-get-values-from-a-sql-database-into-textboxes-using-c[^]
http://www.c-sharpcorner.com/forums/thread/150572/retrieving-data-from-database-into-textbox-in-C-Sharp-using.aspx[^]
这篇关于如何从表中检索值到变量并显示到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!