gpt4 book ai didi

c# - 从数据库中读取 ListBox SelectedItem 和 SELECT where id = value

转载 作者:行者123 更新时间:2023-11-30 21:57:52 26 4
gpt4 key购买 nike

所以我想从列表框中读取所选项目并从数据库中选择,其中 id = value 来自列表框并将完整详细信息显示到消息框......但我找不到它我在抛出时出错有人可以帮忙吗我如何解决这个问题...

代码

public List<Students> FullDetails(Students student)
{
List<Students> studentList = new List<Students>();
try
{
command.CommandText = "SELECT FROM Students WHERE ID = " + student.Id;

command.CommandType = CommandType.Text;
connection.Open();

OleDbDataReader reader = command.ExecuteReader();

while (reader.Read())
{
Students s = new Students();
s.Id = Convert.ToInt32(reader["ID"].ToString());
s.Name = reader["Name"].ToString();
s.LName = reader["LName"].ToString();
s.FName = reader["FName"].ToString();
s.MName = reader["MName"].ToString();
s.Absences = reader["Absences"].ToString();
s.Degrees = reader["Degrees"].ToString();

studentList.Add(s);
}
return studentList;
}
catch (Exception)
{
throw;
}
finally
{
if (connection != null)
{
connection.Close();
}
}
}

表单代码

Students s = new Students();
s = search_ls.SelectedItem as Students;
string fd = conn.FullDetails(s).ToString();
MessageBox.Show(fd);

最佳答案

您似乎错过了选择查询中的字段列表。我想你的意思是在 SELECT 和 FROM 之间加一个星号:

SELECT * FROM Students WHERE ID = ...

关于c# - 从数据库中读取 ListBox SelectedItem 和 SELECT where id = value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30458504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com