如何在自动完成文本框中显示连接字符串

时间:2015-10-29 05:44:21

标签: vb.net winforms autocompletebox

我正在尝试将连接字符串显示为自动填充文本框 当用户开始输入时,它将按姓氏搜索。查询将查看数据库的全名包含...并在autosuggestion中显示全名,公司


如果我只是通过last_name搜索 并把last_name

MyCollection.Add(reader("last_name").ToString())

显示结果 但是下面的代码没有显示任何内容

这是我的代码

任何人都可以建议我如何解决这个问题?

Dim strauto As String

       Dim name As String
       name = Trim(txtCompanyKeyContacts.Text)
       If name = "" Then Exit Sub
       Dim cnString As String = ConfigurationManager.ConnectionStrings("dbcon").ConnectionString
       Dim con As New SqlConnection(cnString)

       Using con
           strauto = "SELECT (FULL_NAME + ',' + Company + ',' + Status) as contact  FROM name where FULL_NAME like '%" & Replace(name, "'", "''") & "%' "
           'SELECT last_name,FULL_NAME + ',' + Company + ',' + Status as contact FROM name"
           con.Open()
           Dim cmd As New SqlCommand(strauto, con)

           Dim reader As SqlDataReader = cmd.ExecuteReader()
           Dim MyCollection As New AutoCompleteStringCollection()

           If reader.HasRows = True Then
               While reader.Read()


                   ' MyCollection.Add(reader("FULL_NAME + ',' + Company + ',' + Status").ToString())
                   MyCollection.Add(reader("contact").ToString())
               End While
           End If



           txtCompanyKeyContacts.AutoCompleteCustomSource = MyCollection
           con.Close()
       End Using

1 个答案:

答案 0 :(得分:0)

This suggestion is a different direction...

I'd use a ComboBox instead to allow humans to see the list of suggestions. This will loosen things up a bit and result in more likely success IMO

    Dim sName As String = "elizibeth"
    Dim SQL As String = <x>
    SELECT [ContactName] + ' - ' + [CompanyName] Name
      FROM [northwind].[dbo].[Contacts] 
      WHERE soundex(ContactName) = soundex('<%= sName %>')
    </x>.Value
    Dim dt As DataTable = Gen.GetDataTable(SQL, sConnectNorthwind) ' my libray for a read-only datatable
    ComboBox1.DataSource = dt
    ComboBox1.DisplayMember = "Name"