如何检索SQL Server表的列值,并将其存储在label.Text C#ASP.Net的 [英] How to retrieve column value of sql server table and store it in label.Text of c# ASP.Net
本文介绍了如何检索SQL Server表的列值,并将其存储在label.Text C#ASP.Net的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是
假设我有一列FNAME,其值为尼基尔表中的配置文件。
Suppose I have a Column "fname" whose value is 'Nikhil' in table "profile".
如何检索SQL Server表的列值,并将其存储在label.Text C#ASP.Net的。
How to retrieve column value of sql server table and store it in label.Text of c# ASP.Net.
我的意思应该是code,如果我想标签文本是FNAME值是尼基尔
I mean what should be the code if I want label text to be "fname" value that is "Nikhil"
连接,因为我能够在GridView控件显示表中的数据已经做好。
Connection is already done properly because I am able to display table data in Gridview.
label1.Text =?; //我想在这里FNAME
label1.Text = ?; // I want fname here
问候,
尼基尔
推荐答案
首先,我建立了连接。
SqlConnection con = new SqlConnection("data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true");
SqlCommand cmd = new SqlCommand();
然后
cmd.CommandText = "select fname from table where qid=1";
cmd.Connection = con;
string fname = ((string)cmd.ExecuteScalar());
或
Label1.text = ((string)cmd.ExecuteScalar());
这篇关于如何检索SQL Server表的列值,并将其存储在label.Text C#ASP.Net的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文