C# 我正在检索Ms.Access文件的数据表单,但它向我显示了文件名如何修复它?
我想通过列表从MS.Access检索数据,但当运行应用程序时,它会显示所有条目,但只显示文件名,如Student.StudentInformation,我不知道为什么,然后当我选择第一个条目时,我会在文本框中显示正确的数据? 我的问题是:C# 我正在检索Ms.Access文件的数据表单,但它向我显示了文件名如何修复它?,c#,c#-4.0,c#-3.0,C#,C# 4.0,C# 3.0,我想通过列表从MS.Access检索数据,但当运行应用程序时,它会显示所有条目,但只显示文件名,如Student.StudentInformation,我不知道为什么,然后当我选择第一个条目时,我会在文本框中显示正确的数据? 我的问题是: public ICollection<StudentInformation> GetStudents() { OleDbConnection con = new OleDbConnection(connectionString
public ICollection<StudentInformation> GetStudents()
{
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Students";
ObservableCollection<StudentInformation> students = new ObservableCollection<Student>();
try
{
con.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Student aStudent = new Student(Convert.ToInt32(reader["StudentID"]),
reader["StudentName"].ToString(),
reader["StudentEmail"].ToString());
students.Add(aStudent);
}
reader.Close();
return students;
}
finally
{
con.Close();
}
}
公共ICollection GetStudents()
{
OLEDB连接con=新的OLEDB连接(connectionString);
OleDbCommand cmd=新的OleDbCommand();
cmd.Connection=con;
cmd.CommandType=CommandType.Text;
cmd.CommandText=“从学生中选择*”;
ObservableCollection学生=新ObservableCollection();
尝试
{
con.Open();
OleDbDataReader=cmd.ExecuteReader();
while(reader.Read())
{
Student aStudent=新学生(转换为32(读卡器[“StudentID”),
reader[“StudentName”].ToString(),
阅读器[“StudentEmail”].ToString());
学生。添加(aStudent);
}
reader.Close();
留学生;
}
最后
{
con.Close();
}
}
在学生信息课上,我设置了两个构造函数,一个是默认构造函数,第二个是传递值
我在列表框中显示:
private ICollection<StudentInformation> students;
private void BtnGetStudent_Click(object sender, RoutedEventArgs e)
{
students = StudentDb.GetStudents();
Studentlst.ItemsSource = students;
}
私立i学院学生;
私有无效BtnGetStudent_单击(对象发送者,路由目标e)
{
students=StudentDb.GetStudents();
Studentlst.itemsource=学生;
}
它向我显示类似Student.StudentInformation的列表
如何修复它?这似乎是一个演示问题。您应该解释WPF(您正在使用WPF,对吗?)如何显示
学生信息
作为一种简单的解决方法,您可以为学生信息
重载函数ToString()
,但更好的解决方案是为其编写一个数据模板
。(你知道怎么做吗?)
请注意:在UI线程中访问数据库不是一个很好的主意,这会在查询期间阻塞您的UI。非常感谢我尝试过使用重载功能,并且效果良好。非常感谢。