通过c#.net自动完成带有sql的文本框
问题描述:
嗨
我在Windows应用程序中有自动完成文本框,其中填充了数据库中的列表。但输入它只是从第一个字符开始过滤。我应该如何提供在自动完成文本中间搜索LIKE运算符的选项。
当我的用户开始输入jas之间的所有列表jas也应该被过滤和显示。
提前谢谢
i写了但是这段代码不起作用
Hi
I have auto complete text box in Windows application where in it is populating the list from database. But typing it is filtering only starting with first character. How should i able to provide option to search in middle of the auto complete text as a LIKE operator.
When my user start typing jas all the list in between matches of jas also should be filtered and displayed.
Thanks in advance
i was wrote that but this code did not work
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace auto_complete_textbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
AutoCompleteStringCollection ACSC = new AutoCompleteStringCollection();
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
SqlCommand sqm = new SqlCommand("SELECT Location FROM DBO.Incident WHERE Location LIKE '%@Location%' ", new SqlConnection("data source=.;database=A police control system;uid=sa;pwd=sql"));
sqm.Connection.Open();
sqm.Parameters.Add("@Location", SqlDbType.NVarChar).Value = textBox1.Text;
SqlDataReader sdr = sqm.ExecuteReader();
if (sdr.HasRows == true)
{
while (sdr.Read())
{
ACSC.Add(sdr["Location"].ToString());
}
}
sqm.Connection.Close();
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = ACSC;
}
catch (Exception ex)
{
messagebox.show(ex.message);
}
}
答
请参阅此链接 - >
http://www.codeproject.com/Articles/251110/AutoComplete-TextBox-with-substing-search-similar
plese refere this link-->
http://www.codeproject.com/Articles/251110/AutoComplete-TextBox-with-substing-search-similar