【发布时间】:2014-03-10 18:04:11
【问题描述】:
我在 Windows 窗体应用程序中有一个文本框(名为 textbox1)。我有一个名为 nn.sdf 的数据库,我想将其用作自动完成的来源。每当用户在 textbox1 中输入时,它都会显示来自数据库的建议,与用户给出的输入文本相匹配。所以我将我的代码放入textBox1_TextChangedproperty。我的代码在这里:
private void textBox1_TextChanged(object sender, EventArgs e)
{
AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Users\Imon-Bayazid\Documents\nn.sdf");
con.Open();
SqlCeCommand cmnd = con.CreateCommand();
cmnd.CommandType = CommandType.Text;
cmnd.CommandText = "SELECT top(10) english FROM dic";
SqlCeDataReader dReader;
dReader = cmnd.ExecuteReader();
if (dReader.Read())
{
while (dReader.Read())
namesCollection.Add(dReader["english"].ToString());
}
else
{
MessageBox.Show("Data not found");
}
dReader.Close();
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = namesCollection;
}
但它只显示前 10 个数据。我知道我的行有问题
cmnd.CommandText = "SELECT top(10) english FROM dic";// english is my column name and dic is my table name
我不知道 cmnd.CommandText 应该是什么。每当用户在 textbox1 中输入任何内容时,我都想要自动提示。 我该怎么做???
【问题讨论】:
标签: c# autocomplete