哪位高手能给我提供一段从SQL里调一组字段到listbox1里的源程序??谢谢了!

来源:百度知道 编辑:UC知道 时间:2024/07/01 01:14:11
哪位高手能给我提供一段从SQL里调一组数据到listbox1里的源程序??谢谢了! 是关于ASP.net 的>

本来是用三层架构来写的,现在我拼凑在一起给你展示出来,只是核心程序哦。
假设:你要在ListBox1中显示数据库中Student表中的姓名,代码如下:
...
public class Student
{
private string _Name;
get { return _Name; }
set { _Radius = Name; }
}//创建Student类,篇幅关系,其他的类成员就不写了
...
SqlConnection _myConnection = new SqlConnection("连接字符串");
List<Student> list = new List<Student>;//创建一个Student类链表
SqlCommand cmd = new SqlCommand();
cmd.Connection = _myConnection;
cmd.CommandText = "SELECT Name FROM Student";
_myConnection.Open();
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
Student student = new Student();//实体化一个Student
student.Name=reader["Name"].ToString();//给student的Name属性赋值
list.Add(student);//在list中添加一个student
}
_myConnection.Close();

ListBox1.DataTextField="Name";//设置ListBox1的显示列为“Name”
ListBox1.DataSource=list;//设置ListBox1的数据源为list
ListBox1.DataB