如何从数据库中将值存储在标签中 [英] how to store values in lables from database
本文介绍了如何从数据库中将值存储在标签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["itax"].ConnectionString;
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT @UserId 'UserId',@userPassword 'userPassword' FROM Userlogin WHERE UserId = @UserId", con);
cmd.Parameters.Add("@UserId", SqlDbType.VarChar, 50).Value = lbl_Employee_code.Text;
cmd.Parameters.Add("@userPassword", SqlDbType.VarChar, 50).Value = lbl_employee_name.Text;
cmd.ExecuteNonQuery();
con.Close();
推荐答案
您需要将查询结果存储到数据读取器,并且可以在UI中将其分配给标签
这是一个示例 [
You need to store your query-result to a data-reader and in your UI you can assign it to a label
Here is a sample[^]
Regards
Sebastian
使用
ExecuteQuery();返回SqlDataReader
而不是
cmd.ExecuteNonQuery();.
Use
ExecuteQuery(); that returnsSqlDataReader
instead of
cmd.ExecuteNonQuery();.
SqlDataReader dr=cmd.ExecuteQuery();
if(dr.Read())
{
Label1.Text=dr["UserId"].ToString();
}
这篇关于如何从数据库中将值存储在标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文