Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 代码中没有错误,但从数据库检索到的数据不';我不会出现在列表框中_C#_Database_Visual Studio_Ms Access_Listbox - Fatal编程技术网

C# 代码中没有错误,但从数据库检索到的数据不';我不会出现在列表框中

C# 代码中没有错误,但从数据库检索到的数据不';我不会出现在列表框中,c#,database,visual-studio,ms-access,listbox,C#,Database,Visual Studio,Ms Access,Listbox,我试图从数据库中检索数据并将其显示在列表框中。我有下面的代码,当我运行它时,它没有给出任何错误或其他东西,但是列表框中没有显示任何数据 connection.Open(); DataTable dt = new DataTable(); OleDbCommand command = new OleDbCommand(); command.Connection = connection; command.CommandText = "select * from Appointments whe

我试图从数据库中检索数据并将其显示在列表框中。我有下面的代码,当我运行它时,它没有给出任何错误或其他东西,但是列表框中没有显示任何数据

connection.Open();

DataTable dt = new DataTable();

OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "select * from Appointments where PersonID = '" + textBox4.Text + "'";

OleDbDataReader reader = command.ExecuteReader();

dt.Load(reader);

foreach (DataRow Dr in dt.Rows)
{
    listBox1.Items.Add(Dr["PersonID"].ToString());
}

connection.Close();

您不显示连接字符串,但在VisualStudio中使用基于文件的数据库(似乎正在使用Access)时,它听起来像是一个老问题

如果MDB文件是项目的一部分,且其“操作”设置为“始终复制”,则每次运行应用程序时,BIN文件夹中的MDB文件将被源文件夹中的MDB文件覆盖,从而覆盖上次运行时所做的任何更改

请确认情况并非如此,因为这是问题的一个常见来源


干杯

我相信您需要调用listBox1.Refresh()-不过,请不要引用我的话,我不记得这是不是真的。我已经添加了您的建议,但不幸的是它不起作用。datatable是否包含任何行?您的SQL可能无法从数据库中获取任何符合条件的数据。不要使用SQL,请使用参数;您正在强制将Id(int?)转换为文本。不需要使用Datatable,只需使用reader即可。如果没有匹配的行,则不会发生任何情况。断点会将这个打印输出dt.Rows.Length显示到控制台中,以查看实际上是否有任何无关紧要的行。因为我在应用程序运行时添加数据。因此,在启动应用程序时,数据可能被覆盖是没有问题的。