【发布时间】:2015-10-22 12:21:56
【问题描述】:
我是 c# 的新手,正在上课,但是我正在努力做的事情,我知道我已经提前了很多。 我有一个带有列表框和文本框的表单。 这就是我填充列表框的方式
private void Centrale_Gegevens_Load(object sender, EventArgs e)
try
{
OleDbConnection verbinding = new OleDbConnection();
verbinding.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=E:\Het Vlaamse Kruis\Het Vlaamse Kruis\data\Patienten.accdb; Jet OLEDB:Database Password=internet;";
verbinding.Open();
OleDbCommand combo = new OleDbCommand();
combo.Connection = verbinding;
string query = "select NaamPatient from tbl_Patient";
combo.CommandText = query;
OleDbDataReader reader = combo.ExecuteReader();
while (reader.Read())
{
lstBox.Items.Add(reader["NaamPatient"]);
}
verbinding.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
列表框以这种方式填充人名。 名为 textbox1 的文本框是我想要用来过滤列表框的。
这是我得到的sofare,但它不起作用。
private void textBox1_TextChanged(object sender, EventArgs e)
{
OleDbConnection verbinding = new OleDbConnection();
verbinding.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=E:\Het Vlaamse Kruis\Het Vlaamse Kruis\data\Patienten.accdb; Jet OLEDB:Database Password=internet;";
verbinding.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tbl_Patienten where NaamPatient like '" + textBox1.Text + "%' ";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
lstBox.DataSource = dt;
lstBox.DisplayMember = "NaamPatient";
verbinding.Close();
}
我几乎可以在网上找到所有关于它的东西,不管我做什么,我都无法让它工作。 如果我在文本框中键入 A,列表框会显示所有以 A 开头的名称,如果我键入 AB,则列表框会显示以 AB 开头的所有内容等。 提前致谢
【问题讨论】:
-
textBox1.Text的值是多少,NaamPatient列的类型到底是什么?你的cmd.ExecuteNonQuery();是不必要的。