我使用C#.net Windows窗体,我需要创建一个搜索文本框,它将显示组合框值(类似于谷歌搜索);显示在组合框中的值将是来自SQL 2005数据库的值(例如用户在FirstName上搜索,组合框将显示所有名字,当用户输入更多字母时,会显示所有名字,如果用户搜索姓氏,组合框显示数据库中的所有姓氏值..等)文本框搜索像谷歌
当我做上述任务
当我执行我写SQL查询这样
SELECT distinct(person_Firstname+''+person_Lastname)
AS
name FROM persondetails
WHERE name Like '%'[email protected]+'%'
这个查询给出了这样的错误---必须声明一个标量变量
我的目标是当我在搜索框中输入第一个字母,将显示所有名称以像谷歌的信...
任何一个可以解决这个....
private void tbautocomplete_TextChanged(object sender, EventArgs e)
{
AutoCompleteStringCollection namecollection = new AutoCompleteStringCollection();
SqlConnection con = new SqlConnection(@"Data Source=88888;Initial Catalog=contrynames;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT distinct(person_Firstname+''+person_Lastname) AS name FROM persondetails WHERE name Like '%'[email protected]+'%'";
con.Open();
SqlDataReader rea = cmd.ExecuteReader();
if (rea.HasRows == true)
{
while (rea.Read())
namecollection.Add(rea["name"].ToString());
}
rea.Close();
tbautocomplete.AutoCompleteMode = AutoCompleteMode.Suggest;
tbautocomplete.AutoCompleteSource = AutoCompleteSource.CustomSource;
tbautocomplete.AutoCompleteCustomSource = namecollection;
编辑您的问题以显示将文本框的值插入到SQL语句中的代码的完整剪切。 – 2011-06-11 15:07:59
它给错误,如“必须声明一个标量变量”在cmd.excutereader()语句 – user682417 2011-06-11 15:12:03