如何使用c#单击一个按钮asp.net从表中检索某些数据行 [英] How to retrieve certain data row from table when clicked a button asp.net with c#
问题描述
你好我是一个新手,只是想知道如何从数据库中检索某些数据,就像我的数据库中填充了id,name,class等数据。
示例:name = kevin id = 123 class = 5
name = cris id = 156 class = 6
i想要使用name或id检索表的一行。如果我给的话id或name我应该得到完整的数据行
提前谢谢:)
Hello Im a newbie and just wanted to know how to retrieve a certain data from database ,its like i have my database filled with data such as id,name,class.
Example: name=kevin id=123 class=5
name=cris id=156 class=6
i want to retrieve one row of the table by using name or id.if i give the id or name and i should get the full data row
Thankyou in advance :)
推荐答案
SqlConnection con = new SqlConnection("DatabaseConnectionString");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Student WHERE ID = " + id, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
foreach(DataRow dr in dt.Rows)
{
idLbl.Text = dr["Id"].ToString();
nameLbl.Text = dr["Name"].ToString();
classLbl.Text = dr["ClassName"].ToString();
}
你可能会找到不同的SQL教程和C#
Beginners指导通过C#访问SQL Server [ ^ ] br />
http://msdn.microsoft.com/en -us / library / dw70f090(v = vs.110).aspx [ ^ ]
我希望这是你正在寻找的东西..
-KR
And you may find different tutorial for SQL and C#
Beginners guide to accessing SQL Server through C#[^]
http://msdn.microsoft.com/en-us/library/dw70f090(v=vs.110).aspx[^]
I hope this is what you're looking for..
-KR
id将是识别gridview中每一行的唯一键,你可以使用gridview的DataKeyNames属性来存储id而不显示它们。请参阅: DataKeyNames [ ^ ]
The id will be the unique key to identify each row in a gridview, you can use DataKeyNames property of gridview to store the ids without displaying them. Refer: DataKeyNames[^]
这篇关于如何使用c#单击一个按钮asp.net从表中检索某些数据行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!